Ejemplo n.º 1
0
 private function dumpMethods($className, ClassDefinition $definition)
 {
     $str = '';
     foreach ($definition->getMethodInjections() as $methodInjection) {
         $parameters = $this->dumpMethodParameters($className, $methodInjection);
         $str .= sprintf("\n    %s(\n        %s\n    )", $methodInjection->getMethodName(), $parameters);
     }
     return $str;
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function getDefinition($name, MergeableDefinition $parentDefinition = null)
 {
     // Only merges with class definition
     if ($parentDefinition && !$parentDefinition instanceof ClassDefinition) {
         return null;
     }
     $className = $parentDefinition ? $parentDefinition->getClassName() : $name;
     if (!class_exists($className) && !interface_exists($className)) {
         return null;
     }
     $class = new ReflectionClass($className);
     $definition = new ClassDefinition($name);
     // Constructor
     $constructor = $class->getConstructor();
     if ($constructor && $constructor->isPublic()) {
         $definition->setConstructorInjection($this->getConstructorInjection($constructor));
     }
     // Merge with parent
     if ($parentDefinition) {
         $definition = $parentDefinition->merge($definition);
     }
     return $definition;
 }
Ejemplo n.º 3
0
 /**
  * Fixes parameters indexed by the parameter name -> reindex by position.
  *
  * This is necessary so that merging definitions between sources is possible.
  *
  * @param ClassDefinition $definition
  * @param string          $method
  * @param array           $parameters
  * @return array
  */
 private function fixParameters(ClassDefinition $definition, $method, $parameters)
 {
     $fixedParameters = array();
     foreach ($parameters as $index => $parameter) {
         // Parameter indexed by the parameter name, we reindex it with its position
         if (is_string($index)) {
             $callable = array($definition->getClassName(), $method);
             $reflectionParameter = new \ReflectionParameter($callable, $index);
             $index = $reflectionParameter->getPosition();
         }
         $fixedParameters[$index] = $parameter;
     }
     return $fixedParameters;
 }
Ejemplo n.º 4
0
 /**
  * @param ClassDefinition $definition
  * @param object          $instance
  * @param ClassDefinition $classDefinition
  */
 private function injectMethodsAndProperties(ClassDefinition $definition, $instance, ClassDefinition $classDefinition)
 {
     // Property injections
     foreach ($classDefinition->getPropertyInjections() as $propertyInjection) {
         $this->injectProperty($instance, $propertyInjection);
     }
     // Method injections
     foreach ($classDefinition->getMethodInjections() as $methodInjection) {
         $this->injectMethod($definition, $instance, $methodInjection);
     }
 }
Ejemplo n.º 5
0
 private function assertClassExists(ClassDefinition $classDefinition)
 {
     if (!class_exists($classDefinition->getClassName()) && !interface_exists($classDefinition->getClassName())) {
         throw DefinitionException::create($classDefinition, sprintf("Entry %s cannot be resolved: class %s doesn't exist", $classDefinition->getName(), $classDefinition->getClassName()));
     }
 }
Ejemplo n.º 6
0
 /**
  * Browse the object's methods looking for annotated methods.
  *
  * @param ReflectionClass $class
  * @param ClassDefinition $classDefinition
  */
 private function readMethods(ReflectionClass $class, ClassDefinition $classDefinition)
 {
     // This will look in all the methods, including those of the parent classes
     foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
         if ($method->isStatic()) {
             continue;
         }
         $methodInjection = $this->getMethodInjection($method);
         if (!$methodInjection) {
             continue;
         }
         if ($method->isConstructor()) {
             $classDefinition->setConstructorInjection($methodInjection);
         } else {
             $classDefinition->addMethodInjection($methodInjection);
         }
     }
 }
Ejemplo n.º 7
0
 private function readInjectableAnnotation(ReflectionClass $class, ClassDefinition $definition)
 {
     try {
         /** @var $annotation Injectable|null */
         $annotation = $this->getAnnotationReader()->getClassAnnotation($class, 'DI\\Annotation\\Injectable');
     } catch (UnexpectedValueException $e) {
         throw new DefinitionException(sprintf('Error while reading @Injectable on %s: %s', $class->getName(), $e->getMessage()), 0, $e);
     }
     if (!$annotation) {
         return;
     }
     if ($annotation->getScope()) {
         $definition->setScope($annotation->getScope());
     }
     if ($annotation->isLazy() !== null) {
         $definition->setLazy($annotation->isLazy());
     }
 }