Skip to content

Commit

Permalink
Merge pull request #4 from picamator/dev
Browse files Browse the repository at this point in the history
Added class_exists check. Fixed bug in readme example.
  • Loading branch information
picamator committed Apr 4, 2017
2 parents 57412c5 + f170c0f commit b5580bd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

1.2.0 (2017-04-04)
------------------
* Added ``class_exists`` check for creating object with construct's arguments
* Fixed bug in readme example

1.1.0 (2017-04-01)
------------------
* Optimized performance. Moved result of ``__construct`` method exist check to internal cache.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -70,7 +70,7 @@ class UserRepository

public function __construct()
{
$this->connection = ObjectManagerSingleton::getInstance()->create();
$this->connection = ObjectManagerSingleton::getInstance()->create('Connection');
}
}
```
Expand Down
8 changes: 6 additions & 2 deletions src/ObjectManager.php
Expand Up @@ -22,6 +22,7 @@ class ObjectManager implements ObjectManagerInterface
public function create($className, array $arguments = [])
{
if (empty($arguments)) {
// for performance reason ``class_exists`` is not checked here
return new $className();
}

Expand All @@ -42,8 +43,11 @@ private function getReflection($className)
return $this->reflectionContainer[$className];
}

// construction does not available
if (method_exists($className, '__construct') === false) {
if (!class_exists($className)) {
throw new RuntimeException(sprintf('Class "%s" does not exist', $className));
}

if (!method_exists($className, '__construct')) {
throw new RuntimeException(sprintf('Class "%s" does not have __construct', $className));
}

Expand Down
10 changes: 9 additions & 1 deletion tests/unit/src/ObjectManagerTest.php
Expand Up @@ -35,7 +35,15 @@ public function testCreate(array $arguments)
/**
* @expectedException \Picamator\ObjectManager\Exception\RuntimeException
*/
public function testFailCreate()
public function testFailNoClassCreate()
{
$this->objectManager->create('NotExistingClass', [1, 2]);
}

/**
* @expectedException \Picamator\ObjectManager\Exception\RuntimeException
*/
public function testFailNoConstructCreate()
{
$this->objectManager->create('Picamator\ObjectManager\ObjectManager', [1, 2]);
}
Expand Down

0 comments on commit b5580bd

Please sign in to comment.