|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the webmozart/php-scoper package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Humbug\PhpScoper\Handler; |
|
13
|
|
|
|
|
14
|
|
|
use Humbug\PhpScoper\Logger\ConsoleLogger; |
|
15
|
|
|
use Humbug\PhpScoper\Scoper; |
|
16
|
|
|
use Humbug\PhpScoper\Throwable\Exception\ParsingException; |
|
17
|
|
|
use Humbug\PhpScoper\Throwable\Exception\RuntimeException; |
|
18
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
19
|
|
|
use Symfony\Component\Finder\Finder; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @final |
|
23
|
|
|
*/ |
|
24
|
|
|
class HandleAddPrefix |
|
25
|
|
|
{ |
|
26
|
|
|
/** @internal */ |
|
27
|
|
|
const PHP_FILE_PATTERN = '/\.php$/'; |
|
28
|
|
|
|
|
29
|
|
|
private $fileSystem; |
|
30
|
|
|
private $scoper; |
|
31
|
|
|
|
|
32
|
6 |
|
public function __construct(Scoper $scoper) |
|
33
|
|
|
{ |
|
34
|
6 |
|
$this->fileSystem = new Filesystem(); |
|
35
|
6 |
|
$this->scoper = $scoper; |
|
36
|
6 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Apply prefix to all the code found in the given paths, AKA scope all the files found. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $prefix |
|
42
|
|
|
* @param string[] $paths |
|
43
|
|
|
* @param ConsoleLogger $logger |
|
44
|
|
|
* |
|
45
|
|
|
* @throws RuntimeException |
|
46
|
|
|
*/ |
|
47
|
6 |
|
public function __invoke(string $prefix, array $paths, ConsoleLogger $logger) |
|
48
|
|
|
{ |
|
49
|
6 |
|
$files = $this->retrieveFiles($paths); |
|
50
|
|
|
|
|
51
|
6 |
|
$this->scopeFiles($files, $prefix, $logger); |
|
52
|
6 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string[] $paths |
|
56
|
|
|
* |
|
57
|
|
|
* @return Finder |
|
58
|
|
|
*/ |
|
59
|
6 |
|
private function retrieveFiles(array $paths): Finder |
|
60
|
|
|
{ |
|
61
|
6 |
|
$pathsToSearch = []; |
|
62
|
6 |
|
$filesToAppend = []; |
|
63
|
|
|
|
|
64
|
6 |
|
foreach ($paths as $path) { |
|
65
|
6 |
|
if (is_dir($path)) { |
|
66
|
3 |
|
$pathsToSearch[] = $path; |
|
67
|
4 |
|
} elseif (1 === preg_match(self::PHP_FILE_PATTERN, $path)) { |
|
68
|
6 |
|
$filesToAppend[] = $path; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
6 |
|
$finder = new Finder(); |
|
73
|
|
|
|
|
74
|
6 |
|
$finder->files() |
|
75
|
6 |
|
->name(self::PHP_FILE_PATTERN) |
|
76
|
6 |
|
->in($pathsToSearch) |
|
77
|
6 |
|
->append($filesToAppend) |
|
78
|
6 |
|
->sortByName() |
|
79
|
|
|
; |
|
80
|
|
|
|
|
81
|
6 |
|
return $finder; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
6 |
|
private function scopeFiles(Finder $files, string $prefix, ConsoleLogger $logger) |
|
85
|
|
|
{ |
|
86
|
6 |
|
$count = count($files); |
|
87
|
6 |
|
$logger->outputFileCount($count); |
|
88
|
|
|
|
|
89
|
6 |
|
foreach ($files as $file) { |
|
90
|
4 |
|
if (false === file_exists($file)) { |
|
91
|
|
|
throw new RuntimeException( |
|
92
|
|
|
sprintf( |
|
93
|
|
|
'Could not find the path "%s".', |
|
94
|
|
|
$file |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
4 |
|
if (false === is_readable($file)) { |
|
100
|
|
|
throw new RuntimeException( |
|
101
|
|
|
sprintf( |
|
102
|
|
|
'Could not read the path "%s".', |
|
103
|
|
|
$file |
|
104
|
|
|
) |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
4 |
|
$this->scopeFile($file->getPathName(), $prefix, $logger); |
|
109
|
|
|
} |
|
110
|
6 |
|
} |
|
111
|
|
|
|
|
112
|
4 |
|
private function scopeFile(string $path, string $prefix, ConsoleLogger $logger) |
|
113
|
|
|
{ |
|
114
|
4 |
|
$fileContent = file_get_contents($path); |
|
115
|
|
|
|
|
116
|
|
|
try { |
|
117
|
4 |
|
$scoppedContent = $this->scoper->scope($fileContent, $prefix); |
|
118
|
|
|
|
|
119
|
4 |
|
$this->fileSystem->dumpFile($path, $scoppedContent); |
|
120
|
|
|
|
|
121
|
4 |
|
$logger->outputSuccess($path); |
|
122
|
|
|
} catch (ParsingException $exception) { |
|
123
|
|
|
//TODO: display error in verbose mode |
|
124
|
|
|
$logger->outputFail($path); |
|
125
|
|
|
} |
|
126
|
4 |
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|