Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Di / Definition / BuilderDefinition.php
blob8191e96dfbf09e7e4a7297c89e0829e9e4cb1611
1 <?php
2 /**
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
8 */
10 namespace Zend\Di\Definition;
12 use Zend\Di\Exception;
14 /**
15 * Class definitions based on a configuration array
17 class BuilderDefinition implements DefinitionInterface
19 /**
20 * @var string
22 protected $defaultClassBuilder = 'Zend\Di\Definition\Builder\PhpClass';
24 /**
25 * @var Builder\PhpClass[]
27 protected $classes = array();
29 /**
30 * Create classes from array
32 * @param array $builderData
33 * @return void
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)) {
42 case 'supertypes':
43 foreach ($typeData as $superType) {
44 $class->addSuperType($superType);
46 break;
47 case 'instantiator':
48 $class->setInstantiator($typeData);
49 break;
50 case 'methods':
51 case 'method':
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);
61 break;
65 $this->addClass($class);
69 /**
70 * Add class
72 * @param Builder\PhpClass $phpClass
73 * @return BuilderDefinition
75 public function addClass(Builder\PhpClass $phpClass)
77 $this->classes[] = $phpClass;
79 return $this;
82 /**
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();
96 if (null !== $name) {
97 $class->setName($name);
100 $this->addClass($class);
102 return $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;
115 return $this;
119 * Get the class used for {@link createClass()}
121 * This is primarily to allow developers to temporarily override
122 * the builder strategy.
124 * @return string
126 public function getClassBuilder()
128 return $this->defaultClassBuilder;
132 * {@inheritDoc}
134 public function getClasses()
136 $classNames = array();
138 /* @var $class Builder\PhpClass */
139 foreach ($this->classes as $class) {
140 $classNames[] = $class->getName();
143 return $classNames;
147 * {@inheritDoc}
149 public function hasClass($class)
151 foreach ($this->classes as $classObj) {
152 if ($classObj->getName() === $class) {
153 return true;
157 return false;
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) {
168 return $classObj;
172 return false;
176 * {@inheritDoc}
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();
191 * {@inheritDoc}
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();
205 * {@inheritDoc}
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);
220 * {@inheritDoc}
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();
237 return $methodNames;
241 * {@inheritDoc}
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) {
255 return true;
259 return false;
263 * {@inheritDoc}
265 public function hasMethodParameters($class, $method)
267 $class = $this->getClass($class);
268 if ($class === false) {
269 return 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) {
279 return false;
282 /* @var $method Builder\InjectionMethod */
284 return (count($method->getParameters()) > 0);
288 * {@inheritDoc}
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;