| 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 |
|
class Standard extends AbstractInjector |
| 26 |
1 |
{ |
| 27 |
|
/** |
| 28 |
|
* Get an instance of the provided component. |
| 29 |
|
* |
| 30 |
|
* @param \Xyster\Container\IContainer $container The container (used for dependency resolution) |
| 31 |
|
* @param \Xyster\Type\Type $into Optional. The type into which this component will be injected |
| 32 |
|
* @return mixed The component |
| 33 |
|
*/ |
| 34 |
|
public function get(\Xyster\Container\IContainer $container, \Xyster\Type\Type $into = null) |
| 35 |
|
{ |
| 36 |
22 |
$type = $this->getType(); |
| 37 |
|
// instantiate a copy of the type |
| 38 |
22 |
$instance = $this->_newInstance($type, $container); |
| 39 |
|
// inject literal and referenced properties |
| 40 |
17 |
$this->injectProperties($instance, $container); |
| 41 |
|
// inject container if necessary |
| 42 |
15 |
if ( $instance instanceof \Xyster\Container\IContainerAware ) { |
| 43 |
3 |
$instance->setContainer($container); |
| 44 |
3 |
} |
| 45 |
|
// call init method if necessary |
| 46 |
15 |
if ( $method = $this->_initMethod ) { |
| 47 |
1 |
$instance->$method(); |
| 48 |
1 |
} |
| 49 |
15 |
return $instance; |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
/** |
| 53 |
|
* Gets the label for the type of provider. |
| 54 |
|
* |
| 55 |
|
* @return string The provider label |
| 56 |
|
*/ |
| 57 |
|
public function getLabel() |
| 58 |
|
{ |
| 59 |
5 |
return 'Injector'; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
/** |
| 63 |
|
* Verify that all dependencies for this component can be satisifed. |
| 64 |
|
* |
| 65 |
|
* @param \Xyster\Container\IContainer $container The container |
| 66 |
|
* @throws \Xyster\Container\Exception if one or more required dependencies aren't met |
| 67 |
|
*/ |
| 68 |
|
public function validate(\Xyster\Container\IContainer $container) |
| 69 |
|
{ |
| 70 |
5 |
$reflectionClass = $this->getType()->getClass(); |
| 71 |
5 |
$member = ($reflectionClass) ? $reflectionClass->getConstructor() : null; |
| 72 |
|
/* @var $member \ReflectionMethod */ |
| 73 |
5 |
if ( $member != null ) { |
| 74 |
3 |
$types = \Xyster\Type\Type::getForParameters($member); |
| 75 |
3 |
foreach( $member->getParameters() as $k => $reflectionParameter ) { |
| 76 |
|
/* @var $reflectionParameter \ReflectionParameter */ |
| 77 |
3 |
$paramType = $types[$k]; |
| 78 |
|
/* @var $paramType \Xyster\Type\Type */ |
| 79 |
3 |
$argument = isset($this->_constructorArguments[$k]) ? |
| 80 |
3 |
$this->_constructorArguments[$k] : null; |
| 81 |
3 |
if ( !$paramType->isInstance($argument) && |
| 82 |
3 |
!$container->contains($argument) && |
| 83 |
3 |
!$reflectionParameter->isOptional() ) { |
| 84 |
1 |
throw new Exception('Component not found in the container: ' . $argument); |
| 85 |
|
} |
| 86 |
2 |
} |
| 87 |
2 |
} |
| 88 |
4 |
foreach( $this->_dependsOn as $k => $v ) { |
| 89 |
2 |
if ( !$container->contains($v) ) { |
| 90 |
1 |
throw new Exception('Component not found in the container: ' . $v); |
| 91 |
|
} |
| 92 |
3 |
} |
| 93 |
|
} |
| 94 |
1 |
} |