| 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; |
| 17 |
|
use Xyster\Type\Type; |
| 18 |
|
/** |
| 19 |
|
* Component definition class |
| 20 |
|
* |
| 21 |
|
* @category Xyster |
| 22 |
|
* @package Xyster_Container |
| 23 |
|
* @copyright Copyright LibreWorks, LLC (http://libreworks.net) |
| 24 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 25 |
|
*/ |
| 26 |
|
class Definition |
| 27 |
|
{ |
| 28 |
|
protected $_name; |
| 29 |
|
protected $_type; |
| 30 |
|
protected $_initMethod; |
| 31 |
|
protected $_constructorArguments = array(); |
| 32 |
|
protected $_depends = array(); |
| 33 |
|
protected $_properties = array(); |
| 34 |
|
|
| 35 |
|
/** |
| 36 |
|
* Creates a new definition |
| 37 |
|
* |
| 38 |
|
* @param mixed $type A Type or the name of a class |
| 39 |
|
* @param string $name Optional. The component name. |
| 40 |
|
*/ |
| 41 |
|
public function __construct($type, $name = null) |
| 42 |
|
{ |
| 43 |
59 |
$this->_type = ( $type instanceof Type ) ? |
| 44 |
59 |
$type : new Type($type); |
| 45 |
59 |
$this->_name = !$name ? $this->_type->getName() : $name; |
| 46 |
|
} |
| 47 |
|
|
| 48 |
|
/** |
| 49 |
|
* Adds a constructor argument. |
| 50 |
|
* |
| 51 |
|
* Call this method multiple times for several constructor arguments. The |
| 52 |
|
* value argument can either be a literal value or the name of another |
| 53 |
|
* component in the container. |
| 54 |
|
* |
| 55 |
|
* @param mixed $value The argument value |
| 56 |
|
* @return Definition provides a fluent interface |
| 57 |
|
*/ |
| 58 |
|
public function constructorArg($value) |
| 59 |
|
{ |
| 60 |
14 |
$this->_constructorArguments[] = $value; |
| 61 |
14 |
return $this; |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
/** |
| 65 |
|
* Adds a property dependency to be injected. |
| 66 |
|
* |
| 67 |
|
* As opposed to {@link property()}, this method is used for class |
| 68 |
|
* properties that should be resolved from the container. |
| 69 |
|
* |
| 70 |
|
* @param string $name The property name |
| 71 |
|
* @param string $value The name of the referenced component in the container |
| 72 |
|
* @return Definition provides a fluent interface |
| 73 |
|
*/ |
| 74 |
|
public function dependsOn($name, $value) |
| 75 |
|
{ |
| 76 |
14 |
$this->_depends[$name] = $value; |
| 77 |
14 |
return $this; |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
/** |
| 81 |
|
* Gets the constructor arguments |
| 82 |
|
* |
| 83 |
|
* @return array |
| 84 |
|
*/ |
| 85 |
|
public function getConstructorArgs() |
| 86 |
|
{ |
| 87 |
52 |
return $this->_constructorArguments; |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
/** |
| 91 |
|
* Gets the referenced properties |
| 92 |
|
* |
| 93 |
|
* @return array |
| 94 |
|
*/ |
| 95 |
|
public function getDependsOn() |
| 96 |
|
{ |
| 97 |
52 |
return $this->_depends; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
/** |
| 101 |
|
* Gets the initialization method |
| 102 |
|
* |
| 103 |
|
* @return string |
| 104 |
|
*/ |
| 105 |
|
public function getInitMethod() |
| 106 |
|
{ |
| 107 |
52 |
return $this->_initMethod; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
/** |
| 111 |
|
* Gets the name |
| 112 |
|
* |
| 113 |
|
* @return string |
| 114 |
|
*/ |
| 115 |
|
public function getName() |
| 116 |
|
{ |
| 117 |
52 |
return $this->_name; |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
/** |
| 121 |
|
* Gets the literal properties |
| 122 |
|
|
| 123 |
|
* @return array |
| 124 |
|
*/ |
| 125 |
|
public function getProperties() |
| 126 |
|
{ |
| 127 |
52 |
return $this->_properties; |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
/** |
| 131 |
|
* Gets the type |
| 132 |
|
* |
| 133 |
|
* @return Type |
| 134 |
|
*/ |
| 135 |
|
public function getType() |
| 136 |
|
{ |
| 137 |
52 |
return $this->_type; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
/** |
| 141 |
|
* Sets the name of the method to be invoked when an object has been created |
| 142 |
|
* |
| 143 |
|
* @param string $name The method name |
| 144 |
|
* @return Definition provides a fluent interface |
| 145 |
|
* @throws ContainerException if the method wasn't found |
| 146 |
|
*/ |
| 147 |
|
public function initMethod($name) |
| 148 |
|
{ |
| 149 |
3 |
if (!$this->_type->getClass()->hasMethod($name) && |
| 150 |
3 |
!method_exists($this->_type->getName(), '__call')) { |
| 151 |
1 |
throw new ContainerException('Method not found: ' . |
| 152 |
1 |
$this->_type->getName() . '::' . $name); |
| 153 |
|
} |
| 154 |
2 |
$this->_initMethod = $name; |
| 155 |
2 |
return $this; |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
/** |
| 159 |
|
* Adds a literal property to be injected. |
| 160 |
|
* |
| 161 |
|
* The value argument must be a literal value (string, boolean, array, etc.) |
| 162 |
|
* |
| 163 |
|
* @param string $name The property name |
| 164 |
|
* @param mixed $value The property value |
| 165 |
|
* @return Definition provides a fluent interface |
| 166 |
|
*/ |
| 167 |
|
public function property($name, $value) |
| 168 |
|
{ |
| 169 |
11 |
$this->_properties[$name] = $value; |
| 170 |
11 |
return $this; |
| 171 |
|
} |
| 172 |
1 |
} |