1 | <?php |
||
31 | class Tokenizer extends Component implements SingletonInterface, TokenizerInterface, InjectorInterface |
||
32 | { |
||
33 | use LoggerTrait, BenchmarkTrait, TokensTrait; |
||
34 | |||
35 | /** |
||
36 | * Memory section. |
||
37 | */ |
||
38 | const MEMORY = 'tokenizer'; |
||
39 | |||
40 | /** |
||
41 | * @var TokenizerConfig |
||
42 | */ |
||
43 | protected $config; |
||
44 | |||
45 | /** |
||
46 | * @invisible |
||
47 | * |
||
48 | * @var FilesInterface |
||
49 | */ |
||
50 | protected $files; |
||
51 | |||
52 | /** |
||
53 | * @invisible |
||
54 | * |
||
55 | * @var MemoryInterface |
||
56 | */ |
||
57 | protected $memory; |
||
58 | |||
59 | /** |
||
60 | * Tokenizer constructor. |
||
61 | * |
||
62 | * @param TokenizerConfig $config |
||
63 | * @param FilesInterface $files |
||
64 | * @param MemoryInterface $memory |
||
65 | */ |
||
66 | public function __construct( |
||
67 | TokenizerConfig $config, |
||
68 | FilesInterface $files = null, |
||
69 | MemoryInterface $memory = null |
||
70 | ) { |
||
71 | $this->config = $config; |
||
72 | |||
73 | $this->files = $files ?? new FileManager(); |
||
74 | $this->memory = $memory ?? new NullMemory(); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function fileReflection(string $filename): ReflectionFile |
||
81 | { |
||
82 | $fileMD5 = $this->files->md5($filename = $this->files->normalizePath($filename)); |
||
83 | |||
84 | $reflection = new ReflectionFile( |
||
85 | $filename, |
||
86 | $this->normalizeTokens(token_get_all($this->files->read($filename))), |
||
87 | (array)$this->memory->loadData(self::MEMORY . '.' . $fileMD5) |
||
88 | ); |
||
89 | |||
90 | //Let's save to cache |
||
91 | $this->memory->saveData(self::MEMORY . '.' . $fileMD5, $reflection->exportSchema()); |
||
92 | |||
93 | return $reflection; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get pre-configured class locator. |
||
98 | * |
||
99 | * @param array $directories |
||
100 | * @param array $exclude |
||
101 | * |
||
102 | * @return ClassesInterface |
||
103 | */ |
||
104 | public function classLocator( |
||
105 | array $directories = [], |
||
106 | array $exclude = [] |
||
107 | ): ClassesInterface { |
||
108 | return new ClassLocator($this, $this->makeFinder($directories, $exclude)); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get pre-configured invocation locator. |
||
113 | * |
||
114 | * @param array $directories |
||
115 | * @param array $exclude |
||
116 | * |
||
117 | * @return InvocationsInterface |
||
118 | */ |
||
119 | public function invocationLocator( |
||
120 | array $directories = [], |
||
121 | array $exclude = [] |
||
122 | ): InvocationsInterface { |
||
123 | return new InvocationsLocator($this, $this->makeFinder($directories, $exclude)); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | * |
||
129 | * @throws InjectionException |
||
130 | */ |
||
131 | public function createInjection(\ReflectionClass $class, string $context = null) |
||
141 | |||
142 | /** |
||
143 | * @param array $directories |
||
144 | * @param array $exclude |
||
145 | * |
||
146 | * @return Finder |
||
147 | */ |
||
148 | private function makeFinder( |
||
164 | } |
||
165 |