diff --git a/src/Index/AbstractAggregateIndex.php b/src/Index/AbstractAggregateIndex.php index 038117a..ca4b7d4 100644 --- a/src/Index/AbstractAggregateIndex.php +++ b/src/Index/AbstractAggregateIndex.php @@ -99,20 +99,16 @@ abstract class AbstractAggregateIndex implements ReadableIndex } /** - * Returns an associative array [string => Definition] that maps fully qualified symbol names - * to Definitions (global or not) + * Returns a Generator providing an associative array [string => Definition] + * that maps fully qualified symbol names to Definitions (global or not) * - * @return Definition[] + * @return \Generator providing Definition[] */ - public function getDefinitions(): array + public function getDefinitions(): \Generator { - $defs = []; foreach ($this->getIndexes() as $index) { - foreach ($index->getDefinitions() as $fqn => $def) { - $defs[$fqn] = $def; - } + yield $index->getDefinitions(); } - return $defs; } /** diff --git a/src/Index/Index.php b/src/Index/Index.php index bf03622..ffd5941 100644 --- a/src/Index/Index.php +++ b/src/Index/Index.php @@ -15,11 +15,12 @@ class Index implements ReadableIndex, \Serializable use EmitterTrait; /** - * An associative array that maps fully qualified symbol names to Definitions (global or not) + * An associative array that maps namespaces to + * an associative array that maps fully qualified symbol names to global Definitions * - * @var Definition[] + * @var array */ - private $definitions = []; + private $namespaceDefinitions = []; /** * An associative array that maps fully qualified symbol names to global Definitions @@ -28,13 +29,6 @@ class Index implements ReadableIndex, \Serializable */ private $globalDefinitions = []; - /** - * An associative array that maps namespaces to an associative array of FQN to Definitions - * - * @var Definition[] - */ - private $namespaceDefinitions = []; - /** * An associative array that maps fully qualified symbol names to arrays of document URIs that reference the symbol * @@ -98,14 +92,18 @@ class Index implements ReadableIndex, \Serializable } /** - * Returns an associative array [string => Definition] that maps fully qualified symbol names - * to Definitions (global or not) + * Returns a Generator providing an associative array [string => Definition] + * that maps fully qualified symbol names to Definitions (global or not) * - * @return Definition[] + * @return \Generator providing Definition[] */ - public function getDefinitions(): array + public function getDefinitions(): \Generator { - return $this->definitions; + foreach ($this->namespaceDefinitions as $namespaceDefinition) { + foreach ($namespaceDefinition as $fqn => $definition) { + yield $fqn => $definition; + } + } } /** @@ -142,9 +140,13 @@ class Index implements ReadableIndex, \Serializable */ public function getDefinition(string $fqn, bool $globalFallback = false) { - if (isset($this->definitions[$fqn])) { - return $this->definitions[$fqn]; + $namespace = $this->extractNamespace($fqn); + $definitions = $this->getDefinitionsForNamespace($namespace); + + if (isset($definitions[$fqn])) { + return $definitions[$fqn]; } + if ($globalFallback) { $parts = explode('\\', $fqn); $fqn = end($parts); @@ -161,9 +163,13 @@ class Index implements ReadableIndex, \Serializable */ public function setDefinition(string $fqn, Definition $definition) { - $this->definitions[$fqn] = $definition; + $namespace = $this->extractNamespace($fqn); + if (!isset($this->namespaceDefinitions[$namespace])) { + $this->namespaceDefinitions[$namespace] = []; + } + + $this->namespaceDefinitions[$namespace][$fqn] = $definition; $this->setGlobalDefinition($fqn, $definition); - $this->setNamespaceDefinition($fqn, $definition); $this->emit('definition-added'); } @@ -176,10 +182,17 @@ class Index implements ReadableIndex, \Serializable */ public function removeDefinition(string $fqn) { - unset($this->definitions[$fqn]); + $namespace = $this->extractNamespace($fqn); + if (isset($this->namespaceDefinitions[$namespace])) { + unset($this->namespaceDefinitions[$namespace][$fqn]); + + if (empty($this->namespaceDefinitions[$namespace])) { + unset($this->namespaceDefinitions[$namespace]); + } + } + unset($this->globalDefinitions[$fqn]); unset($this->references[$fqn]); - $this->removeNamespaceDefinition($fqn); } /** @@ -252,9 +265,10 @@ class Index implements ReadableIndex, \Serializable $this->$prop = $val; } - foreach ($this->definitions as $fqn => $definition) { - $this->setGlobalDefinition($fqn, $definition); - $this->setNamespaceDefinition($fqn, $definition); + foreach ($this->namespaceDefinitions as $namespaceDefinition) { + foreach ($namespaceDefinition as $fqn => $definition) { + $this->setGlobalDefinition($fqn, $definition); + } } } @@ -265,7 +279,7 @@ class Index implements ReadableIndex, \Serializable public function serialize() { return serialize([ - 'definitions' => $this->definitions, + 'namespaceDefinitions' => $this->namespaceDefinitions, 'references' => $this->references, 'complete' => $this->complete, 'staticComplete' => $this->staticComplete @@ -286,40 +300,6 @@ class Index implements ReadableIndex, \Serializable } } - /** - * Registers a definition to a namespace - * - * @param string $fqn The fully qualified name of the symbol - * @param Definition $definition The Definition object - * @return void - */ - private function setNamespaceDefinition(string $fqn, Definition $definition) - { - $namespace = $this->extractNamespace($fqn); - if (!isset($this->namespaceDefinitions[$namespace])) { - $this->namespaceDefinitions[$namespace] = []; - } - $this->namespaceDefinitions[$namespace][$fqn] = $definition; - } - - /** - * Removes a definition from a namespace - * - * @param string $fqn The fully qualified name of the symbol - * @return void - */ - private function removeNamespaceDefinition(string $fqn) - { - $namespace = $this->extractNamespace($fqn); - if (isset($this->namespaceDefinitions[$namespace])) { - unset($this->namespaceDefinitions[$namespace][$fqn]); - - if (0 === sizeof($this->namespaceDefinitions[$namespace])) { - unset($this->namespaceDefinitions[$namespace]); - } - } - } - /** * @param string $fqn * @return string The namespace extracted from the given FQN diff --git a/src/Index/ReadableIndex.php b/src/Index/ReadableIndex.php index 62b9c06..0c5efca 100644 --- a/src/Index/ReadableIndex.php +++ b/src/Index/ReadableIndex.php @@ -30,12 +30,12 @@ interface ReadableIndex extends EmitterInterface public function isStaticComplete(): bool; /** - * Returns an associative array [string => Definition] that maps fully qualified symbol names - * to Definitions (global or not) + * Returns a Generator providing an associative array [string => Definition] + * that maps fully qualified symbol names to Definitions (global or not) * - * @return Definitions[] + * @return \Generator providing Definition[] */ - public function getDefinitions(): array; + public function getDefinitions(): \Generator; /** * Returns an associative array [string => Definition] that maps fully qualified symbol names