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 public function getDefinitions(): \Generator
{ {
foreach ($this->getIndexes() as $index) { 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
*
* @return Definition[]
*/
public function getGlobalDefinitions(): array
{
$defs = [];
foreach ($this->getIndexes() as $index) {
foreach ($index->getGlobalDefinitions() as $fqn => $def) {
$defs[$fqn] = $def;
} }
} }
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 * @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 ($this->getIndexes() as $index) {
foreach ($index->getDefinitionsForNamespace($namespace) as $fqn => $def) { foreach ($index->getDefinitionsForNamespace($namespace) as $fqn => $definition) {
$defs[$fqn] = $def; 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 * Returns a Generator providing an associative array [string => Definition]
* to global Definitions * 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 * @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]) foreach ($this->doGetDefinitionsForNamespace($namespace) as $fqn => $definition) {
? $this->namespaceDefinitions[$namespace] yield $fqn => $definition;
: [] }
;
} }
/** /**
@ -141,7 +142,7 @@ class Index implements ReadableIndex, \Serializable
public function getDefinition(string $fqn, bool $globalFallback = false) public function getDefinition(string $fqn, bool $globalFallback = false)
{ {
$namespace = $this->extractNamespace($fqn); $namespace = $this->extractNamespace($fqn);
$definitions = $this->getDefinitionsForNamespace($namespace); $definitions = $this->doGetDefinitionsForNamespace($namespace);
if (isset($definitions[$fqn])) { if (isset($definitions[$fqn])) {
return $definitions[$fqn]; return $definitions[$fqn];
@ -314,4 +315,18 @@ class Index implements ReadableIndex, \Serializable
return $fqn; 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; public function getDefinitions(): \Generator;
/** /**
* Returns an associative array [string => Definition] that maps fully qualified symbol names * Returns a Generator providing an associative array [string => Definition]
* to global Definitions * 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 * @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 * Returns the Definition object by a specific FQN