1
0
Fork 0

use Generators to get definitions without wasting memory

pull/451/head
Nicolas MURE 2017-08-09 22:51:42 +02:00
parent ca0caf1678
commit 8768b698d5
No known key found for this signature in database
GPG Key ID: E5B036F9145C4CAA
3 changed files with 57 additions and 44 deletions

View File

@ -107,42 +107,40 @@ abstract class AbstractAggregateIndex implements ReadableIndex
public function getDefinitions(): \Generator
{
foreach ($this->getIndexes() as $index) {
yield $index->getDefinitions();
foreach ($index->getDefinitions() as $fqn => $definitions) {
yield $fqn => $definition;
}
}
}
/**
* 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
{
$defs = [];
foreach ($this->getIndexes() as $index) {
foreach ($index->getGlobalDefinitions() as $fqn => $def) {
$defs[$fqn] = $def;
foreach ($index->getGlobalDefinitions() as $fqn => $definition) {
yield $fqn => $definition;
}
}
return $defs;
}
/**
* 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
{
$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;
}
/**

View File

@ -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]
: []
;
}
}

View File

@ -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