Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
87.50% |
7 / 8 |
CRAP | |
95.45% |
21 / 22 |
SimpleFactory | |
0.00% |
0 / 1 |
|
87.50% |
7 / 8 |
12 | |
95.45% |
21 / 22 |
__construct(\aae\di\DependencyResolverInterface $dependencyResolver = NULL) | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
build() | |
100.00% |
1 / 1 |
3 | |
100.00% |
7 / 7 |
|||
_constructorArgumentsGiven() | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
_classIsNotDefined() | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
_buildWithoutConstructorArguments() | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
_buildWithConstructorArguments() | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
buildWithArgsArray($className, $constructorArgs) | |
0.00% |
0 / 1 |
3.14 | |
75.00% |
3 / 4 |
|||
buildWithResolvedDependencies($className, $overrides = array()) | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
<?php | |
/** | |
* | |
*/ | |
namespace aae\std { | |
/** | |
* @author Axel Ancona Esselmann | |
* @package aae\std | |
*/ | |
class SimpleFactory { | |
private $_className = null, $_args = null, $_dependencyResolver; | |
public function __construct(\aae\di\DependencyResolverInterface $dependencyResolver = NULL) { | |
$this->_dependencyResolver = $dependencyResolver; | |
} | |
public function build() { | |
$this->_args = func_get_args(); | |
$this->_className = array_shift($this->_args); | |
if ($this->_classIsNotDefined()) | |
throw new BuildException(sprintf("The class '%s' is not defined.", $this->_className), 211141513); | |
return ($this->_constructorArgumentsGiven()) | |
? $this->_buildWithConstructorArguments() | |
: $this->_buildWithoutConstructorArguments(); | |
} | |
private function _constructorArgumentsGiven() { | |
return count($this->_args) > 0; | |
} | |
private function _classIsNotDefined() { | |
return !class_exists($this->_className); | |
} | |
private function _buildWithoutConstructorArguments() { | |
$className = $this->_className; | |
return new $className(); | |
} | |
private function _buildWithConstructorArguments() { | |
$reflect = new \ReflectionClass($this->_className); | |
#if (!method_exists($reflect, "__construct")) { | |
# $this->_buildWithoutConstructorArguments(); | |
#} | |
return $reflect->newInstanceArgs($this->_args); | |
} | |
public function buildWithArgsArray($className, $constructorArgs) { | |
if (!is_string($className) || !is_array($constructorArgs)) { | |
throw new \Exception(sprintf("Arguments have to be a string and an array, %s and %s given.", gettype($className), gettype($constructorArgs)), 227141322); | |
} | |
$arguments = array_merge(array($className), $constructorArgs); | |
return call_user_func_array(array($this, "build"), $arguments); | |
} | |
public function buildWithResolvedDependencies($className, $overrides = array()) { | |
$dependencies = $this->_dependencyResolver->resolve($className, "__construct", $overrides); | |
array_unshift($dependencies, $className); | |
return call_user_func_array(array($this, "build"), $dependencies); | |
} | |
} | |
} |