Added Dependency Injection configurator
Signed-off-by: Gregory PLANCHAT <g.planchat@gmail.com>
This commit is contained in:
parent
d75265a8fe
commit
7a55d6e026
2 changed files with 62 additions and 41 deletions
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Wootook\Core\DependencyInjection\Config;
|
||||||
|
|
||||||
|
use Wootook\Core\Config,
|
||||||
|
Wootook\Core\DependencyInjection;
|
||||||
|
|
||||||
|
class Config
|
||||||
|
{
|
||||||
|
public function __invoke(DependencyInjection\Factory $factory, Config\Node $config)
|
||||||
|
{
|
||||||
|
foreach ($config as $className => $classConfig) {
|
||||||
|
$classDefinition = $factory->initClassDefinition($className);
|
||||||
|
$factory->registerClassDefinition($className, $classDefinition);
|
||||||
|
|
||||||
|
if ($classConfig->methods instanceof Config\Node) {
|
||||||
|
foreach ($classConfig->methods as $methodName => $methodConfig) {
|
||||||
|
$classDefinition->
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -12,27 +12,15 @@ class Factory
|
||||||
|
|
||||||
protected $_registry = null;
|
protected $_registry = null;
|
||||||
|
|
||||||
protected $_definitions = array();
|
protected $_classDefinitions = array();
|
||||||
|
|
||||||
protected function _construct(Config\Node $config, Registry $registry = null)
|
protected $_classDefinitionClassName = 'Wootook\\Core\\DependencyInjection\\Definition\\ClassDefinition';
|
||||||
|
|
||||||
|
protected function _construct(Registry $registry = null)
|
||||||
{
|
{
|
||||||
if ($registry !== null) {
|
if ($registry !== null) {
|
||||||
$this->setRegistry($registry);
|
$this->setRegistry($registry);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($config as $className => $constructorArguments) {
|
|
||||||
$definition = $this->getDefinition($className);
|
|
||||||
|
|
||||||
if ($constructorArguments === null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($constructorArguments as $argumentId => $argumentConfig) {
|
|
||||||
$argumentType = key($argumentConfig);
|
|
||||||
$argumentValue = key($argumentConfig);
|
|
||||||
$definition->addConstructorArgument($argumentId, $argumentType, $argumentValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setRegistry(Registry $registry)
|
public function setRegistry(Registry $registry)
|
||||||
|
|
@ -51,41 +39,51 @@ class Factory
|
||||||
return $this->_registry;
|
return $this->_registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDefinition($className)
|
public function getClassDefinition($className)
|
||||||
{
|
{
|
||||||
if (!isset($this->_definitions[$className])) {
|
if (isset($this->_classDefinitions[$className])) {
|
||||||
$this->_definitions[$className] = new Definition\ClassDefinition($className);
|
return $this->_classDefinitions[$className];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_definitions[$className];
|
return $this->initClassDefinition($className);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get($className, Array $additionalArguments = array())
|
public function initClassDefinition($className)
|
||||||
{
|
{
|
||||||
$definition = $this->getDefinition($className);
|
$class = $this->getClassDefinitionClassName();
|
||||||
|
return new $class($className);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
public function registerClassDefinition($className, Definition\ClassDefinition $classDefinition)
|
||||||
$reflectionClass = new \ReflectionClass($className);
|
{
|
||||||
} catch (\ReflectionException $e) {
|
$this->_classDefinitions[$className] = $classDefinition;
|
||||||
throw new CoreException\RuntimeException(sprintf('Class "%s" not found', $className), null, $e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$reflectionClass->hasMethod('__construct')) {
|
return $this;
|
||||||
return $reflectionClass->newInstance();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
public function addClassDefinition($className)
|
||||||
$reflectionMethod = $reflectionClass->getMethod('__construct');
|
{
|
||||||
} catch (\ReflectionException $e) {
|
$this->registerClassDefinition($className, $this->initClassDefinition($className));
|
||||||
throw new CoreException\RuntimeException(sprintf('Class "%s" has no contructor', $className), null, $e);
|
|
||||||
}
|
|
||||||
|
|
||||||
$constructorArguments = $definition->prepareArguments($reflectionMethod, $this->getRegistry(), $additionalArguments);
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
public function setClassDefinitionClassName($className)
|
||||||
return $reflectionClass->newInstanceArgs($constructorArguments);
|
{
|
||||||
} catch (\ReflectionException $e) {
|
$this->_classDefinitionClassName = $className;
|
||||||
throw new CoreException\RuntimeException(sprintf('Class "%s" could not be instanciated', $className), null, $e);
|
|
||||||
}
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getClassDefinitionClassName()
|
||||||
|
{
|
||||||
|
return $this->_classDefinitionClassName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __invoke($className, Array $additionalArguments = array())
|
||||||
|
{
|
||||||
|
$definition = $this->getClassDefinition($className);
|
||||||
|
|
||||||
|
return $definition->newInstance($additionalArguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue