Source of file BuilderDefinition.php
Size: 8,846 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Di/Definition/BuilderDefinition.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Di\Definition; use Zend\Di\Exception; /** * Class definitions based on a configuration array */ class BuilderDefinition implements DefinitionInterface { /** * @var string */ protected $defaultClassBuilder = 'Zend\Di\Definition\Builder\PhpClass'; /** * @var Builder\PhpClass[] */ protected $classes = array(); /** * Create classes from array * * @param array $builderData * @return void */ public function createClassesFromArray(array $builderData) { foreach ($builderData as $className => $classInfo) { $class = new Builder\PhpClass(); $class->setName($className); foreach ($classInfo as $type => $typeData) { switch (strtolower($type)) { case 'supertypes': foreach ($typeData as $superType) { $class->addSuperType($superType); } break; case 'instantiator': $class->setInstantiator($typeData); break; case 'methods': case 'method': foreach ($typeData as $injectionMethodName => $injectionMethodData) { $injectionMethod = new Builder\InjectionMethod(); $injectionMethod->setName($injectionMethodName); foreach ($injectionMethodData as $parameterName => $parameterType) { $parameterType = ($parameterType) ?: null; // force empty string to null $injectionMethod->addParameter($parameterName, $parameterType); } $class->addInjectionMethod($injectionMethod); } break; } } $this->addClass($class); } } /** * Add class * * @param Builder\PhpClass $phpClass * @return BuilderDefinition */ public function addClass(Builder\PhpClass $phpClass) { $this->classes[] = $phpClass; return $this; } /** * Create a class builder object using default class builder class * * This method is a factory that can be used in place of addClass(). * * @param null|string $name Optional name of class to assign * @return Builder\PhpClass */ public function createClass($name = null) { $builderClass = $this->defaultClassBuilder; /* @var $class Builder\PhpClass */ $class = new $builderClass(); if (null !== $name) { $class->setName($name); } $this->addClass($class); return $class; } /** * Set the class to use with {@link createClass()} * * @param string $class * @return BuilderDefinition */ public function setClassBuilder($class) { $this->defaultClassBuilder = $class; return $this; } /** * Get the class used for {@link createClass()} * * This is primarily to allow developers to temporarily override * the builder strategy. * * @return string */ public function getClassBuilder() { return $this->defaultClassBuilder; } /** * {@inheritDoc} */ public function getClasses() { $classNames = array(); /* @var $class Builder\PhpClass */ foreach ($this->classes as $class) { $classNames[] = $class->getName(); } return $classNames; } /** * {@inheritDoc} */ public function hasClass($class) { foreach ($this->classes as $classObj) { if ($classObj->getName() === $class) { return true; } } return false; } /** * @param string $name * @return bool|Builder\PhpClass */ protected function getClass($name) { foreach ($this->classes as $classObj) { if ($classObj->getName() === $name) { return $classObj; } } return false; } /** * {@inheritDoc} * @throws \Zend\Di\Exception\RuntimeException */ public function getClassSupertypes($class) { $class = $this->getClass($class); if ($class === false) { throw new Exception\RuntimeException('Cannot find class object in this builder definition.'); } return $class->getSuperTypes(); } /** * {@inheritDoc} * @throws \Zend\Di\Exception\RuntimeException */ public function getInstantiator($class) { $class = $this->getClass($class); if ($class === false) { throw new Exception\RuntimeException('Cannot find class object in this builder definition.'); } return $class->getInstantiator(); } /** * {@inheritDoc} * @throws \Zend\Di\Exception\RuntimeException */ public function hasMethods($class) { /* @var $class \Zend\Di\Definition\Builder\PhpClass */ $class = $this->getClass($class); if ($class === false) { throw new Exception\RuntimeException('Cannot find class object in this builder definition.'); } return (count($class->getInjectionMethods()) > 0); } /** * {@inheritDoc} * @throws \Zend\Di\Exception\RuntimeException */ public function getMethods($class) { $class = $this->getClass($class); if ($class === false) { throw new Exception\RuntimeException('Cannot find class object in this builder definition.'); } $methods = $class->getInjectionMethods(); $methodNames = array(); /* @var $methodObj Builder\InjectionMethod */ foreach ($methods as $methodObj) { $methodNames[] = $methodObj->getName(); } return $methodNames; } /** * {@inheritDoc} * @throws \Zend\Di\Exception\RuntimeException */ public function hasMethod($class, $method) { $class = $this->getClass($class); if ($class === false) { throw new Exception\RuntimeException('Cannot find class object in this builder definition.'); } $methods = $class->getInjectionMethods(); /* @var $methodObj Builder\InjectionMethod */ foreach ($methods as $methodObj) { if ($methodObj->getName() === $method) { return true; } } return false; } /** * {@inheritDoc} */ public function hasMethodParameters($class, $method) { $class = $this->getClass($class); if ($class === false) { return false; } $methods = $class->getInjectionMethods(); /* @var $methodObj Builder\InjectionMethod */ foreach ($methods as $methodObj) { if ($methodObj->getName() === $method) { $method = $methodObj; } } if (!$method instanceof Builder\InjectionMethod) { return false; } /* @var $method Builder\InjectionMethod */ return (count($method->getParameters()) > 0); } /** * {@inheritDoc} * @throws \Zend\Di\Exception\RuntimeException */ public function getMethodParameters($class, $method) { $class = $this->getClass($class); if ($class === false) { throw new Exception\RuntimeException('Cannot find class object in this builder definition.'); } $methods = $class->getInjectionMethods(); /* @var $methodObj Builder\InjectionMethod */ foreach ($methods as $methodObj) { if ($methodObj->getName() === $method) { $method = $methodObj; } } if (!$method instanceof Builder\InjectionMethod) { throw new Exception\RuntimeException('Cannot find method object for method ' . $method . ' in this builder definition.'); } $methodParameters = array(); /* @var $method Builder\InjectionMethod */ foreach ($method->getParameters() as $name => $info) { $methodParameters[$class->getName() . '::' . $method->getName() . ':' . $name] = $info; } return $methodParameters; } } |