|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace ComposerRequireChecker\DependencyGuesser; |
|
5
|
|
|
|
|
6
|
|
|
use Roave\BetterReflection\BetterReflection; |
|
7
|
|
|
use Roave\BetterReflection\Identifier\Identifier; |
|
8
|
|
|
use Roave\BetterReflection\Identifier\IdentifierType; |
|
9
|
|
|
use Roave\BetterReflection\Reflection\ReflectionClass; |
|
10
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
|
11
|
|
|
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\MakeLocatorForInstalledJson; |
|
12
|
|
|
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator; |
|
13
|
|
|
use Roave\BetterReflection\SourceLocator\Type\SourceLocator; |
|
14
|
|
|
|
|
15
|
|
|
final class GuessFromComposerInstalledJson implements GuesserInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var SourceLocator |
|
19
|
|
|
*/ |
|
20
|
|
|
private $sourceLocator; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ClassReflector |
|
24
|
|
|
*/ |
|
25
|
|
|
private $reflector; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $pathRegex; |
|
31
|
|
|
|
|
32
|
4 |
|
public function __construct(string $projectPath) |
|
33
|
|
|
{ |
|
34
|
4 |
|
$this->sourceLocator = new MemoizingSourceLocator( |
|
35
|
4 |
|
(new MakeLocatorForInstalledJson())( |
|
36
|
4 |
|
$projectPath, |
|
37
|
4 |
|
(new BetterReflection())->astLocator() |
|
38
|
|
|
) |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
4 |
|
$this->reflector = new ClassReflector($this->sourceLocator); |
|
42
|
|
|
|
|
43
|
|
|
// @todo: support https://getcomposer.org/doc/06-config.md#vendor-dir; useless as BetterReflection does not, at this moment. |
|
44
|
4 |
|
$cleanPath = preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $projectPath) . '/' . 'vendor', '@'); |
|
45
|
|
|
|
|
46
|
4 |
|
$this->pathRegex = '@^' . $cleanPath . '/(?:composer/\.\./)?([^/]+/[^/]+)/@'; |
|
47
|
4 |
|
} |
|
48
|
|
|
|
|
49
|
4 |
|
public function __invoke(string $symbolName): \Generator |
|
50
|
|
|
{ |
|
51
|
4 |
|
$reflection = $this->sourceLocator->locateIdentifier($this->reflector, new Identifier($symbolName, new IdentifierType(IdentifierType::IDENTIFIER_CLASS))); |
|
52
|
|
|
|
|
53
|
4 |
|
if (!($reflection instanceof ReflectionClass)) { |
|
54
|
2 |
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
$path = $reflection->getFileName(); |
|
58
|
|
|
|
|
59
|
2 |
|
if (preg_match($this->pathRegex, $path, $captures)) { |
|
60
|
2 |
|
yield $captures[1]; |
|
61
|
|
|
} |
|
62
|
1 |
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|