1 | <?php |
||
31 | class HandleAddPrefix |
||
32 | { |
||
33 | private $fileSystem; |
||
34 | private $scoper; |
||
35 | |||
36 | 10 | public function __construct(Scoper $scoper) |
|
41 | |||
42 | /** |
||
43 | * Apply prefix to all the code found in the given paths, AKA scope all the files found. |
||
44 | * |
||
45 | * @param string $prefix e.g. 'Foo' |
||
46 | * @param string[] $paths List of files to scan (absolute paths) |
||
47 | * @param string $output absolute path to the output directory |
||
48 | * @param callable[] $patchers |
||
49 | * @param string[]|callable[] $globalNamespaceWhitelist |
||
50 | * @param bool $stopOnFailure |
||
51 | * @param ConsoleLogger $logger |
||
52 | */ |
||
53 | 10 | public function __invoke( |
|
54 | string $prefix, |
||
55 | array $paths, |
||
56 | string $output, |
||
57 | array $patchers, |
||
58 | array $globalNamespaceWhitelist, |
||
59 | bool $stopOnFailure, |
||
60 | ConsoleLogger $logger |
||
61 | ) { |
||
62 | 10 | $this->fileSystem->mkdir($output); |
|
63 | |||
64 | try { |
||
65 | 10 | $files = $this->retrieveFiles($paths, $output); |
|
66 | |||
67 | 9 | $globalWhitelister = $this->createGlobalWhitelister($globalNamespaceWhitelist); |
|
68 | |||
69 | 9 | $this->scopeFiles($files, $prefix, $patchers, $globalWhitelister, $stopOnFailure, $logger); |
|
70 | 3 | } catch (Throwable $throwable) { |
|
71 | 3 | $this->fileSystem->remove($output); |
|
72 | |||
73 | 3 | throw $throwable; |
|
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param string[]|callable[] $globalNamespaceWhitelist |
||
79 | * |
||
80 | * @return Closure |
||
81 | */ |
||
82 | 9 | private function createGlobalWhitelister(array $globalNamespaceWhitelist): Closure |
|
103 | |||
104 | /** |
||
105 | * @param string[] $paths |
||
106 | * @param string $output |
||
107 | * |
||
108 | * @return string[] |
||
109 | */ |
||
110 | 10 | private function retrieveFiles(array $paths, string $output): array |
|
179 | |||
180 | /** |
||
181 | * @param string[] $files |
||
182 | * @param string $prefix |
||
183 | * @param callable[] $patchers |
||
184 | * @param callable $globalWhitelister |
||
185 | * @param bool $stopOnFailure |
||
186 | * @param ConsoleLogger $logger |
||
187 | */ |
||
188 | 9 | private function scopeFiles( |
|
203 | |||
204 | /** |
||
205 | * @param string $inputFilePath |
||
206 | * @param string $outputFilePath |
||
207 | * @param string $prefix |
||
208 | * @param callable[] $patchers |
||
209 | * @param callable $globalWhitelister |
||
210 | * @param bool $stopOnFailure |
||
211 | * @param ConsoleLogger $logger |
||
212 | */ |
||
213 | 8 | private function scopeFile( |
|
249 | } |
||
250 |