3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Di\Definition
;
12 use Zend\Di\Exception
;
15 * Class definitions based on a configuration array
17 class BuilderDefinition
implements DefinitionInterface
22 protected $defaultClassBuilder = 'Zend\Di\Definition\Builder\PhpClass';
25 * @var Builder\PhpClass[]
27 protected $classes = array();
30 * Create classes from array
32 * @param array $builderData
35 public function createClassesFromArray(array $builderData)
37 foreach ($builderData as $className => $classInfo) {
38 $class = new Builder\
PhpClass();
39 $class->setName($className);
40 foreach ($classInfo as $type => $typeData) {
41 switch (strtolower($type)) {
43 foreach ($typeData as $superType) {
44 $class->addSuperType($superType);
48 $class->setInstantiator($typeData);
52 foreach ($typeData as $injectionMethodName => $injectionMethodData) {
53 $injectionMethod = new Builder\
InjectionMethod();
54 $injectionMethod->setName($injectionMethodName);
55 foreach ($injectionMethodData as $parameterName => $parameterType) {
56 $parameterType = ($parameterType) ?
: null; // force empty string to null
57 $injectionMethod->addParameter($parameterName, $parameterType);
59 $class->addInjectionMethod($injectionMethod);
65 $this->addClass($class);
72 * @param Builder\PhpClass $phpClass
73 * @return BuilderDefinition
75 public function addClass(Builder\PhpClass
$phpClass)
77 $this->classes
[] = $phpClass;
83 * Create a class builder object using default class builder class
85 * This method is a factory that can be used in place of addClass().
87 * @param null|string $name Optional name of class to assign
88 * @return Builder\PhpClass
90 public function createClass($name = null)
92 $builderClass = $this->defaultClassBuilder
;
93 /* @var $class Builder\PhpClass */
94 $class = new $builderClass();
97 $class->setName($name);
100 $this->addClass($class);
106 * Set the class to use with {@link createClass()}
108 * @param string $class
109 * @return BuilderDefinition
111 public function setClassBuilder($class)
113 $this->defaultClassBuilder
= $class;
119 * Get the class used for {@link createClass()}
121 * This is primarily to allow developers to temporarily override
122 * the builder strategy.
126 public function getClassBuilder()
128 return $this->defaultClassBuilder
;
134 public function getClasses()
136 $classNames = array();
138 /* @var $class Builder\PhpClass */
139 foreach ($this->classes
as $class) {
140 $classNames[] = $class->getName();
149 public function hasClass($class)
151 foreach ($this->classes
as $classObj) {
152 if ($classObj->getName() === $class) {
161 * @param string $name
162 * @return bool|Builder\PhpClass
164 protected function getClass($name)
166 foreach ($this->classes
as $classObj) {
167 if ($classObj->getName() === $name) {
177 * @throws \Zend\Di\Exception\RuntimeException
179 public function getClassSupertypes($class)
181 $class = $this->getClass($class);
183 if ($class === false) {
184 throw new Exception\
RuntimeException('Cannot find class object in this builder definition.');
187 return $class->getSuperTypes();
192 * @throws \Zend\Di\Exception\RuntimeException
194 public function getInstantiator($class)
196 $class = $this->getClass($class);
197 if ($class === false) {
198 throw new Exception\
RuntimeException('Cannot find class object in this builder definition.');
201 return $class->getInstantiator();
206 * @throws \Zend\Di\Exception\RuntimeException
208 public function hasMethods($class)
210 /* @var $class \Zend\Di\Definition\Builder\PhpClass */
211 $class = $this->getClass($class);
212 if ($class === false) {
213 throw new Exception\
RuntimeException('Cannot find class object in this builder definition.');
216 return (count($class->getInjectionMethods()) > 0);
221 * @throws \Zend\Di\Exception\RuntimeException
223 public function getMethods($class)
225 $class = $this->getClass($class);
226 if ($class === false) {
227 throw new Exception\
RuntimeException('Cannot find class object in this builder definition.');
229 $methods = $class->getInjectionMethods();
230 $methodNames = array();
232 /* @var $methodObj Builder\InjectionMethod */
233 foreach ($methods as $methodObj) {
234 $methodNames[] = $methodObj->getName();
242 * @throws \Zend\Di\Exception\RuntimeException
244 public function hasMethod($class, $method)
246 $class = $this->getClass($class);
247 if ($class === false) {
248 throw new Exception\
RuntimeException('Cannot find class object in this builder definition.');
250 $methods = $class->getInjectionMethods();
252 /* @var $methodObj Builder\InjectionMethod */
253 foreach ($methods as $methodObj) {
254 if ($methodObj->getName() === $method) {
265 public function hasMethodParameters($class, $method)
267 $class = $this->getClass($class);
268 if ($class === false) {
271 $methods = $class->getInjectionMethods();
272 /* @var $methodObj Builder\InjectionMethod */
273 foreach ($methods as $methodObj) {
274 if ($methodObj->getName() === $method) {
275 $method = $methodObj;
278 if (!$method instanceof Builder\InjectionMethod
) {
282 /* @var $method Builder\InjectionMethod */
284 return (count($method->getParameters()) > 0);
289 * @throws \Zend\Di\Exception\RuntimeException
291 public function getMethodParameters($class, $method)
293 $class = $this->getClass($class);
295 if ($class === false) {
296 throw new Exception\
RuntimeException('Cannot find class object in this builder definition.');
299 $methods = $class->getInjectionMethods();
301 /* @var $methodObj Builder\InjectionMethod */
302 foreach ($methods as $methodObj) {
303 if ($methodObj->getName() === $method) {
304 $method = $methodObj;
308 if (!$method instanceof Builder\InjectionMethod
) {
309 throw new Exception\
RuntimeException('Cannot find method object for method ' . $method . ' in this builder definition.');
312 $methodParameters = array();
314 /* @var $method Builder\InjectionMethod */
315 foreach ($method->getParameters() as $name => $info) {
316 $methodParameters[$class->getName() . '::' . $method->getName() . ':' . $name] = $info;
319 return $methodParameters;