/** * {@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; }
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); } }
/** * {@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; }
private function setDefinition($name, Definition $definition) { if ($this->definitionSource instanceof CachedDefinitionSource) { throw new \LogicException('You cannot set a definition at runtime on a container that has a cache configured. Doing so would risk caching the definition for the next execution, where it might be different. You can either put your definitions in a file, remove the cache or ->set() a raw value directly (PHP object, string, int, ...) instead of a PHP-DI definition.'); } if (!$this->definitionSource instanceof MutableDefinitionSource) { // This can happen if you instantiate the container yourself throw new \LogicException('The container has not been initialized correctly'); } // Clear existing entry if it exists if (array_key_exists($name, $this->singletonEntries)) { unset($this->singletonEntries[$name]); } $this->definitionSource->addDefinition($definition); }