1
0
Fork 0
php-language-server/src/Index/AbstractAggregateIndex.php

177 lines
4.5 KiB
PHP
Raw Normal View History

2016-12-13 00:51:02 +00:00
<?php
declare(strict_types = 1);
namespace LanguageServer\Index;
use LanguageServer\Definition;
2017-01-25 00:38:11 +00:00
use Sabre\Event\EmitterTrait;
2016-12-13 00:51:02 +00:00
abstract class AbstractAggregateIndex implements ReadableIndex
{
2017-01-25 00:38:11 +00:00
use EmitterTrait;
2016-12-13 00:51:02 +00:00
/**
* Returns all indexes managed by the aggregate index
*
* @return ReadableIndex[]
*/
abstract protected function getIndexes(): array;
2017-01-25 00:38:11 +00:00
public function __construct()
{
foreach ($this->getIndexes() as $index) {
$this->registerIndex($index);
}
}
/**
* @param ReadableIndex $index
*/
protected function registerIndex(ReadableIndex $index)
{
$index->on('complete', function () {
if ($this->isComplete()) {
$this->emit('complete');
}
});
$index->on('static-complete', function () {
if ($this->isStaticComplete()) {
$this->emit('static-complete');
}
});
$index->on('definition-added', function () {
$this->emit('definition-added');
});
}
/**
* Marks this index as complete
*
* @return void
*/
public function setComplete()
{
foreach ($this->getIndexes() as $index) {
$index->setComplete();
}
}
/**
* Marks this index as complete for static definitions and references
*
* @return void
*/
public function setStaticComplete()
{
foreach ($this->getIndexes() as $index) {
$index->setStaticComplete();
}
}
/**
* Returns true if this index is complete
*
* @return bool
*/
public function isComplete(): bool
{
foreach ($this->getIndexes() as $index) {
if (!$index->isComplete()) {
return false;
}
}
return true;
}
/**
* Returns true if this index is complete for static definitions or references
*
* @return bool
*/
public function isStaticComplete(): bool
{
foreach ($this->getIndexes() as $index) {
if (!$index->isStaticComplete()) {
return false;
}
}
return true;
}
2016-12-13 00:51:02 +00:00
/**
* Returns a Generator providing an associative array [string => Definition]
* that maps fully qualified symbol names to Definitions (global or not)
2016-12-13 00:51:02 +00:00
*
* @return \Generator providing Definition[]
2016-12-13 00:51:02 +00:00
*/
public function getDefinitions(): \Generator
2016-12-13 00:51:02 +00:00
{
foreach ($this->getIndexes() as $index) {
foreach ($index->getDefinitions() as $fqn => $definitions) {
yield $fqn => $definition;
}
}
}
/**
* 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 \Generator providing Definitions[]
*/
public function getDefinitionsForNamespace(string $namespace): \Generator
{
foreach ($this->getIndexes() as $index) {
foreach ($index->getDefinitionsForNamespace($namespace) as $fqn => $definition) {
yield $fqn => $definition;
2016-12-13 00:51:02 +00:00
}
}
}
/**
* Returns the Definition object by a specific FQN
*
* @param string $fqn
* @param bool $globalFallback Whether to fallback to global if the namespaced FQN was not found
* @return Definition|null
*/
public function getDefinition(string $fqn, bool $globalFallback = false)
{
foreach ($this->getIndexes() as $index) {
if ($def = $index->getDefinition($fqn, $globalFallback)) {
return $def;
}
}
}
/**
2017-08-09 22:06:53 +00:00
* Returns a Generator providing all URIs in this index that reference a symbol
2016-12-13 00:51:02 +00:00
*
* @param string $fqn The fully qualified name of the symbol
2017-08-09 22:06:53 +00:00
* @return \Generator providing string[]
2016-12-13 00:51:02 +00:00
*/
2017-08-09 22:06:53 +00:00
public function getReferenceUris(string $fqn): \Generator
2016-12-13 00:51:02 +00:00
{
foreach ($this->getIndexes() as $index) {
2017-08-09 22:06:53 +00:00
foreach ($index->getReferenceUris($fqn) as $uri) {
yield $uri;
2016-12-13 00:51:02 +00:00
}
}
}
}