1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | /** |
||
5 | * Author: panosru |
||
6 | * Date: 2019-01-13 |
||
7 | * Time: 11:32 |
||
8 | */ |
||
9 | |||
10 | namespace Omega\FaultManager\Traits; |
||
11 | |||
12 | use Roave\BetterReflection\BetterReflection; |
||
13 | use Roave\BetterReflection\Reflection\ReflectionClass; |
||
14 | use PhpParser\PrettyPrinter\Standard as CodePrinter; |
||
15 | use Roave\BetterReflection\Reflector\ClassReflector; |
||
16 | use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; |
||
17 | use Roave\BetterReflection\SourceLocator\Type\ComposerSourceLocator; |
||
18 | use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator; |
||
19 | use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator; |
||
20 | |||
21 | /** |
||
22 | * Trait FaultReflector |
||
23 | * @package Omega\FaultManager\Traits |
||
24 | * @codeCoverageIgnore |
||
25 | */ |
||
26 | trait FaultReflector |
||
27 | { |
||
28 | /** @var BetterReflection */ |
||
29 | private static $reflection; |
||
30 | |||
31 | /** |
||
32 | * @param string $className |
||
33 | * @return ReflectionClass |
||
34 | */ |
||
35 | protected static function reflectFromClassName(string $className): ReflectionClass |
||
36 | { |
||
37 | return self::reflection()->classReflector()->reflect($className); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param string $path |
||
42 | * @return ReflectionClass |
||
43 | */ |
||
44 | protected static function reflectFromFile(string $path): ReflectionClass |
||
45 | { |
||
46 | $astLocator = self::reflection()->astLocator(); |
||
47 | $reflector = new ClassReflector(new AggregateSourceLocator([ |
||
48 | new SingleFileSourceLocator($path, $astLocator), |
||
49 | new ComposerSourceLocator((require __DIR__ . '/../../vendor/autoload.php'), $astLocator), |
||
50 | new PhpInternalSourceLocator($astLocator) |
||
51 | ])); |
||
52 | $className = $reflector->getAllClasses()[0]->getName(); |
||
53 | /** @var ReflectionClass $reflection */ |
||
54 | $reflection = $reflector->reflect($className); |
||
55 | |||
56 | self::loadClass($reflection); |
||
57 | |||
58 | return $reflection; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return BetterReflection |
||
63 | */ |
||
64 | private static function reflection(): BetterReflection |
||
65 | { |
||
66 | return self::$reflection ?? new BetterReflection(); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param ReflectionClass $reflection |
||
71 | */ |
||
72 | private static function loadClass(ReflectionClass $reflection): void |
||
73 | { |
||
74 | eval((new CodePrinter())->prettyPrint([$reflection->getAst()])); |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
75 | } |
||
76 | } |
||
77 |