/**
  * {@inheritdoc}
  */
 public function getDefinition($name, MergeableDefinition $parentDefinition = null)
 {
     if (!array_key_exists($name, $this->definitions)) {
         // Not found, we use the chain or return null
         if ($this->chainedSource) {
             return $this->chainedSource->getDefinition($name, $parentDefinition);
         }
         return null;
     }
     $definition = $this->definitions[$name];
     if ($definition instanceof DefinitionHelper) {
         $definition = $definition->getDefinition($name);
     }
     if (!$definition instanceof Definition) {
         $definition = new ValueDefinition($name, $definition);
     }
     // If the definition we have is not mergeable, and we are supposed to merge, we ignore it
     if ($parentDefinition && !$definition instanceof MergeableDefinition) {
         return $parentDefinition;
     }
     // Merge with parent
     if ($parentDefinition) {
         $definition = $parentDefinition->merge($definition);
     }
     // Enrich definition in sub-source
     if ($this->chainedSource && $definition instanceof MergeableDefinition) {
         $definition = $this->chainedSource->getDefinition($name, $definition);
     }
     return $definition;
 }
 /**
  * {@inheritdoc}
  */
 public function getDefinition($name)
 {
     // Look in cache
     $definition = $this->fetchFromCache($name);
     if ($definition === false) {
         $definition = $this->source->getDefinition($name);
         // Save to cache
         if ($definition === null || $definition instanceof CacheableDefinition) {
             $this->saveToCache($name, $definition);
         }
     }
     return $definition;
 }
Exemple #3
0
 private function resolveSubDefinition(HasSubDefinition $definition, $currentIndex)
 {
     $subDefinitionName = $definition->getSubDefinitionName();
     if ($subDefinitionName === $definition->getName()) {
         // Extending itself: look in the next sources only (else infinite recursion)
         $subDefinition = $this->getDefinition($subDefinitionName, $currentIndex + 1);
     } else {
         // Extending another definition: look from the root
         $subDefinition = $this->rootSource->getDefinition($subDefinitionName);
     }
     if ($subDefinition) {
         $definition->setSubDefinition($subDefinition);
     }
 }
Exemple #4
0
 /**
  * Inject all dependencies on an existing instance
  *
  * @param object $instance Object to perform injection upon
  * @throws InvalidArgumentException
  * @throws DependencyException Error while injecting dependencies
  * @return object $instance Returns the same instance
  */
 public function injectOn($instance)
 {
     $objectDefinition = $this->definitionSource->getDefinition(get_class($instance));
     if (!$objectDefinition instanceof ObjectDefinition) {
         return $instance;
     }
     $definition = new InstanceDefinition($instance, $objectDefinition);
     $this->definitionResolver->resolve($definition);
     return $instance;
 }
 /**
  * {@inheritdoc}
  */
 public function getDefinition($name, MergeableDefinition $parentDefinition = null)
 {
     $definition = $this->findDefinition($name);
     if ($definition === null) {
         // Not found, we use the chain or return null
         if ($this->chainedSource) {
             return $this->chainedSource->getDefinition($name, $parentDefinition);
         }
         return null;
     }
     // If the definition we have is not mergeable, and we are supposed to merge, we ignore it
     if ($parentDefinition && !$definition instanceof MergeableDefinition) {
         return $parentDefinition;
     }
     // Merge with parent
     if ($parentDefinition) {
         $definition = $parentDefinition->merge($definition);
     }
     // Enrich definition in sub-source
     if ($this->chainedSource && $definition instanceof MergeableDefinition) {
         $definition = $this->chainedSource->getDefinition($name, $definition);
     }
     return $definition;
 }