1
0
Fork 0

store definitions under the namespaceDefinitions cache key directly

pull/451/head
Nicolas MURE 2017-08-09 22:10:13 +02:00
parent 6d30035c78
commit ca0caf1678
No known key found for this signature in database
GPG Key ID: E5B036F9145C4CAA
3 changed files with 48 additions and 72 deletions

View File

@ -99,20 +99,16 @@ abstract class AbstractAggregateIndex implements ReadableIndex
} }
/** /**
* Returns an associative array [string => Definition] that maps fully qualified symbol names * Returns a Generator providing an associative array [string => Definition]
* to Definitions (global or not) * 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 ($this->getIndexes() as $index) {
foreach ($index->getDefinitions() as $fqn => $def) { yield $index->getDefinitions();
$defs[$fqn] = $def;
}
} }
return $defs;
} }
/** /**

View File

@ -15,11 +15,12 @@ class Index implements ReadableIndex, \Serializable
use EmitterTrait; 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 * An associative array that maps fully qualified symbol names to global Definitions
@ -28,13 +29,6 @@ class Index implements ReadableIndex, \Serializable
*/ */
private $globalDefinitions = []; 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 * 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 * Returns a Generator providing an associative array [string => Definition]
* to Definitions (global or not) * 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) public function getDefinition(string $fqn, bool $globalFallback = false)
{ {
if (isset($this->definitions[$fqn])) { $namespace = $this->extractNamespace($fqn);
return $this->definitions[$fqn]; $definitions = $this->getDefinitionsForNamespace($namespace);
if (isset($definitions[$fqn])) {
return $definitions[$fqn];
} }
if ($globalFallback) { if ($globalFallback) {
$parts = explode('\\', $fqn); $parts = explode('\\', $fqn);
$fqn = end($parts); $fqn = end($parts);
@ -161,9 +163,13 @@ class Index implements ReadableIndex, \Serializable
*/ */
public function setDefinition(string $fqn, Definition $definition) 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->setGlobalDefinition($fqn, $definition);
$this->setNamespaceDefinition($fqn, $definition);
$this->emit('definition-added'); $this->emit('definition-added');
} }
@ -176,10 +182,17 @@ class Index implements ReadableIndex, \Serializable
*/ */
public function removeDefinition(string $fqn) 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->globalDefinitions[$fqn]);
unset($this->references[$fqn]); unset($this->references[$fqn]);
$this->removeNamespaceDefinition($fqn);
} }
/** /**
@ -252,9 +265,10 @@ class Index implements ReadableIndex, \Serializable
$this->$prop = $val; $this->$prop = $val;
} }
foreach ($this->definitions as $fqn => $definition) { foreach ($this->namespaceDefinitions as $namespaceDefinition) {
$this->setGlobalDefinition($fqn, $definition); foreach ($namespaceDefinition as $fqn => $definition) {
$this->setNamespaceDefinition($fqn, $definition); $this->setGlobalDefinition($fqn, $definition);
}
} }
} }
@ -265,7 +279,7 @@ class Index implements ReadableIndex, \Serializable
public function serialize() public function serialize()
{ {
return serialize([ return serialize([
'definitions' => $this->definitions, 'namespaceDefinitions' => $this->namespaceDefinitions,
'references' => $this->references, 'references' => $this->references,
'complete' => $this->complete, 'complete' => $this->complete,
'staticComplete' => $this->staticComplete '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 * @param string $fqn
* @return string The namespace extracted from the given FQN * @return string The namespace extracted from the given FQN

View File

@ -30,12 +30,12 @@ interface ReadableIndex extends EmitterInterface
public function isStaticComplete(): bool; public function isStaticComplete(): bool;
/** /**
* Returns an associative array [string => Definition] that maps fully qualified symbol names * Returns a Generator providing an associative array [string => Definition]
* to Definitions (global or not) * 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 * Returns an associative array [string => Definition] that maps fully qualified symbol names