diff --git a/src/Index/AbstractAggregateIndex.php b/src/Index/AbstractAggregateIndex.php index ca4b7d4..a5826ab 100644 --- a/src/Index/AbstractAggregateIndex.php +++ b/src/Index/AbstractAggregateIndex.php @@ -107,42 +107,40 @@ abstract class AbstractAggregateIndex implements ReadableIndex public function getDefinitions(): \Generator { foreach ($this->getIndexes() as $index) { - yield $index->getDefinitions(); - } - } - - /** - * Returns an associative array [string => Definition] that maps fully qualified symbol names - * to global Definitions - * - * @return Definition[] - */ - public function getGlobalDefinitions(): array - { - $defs = []; - foreach ($this->getIndexes() as $index) { - foreach ($index->getGlobalDefinitions() as $fqn => $def) { - $defs[$fqn] = $def; + foreach ($index->getDefinitions() as $fqn => $definitions) { + yield $fqn => $definition; } } - return $defs; } /** - * Returns the Definitions that are in the given namespace + * Returns a Generator providing an associative array [string => Definition] + * that maps fully qualified symbol names to global Definitions + * + * @return \Generator providing Definitions[] + */ + public function getGlobalDefinitions(): \Generator + { + foreach ($this->getIndexes() as $index) { + foreach ($index->getGlobalDefinitions() as $fqn => $definition) { + yield $fqn => $definition; + } + } + } + + /** + * Returns a Generator providing the Definitions that are in the given namespace * * @param string $namespace - * @return Definitions[] + * @return \Generator providing Definitions[] */ - public function getDefinitionsForNamespace(string $namespace): array + public function getDefinitionsForNamespace(string $namespace): \Generator { - $defs = []; foreach ($this->getIndexes() as $index) { - foreach ($index->getDefinitionsForNamespace($namespace) as $fqn => $def) { - $defs[$fqn] = $def; + foreach ($index->getDefinitionsForNamespace($namespace) as $fqn => $definition) { + yield $fqn => $definition; } } - return $defs; } /** diff --git a/src/Index/Index.php b/src/Index/Index.php index ffd5941..9130246 100644 --- a/src/Index/Index.php +++ b/src/Index/Index.php @@ -107,28 +107,29 @@ class Index implements ReadableIndex, \Serializable } /** - * Returns an associative array [string => Definition] that maps fully qualified symbol names - * to global Definitions + * Returns a Generator providing an associative array [string => Definition] + * that maps fully qualified symbol names to global Definitions * - * @return Definition[] + * @return \Generator providing Definitions[] */ - public function getGlobalDefinitions(): array + public function getGlobalDefinitions(): \Generator { - return $this->globalDefinitions; + foreach ($this->globalDefinitions as $fqn => $definition) { + yield $fqn => $definition; + } } /** - * Returns the Definitions that are in the given namespace + * Returns a Generator providing the Definitions that are in the given namespace * * @param string $namespace - * @return Definitions[] + * @return \Generator providing Definitions[] */ - public function getDefinitionsForNamespace(string $namespace): array + public function getDefinitionsForNamespace(string $namespace): \Generator { - return isset($this->namespaceDefinitions[$namespace]) - ? $this->namespaceDefinitions[$namespace] - : [] - ; + foreach ($this->doGetDefinitionsForNamespace($namespace) as $fqn => $definition) { + yield $fqn => $definition; + } } /** @@ -141,7 +142,7 @@ class Index implements ReadableIndex, \Serializable public function getDefinition(string $fqn, bool $globalFallback = false) { $namespace = $this->extractNamespace($fqn); - $definitions = $this->getDefinitionsForNamespace($namespace); + $definitions = $this->doGetDefinitionsForNamespace($namespace); if (isset($definitions[$fqn])) { return $definitions[$fqn]; @@ -314,4 +315,18 @@ class Index implements ReadableIndex, \Serializable return $fqn; } + + /** + * Returns the Definitions that are in the given namespace + * + * @param string $namespace + * @return Definition[] + */ + private function doGetDefinitionsForNamespace(string $namespace): array + { + return isset($this->namespaceDefinitions[$namespace]) + ? $this->namespaceDefinitions[$namespace] + : [] + ; + } } diff --git a/src/Index/ReadableIndex.php b/src/Index/ReadableIndex.php index 0c5efca..8d4e1ca 100644 --- a/src/Index/ReadableIndex.php +++ b/src/Index/ReadableIndex.php @@ -38,20 +38,20 @@ interface ReadableIndex extends EmitterInterface public function getDefinitions(): \Generator; /** - * Returns an associative array [string => Definition] that maps fully qualified symbol names - * to global Definitions + * Returns a Generator providing an associative array [string => Definition] + * that maps fully qualified symbol names to global Definitions * - * @return Definitions[] + * @return \Generator providing Definitions[] */ - public function getGlobalDefinitions(): array; + public function getGlobalDefinitions(): \Generator; /** - * Returns the Definitions that are in the given namespace + * Returns a Generator providing the Definitions that are in the given namespace * * @param string $namespace - * @return Definitions[] + * @return \Generator providing Definitions[] */ - public function getDefinitionsForNamespace(string $namespace): array; + public function getDefinitionsForNamespace(string $namespace): \Generator; /** * Returns the Definition object by a specific FQN