1 | <?php |
||
32 | class HandleAddPrefix |
||
33 | { |
||
34 | private $fileSystem; |
||
35 | private $scoper; |
||
36 | |||
37 | 10 | public function __construct(Scoper $scoper) |
|
42 | |||
43 | /** |
||
44 | * Applies prefix to all the code found in the given paths, AKA scope all the files found. |
||
45 | * |
||
46 | * @param string $prefix e.g. 'Foo' |
||
47 | * @param string[] $paths List of files (absolute paths) which will be scoped |
||
48 | * @param string $output Absolute path to the output directory |
||
49 | * @param callable[] $patchers |
||
50 | * @param string[] $whitelist List of classes that will not be scoped |
||
51 | * @param string[]|callable[] $globalNamespaceWhitelist |
||
52 | * @param bool $stopOnFailure |
||
53 | * @param ConsoleLogger $logger |
||
54 | */ |
||
55 | 10 | public function __invoke( |
|
85 | |||
86 | /** |
||
87 | * @param string[]|callable[] $globalNamespaceWhitelist |
||
88 | * |
||
89 | * @return Closure |
||
90 | */ |
||
91 | private function createGlobalWhitelister(array $globalNamespaceWhitelist): Closure |
||
112 | |||
113 | /** |
||
114 | * @param string[] $paths |
||
115 | * @param string $output |
||
116 | * |
||
117 | * @return string[] |
||
118 | */ |
||
119 | 10 | private function retrieveFiles(array $paths, string $output): array |
|
120 | { |
||
121 | 10 | $pathsToSearch = []; |
|
122 | 10 | $filesToAppend = []; |
|
123 | |||
124 | 10 | foreach ($paths as $path) { |
|
125 | 10 | if (false === file_exists($path)) { |
|
126 | 1 | throw new RuntimeException( |
|
127 | 1 | sprintf( |
|
128 | 1 | 'Could not find the file "%s".', |
|
129 | 1 | $path |
|
130 | ) |
||
131 | ); |
||
132 | } |
||
133 | |||
134 | 9 | if (is_dir($path)) { |
|
135 | 3 | $pathsToSearch[] = $path; |
|
136 | } else { |
||
137 | 7 | $filesToAppend[] = $path; |
|
138 | } |
||
139 | } |
||
140 | |||
141 | 9 | $finder = new Finder(); |
|
142 | |||
143 | 9 | $finder->files() |
|
144 | 9 | ->in($pathsToSearch) |
|
145 | 9 | ->append($filesToAppend) |
|
146 | 9 | ->sortByName() |
|
147 | ; |
||
148 | |||
149 | 9 | $files = array_values( |
|
150 | 9 | array_map( |
|
151 | 9 | function (SplFileInfo $fileInfo) { |
|
152 | 8 | return $fileInfo->getRealPath(); |
|
153 | 9 | }, |
|
154 | 9 | iterator_to_array($finder) |
|
155 | ) |
||
156 | ); |
||
157 | |||
158 | 9 | $commonPath = get_common_path($files); |
|
159 | |||
160 | 9 | return array_reduce( |
|
161 | 9 | $files, |
|
162 | 9 | function (array $files, string $file) use ($output, $commonPath): array { |
|
163 | 8 | if (false === file_exists($file)) { |
|
164 | throw new RuntimeException( |
||
165 | sprintf( |
||
166 | 'Could not find the file "%s".', |
||
167 | $file |
||
168 | ) |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | 8 | if (false === is_readable($file)) { |
|
173 | throw new RuntimeException( |
||
174 | sprintf( |
||
175 | 'Could not read the file "%s".', |
||
176 | $file |
||
177 | ) |
||
178 | ); |
||
179 | } |
||
180 | |||
181 | 8 | $files[$file] = $output.str_replace($commonPath, '', $file); |
|
182 | |||
183 | 8 | return $files; |
|
184 | 9 | }, |
|
185 | 9 | [] |
|
186 | ); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param string[] $files |
||
191 | * @param string $prefix |
||
192 | * @param callable[] $patchers |
||
193 | * @param string[] $whitelist |
||
194 | * @param callable $globalWhitelister |
||
195 | * @param bool $stopOnFailure |
||
196 | * @param ConsoleLogger $logger |
||
197 | * |
||
198 | * @return string|null |
||
199 | */ |
||
200 | 9 | private function scopeFiles( |
|
233 | |||
234 | /** |
||
235 | * @param string $inputFilePath |
||
236 | * @param string $outputFilePath |
||
237 | * @param string $prefix |
||
238 | * @param callable[] $patchers |
||
239 | * @param string[] $whitelist |
||
240 | * @param callable $globalWhitelister |
||
241 | * @param bool $stopOnFailure |
||
242 | * @param ConsoleLogger $logger |
||
243 | */ |
||
244 | 8 | private function scopeFile( |
|
281 | } |
||
282 |