1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PHPStan\BetterReflection\SourceLocator\Type; |
6: | |
7: | use Composer\Autoload\ClassLoader; |
8: | use InvalidArgumentException; |
9: | use PHPStan\BetterReflection\Identifier\Identifier; |
10: | use PHPStan\BetterReflection\Identifier\IdentifierType; |
11: | use PHPStan\BetterReflection\SourceLocator\Ast\Locator; |
12: | use PHPStan\BetterReflection\SourceLocator\Exception\InvalidFileLocation; |
13: | use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource; |
14: | |
15: | use function file_get_contents; |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | class ComposerSourceLocator extends AbstractSourceLocator |
25: | { |
26: | |
27: | |
28: | |
29: | private $classLoader; |
30: | public function __construct(ClassLoader $classLoader, Locator $astLocator) |
31: | { |
32: | $this->classLoader = $classLoader; |
33: | parent::__construct($astLocator); |
34: | } |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | protected function createLocatedSource(Identifier $identifier): ?\PHPStan\BetterReflection\SourceLocator\Located\LocatedSource |
43: | { |
44: | if ($identifier->getType()->getName() !== IdentifierType::IDENTIFIER_CLASS) { |
45: | return null; |
46: | } |
47: | |
48: | $filename = $this->classLoader->findFile($identifier->getName()); |
49: | |
50: | if (! $filename) { |
51: | return null; |
52: | } |
53: | |
54: | return new LocatedSource(file_get_contents($filename), $identifier->getName(), $filename); |
55: | } |
56: | } |
57: | |