Repository navigation
Expand file tree
/
Copy pathChainTypeLocator.php
More file actions
63 lines (54 loc) · 1.82 KB
/
Copy pathChainTypeLocator.php
File metadata and controls
63 lines (54 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Phpactor\ReferenceFinder;
use Phpactor\ReferenceFinder\Exception\CouldNotLocateType;
use Phpactor\ReferenceFinder\Exception\UnsupportedDocument;
use Phpactor\TextDocument\ByteOffset;
use Phpactor\TextDocument\Location;
use Phpactor\TextDocument\TextDocument;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
final class ChainTypeLocator implements TypeLocator
{
/**
* @var TypeLocator[]
*/
private $locators = [];
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(array $locators, LoggerInterface $logger = null)
{
$this->logger = $logger ?: new NullLogger();
foreach ($locators as $locator) {
$this->add($locator);
}
}
public function locateType(TextDocument $document, ByteOffset $byteOffset): Location
{
$messages = [];
foreach ($this->locators as $locator) {
try {
return $locator->locateType($document, $byteOffset);
} catch (UnsupportedDocument $unsupported) {
$this->logger->debug(sprintf(
'Document is unsupported by "%s": %s',
get_class($locator),
$unsupported->getMessage()
));
$messages[] = $unsupported->getMessage();
} catch (CouldNotLocateType $exception) {
$this->logger->info(sprintf('Could not locate type ""%s"', $exception->getMessage()));
$messages[] = $exception->getMessage();
}
}
if ($messages) {
throw new CouldNotLocateType(implode(', ', $messages));
}
throw new CouldNotLocateType('No type locators are registered');
}
private function add(TypeLocator $locator): void
{
$this->locators[] = $locator;
}
}