| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* This source file is subject to the new BSD license that is bundled |
| 6 |
|
* with this package in the file LICENSE.txt. |
| 7 |
|
* It is also available through the world-wide-web at this URL: |
| 8 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 9 |
|
* |
| 10 |
|
* @category Xyster |
| 11 |
|
* @package Xyster_Container |
| 12 |
|
* @copyright Copyright LibreWorks, LLC (http://libreworks.net) |
| 13 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 14 |
|
* @version $Id$ |
| 15 |
|
*/ |
| 16 |
|
namespace Xyster\Container\Injector; |
| 17 |
|
/** |
| 18 |
|
* Provides instances of the component type |
| 19 |
|
* |
| 20 |
|
* @category Xyster |
| 21 |
|
* @package Xyster_Container |
| 22 |
|
* @copyright Copyright LibreWorks, LLC (http://libreworks.net) |
| 23 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 24 |
|
*/ |
| 25 |
|
abstract class AbstractInjector extends \Xyster\Container\Provider\AbstractProvider |
| 26 |
1 |
{ |
| 27 |
|
/** |
| 28 |
|
* Creates a new provider |
| 29 |
|
* |
| 30 |
|
* @param \Xyster\Container\Definition $def The component definintion |
| 31 |
|
*/ |
| 32 |
|
public function __construct(\Xyster\Container\Definition $def) |
| 33 |
|
{ |
| 34 |
51 |
parent::__construct($def); |
| 35 |
51 |
$this->_checkConcrete(); |
| 36 |
|
} |
| 37 |
|
|
| 38 |
|
/** |
| 39 |
|
* Checks to make sure the current implementation is a concrete class |
| 40 |
|
* |
| 41 |
|
* @throws Exception if the implementation isn't concrete |
| 42 |
|
*/ |
| 43 |
|
protected function _checkConcrete() |
| 44 |
|
{ |
| 45 |
51 |
$class = $this->getType()->getClass(); |
| 46 |
51 |
if ( $class instanceof \ReflectionClass && ( $class->isInterface() || $class->isAbstract() ) ) { |
| 47 |
1 |
throw new Exception($class->getName() . ' is not a concrete class'); |
| 48 |
|
} |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
/** |
| 52 |
|
* Gets the member arguments |
| 53 |
|
* |
| 54 |
|
* @param \Xyster\Container\IContainer $container |
| 55 |
|
* @param \ReflectionMethod $member |
| 56 |
|
* @return array |
| 57 |
|
*/ |
| 58 |
|
public function getMemberArguments( \Xyster\Container\IContainer $container, \ReflectionMethod $member = null ) |
| 59 |
|
{ |
| 60 |
15 |
if ( $member === null || !$member->getNumberOfParameters() ) { |
| 61 |
10 |
return array(); |
| 62 |
|
} |
| 63 |
8 |
$result = array(); |
| 64 |
8 |
$types = \Xyster\Type\Type::getForParameters($member); |
| 65 |
8 |
$numOfArgs = count($this->_constructorArguments); |
| 66 |
8 |
if ( $numOfArgs < $member->getNumberOfRequiredParameters() ) { |
| 67 |
1 |
throw new Exception('The number of required method parameters must equal the number of arguments provided'); |
| 68 |
|
} |
| 69 |
7 |
foreach( $member->getParameters() as $k => $reflectionParameter ) { |
| 70 |
|
/* @var $reflectionParameter \ReflectionParameter */ |
| 71 |
7 |
$instance = null; |
| 72 |
7 |
$paramType = $types[$k]; |
| 73 |
|
/* @var $paramType \Xyster\Type\Type */ |
| 74 |
7 |
$argument = isset($this->_constructorArguments[$k]) ? |
| 75 |
7 |
$this->_constructorArguments[$k] : null; |
| 76 |
7 |
if ( $paramType->isInstance($argument) ) { |
| 77 |
1 |
$instance = $argument; |
| 78 |
7 |
} else if ( $container->contains($argument) ) { |
| 79 |
3 |
$instance = $container->get($argument); |
| 80 |
7 |
} else if ( $reflectionParameter->isOptional() ) { |
| 81 |
6 |
$instance = $reflectionParameter->getDefaultValue(); |
| 82 |
6 |
} else { |
| 83 |
1 |
throw new Exception('Cannot inject method argument ' . |
| 84 |
1 |
$reflectionParameter->getName() . |
| 85 |
1 |
' into ' . $member->getDeclaringClass()->getName() . |
| 86 |
1 |
': key not found in the container: ' . $argument); |
| 87 |
|
} |
| 88 |
6 |
$result[] = $instance; |
| 89 |
6 |
} |
| 90 |
6 |
return $result; |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
/** |
| 94 |
|
* Injects properties into an instance |
| 95 |
|
* |
| 96 |
|
* @param object $instance |
| 97 |
|
* @param \Xyster\Container\IContainer $container |
| 98 |
|
*/ |
| 99 |
|
public function injectProperties($instance, \Xyster\Container\IContainer $container) |
| 100 |
|
{ |
| 101 |
13 |
foreach( $this->_properties as $name => $propertyValue ) { |
| 102 |
1 |
$prop = \Xyster\Type\Property\Factory::get($instance, $name); |
| 103 |
|
// @todo wrap this exception if it occurs? |
| 104 |
1 |
$prop->set($instance, $propertyValue); |
| 105 |
13 |
} |
| 106 |
13 |
foreach( $this->_dependsOn as $name => $component ) { |
| 107 |
7 |
$this->_injectByNameFromContainer($container, $instance, $name, $component); |
| 108 |
12 |
} |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
/** |
| 112 |
|
* Injects a component from the container into the instance by name. |
| 113 |
|
* |
| 114 |
|
* @param $container The container |
| 115 |
|
* @param $instance The object instance |
| 116 |
|
* @param $name The property name on the object |
| 117 |
|
* @param $component The name of the component in the container |
| 118 |
|
*/ |
| 119 |
|
protected function _injectByNameFromContainer(\Xyster\Container\IContainer $container, $instance, $name, $component) |
| 120 |
|
{ |
| 121 |
7 |
if ( $container->contains($component) ) { |
| 122 |
6 |
$propertyValue = $container->get($component); |
| 123 |
6 |
$prop = \Xyster\Type\Property\Factory::get($instance, $name); |
| 124 |
|
// @todo wrap this exception if it occurs? |
| 125 |
6 |
$prop->set($instance, $propertyValue); |
| 126 |
6 |
} else { |
| 127 |
1 |
require_once 'Xyster/Container/Injector/Exception.php'; |
| 128 |
1 |
throw new Exception( |
| 129 |
1 |
'Cannot inject property ' . $name . ' into ' . |
| 130 |
1 |
get_class($instance) . |
| 131 |
1 |
': key not found in the container: ' . $component); |
| 132 |
|
} |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
/** |
| 136 |
|
* Instantiate an object with given parameters |
| 137 |
|
* |
| 138 |
|
* @param \Xyster\Type\Type $type the class to construct |
| 139 |
|
* @param \Xyster\Container\IContainer the container |
| 140 |
|
* @return object the new object |
| 141 |
|
*/ |
| 142 |
|
protected function _newInstance(\Xyster\Type\Type $type, \Xyster\Container\IContainer $container) |
| 143 |
|
{ |
| 144 |
22 |
$class = $type->getClass(); |
| 145 |
22 |
$constructor = $class ? $class->getConstructor() : null; |
| 146 |
22 |
$parameters = $this->getMemberArguments($container, $constructor); |
| 147 |
|
return $constructor ? |
| 148 |
17 |
$class->newInstanceArgs($parameters) : $class->newInstance(); |
| 149 |
|
} |
| 150 |
1 |
} |