|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Humbug\PhpScoper\Console\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Humbug\PhpScoper\Handler\HandleAddPrefix; |
|
8
|
|
|
use Humbug\PhpScoper\Logger\ConsoleLogger; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
15
|
|
|
|
|
16
|
|
|
final class AddPrefixCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
/** @private */ |
|
19
|
|
|
const PREFIX_ARG = 'prefix'; |
|
20
|
|
|
/** @private */ |
|
21
|
|
|
const PATH_ARG = 'paths'; |
|
22
|
|
|
|
|
23
|
|
|
private $fileSystem; |
|
24
|
|
|
private $handle; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
*/ |
|
29
|
10 |
|
public function __construct(HandleAddPrefix $handle) |
|
30
|
|
|
{ |
|
31
|
10 |
|
parent::__construct(); |
|
32
|
|
|
|
|
33
|
10 |
|
$this->fileSystem = new Filesystem(); |
|
34
|
10 |
|
$this->handle = $handle; |
|
35
|
10 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritdoc |
|
39
|
|
|
*/ |
|
40
|
10 |
|
protected function configure() |
|
41
|
|
|
{ |
|
42
|
|
|
$this |
|
43
|
10 |
|
->setName('add-prefix') |
|
44
|
10 |
|
->setDescription('Goes through all the PHP files found in the given paths to apply the given prefix to namespaces & FQNs.') |
|
45
|
10 |
|
->addArgument(self::PREFIX_ARG, InputArgument::REQUIRED, 'The namespace prefix to add') |
|
46
|
10 |
|
->addArgument(self::PATH_ARG, InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s) to process.') |
|
47
|
|
|
; |
|
48
|
10 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritdoc |
|
52
|
|
|
*/ |
|
53
|
10 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
54
|
|
|
{ |
|
55
|
10 |
|
$this->validatePrefix($input); |
|
56
|
5 |
|
$this->validatePaths($input); |
|
57
|
|
|
|
|
58
|
5 |
|
$logger = new ConsoleLogger($this->getApplication(), $output); |
|
59
|
|
|
|
|
60
|
5 |
|
$logger->outputScopingStart(); |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
5 |
|
$this->handle->__invoke( |
|
64
|
5 |
|
$input->getArgument(self::PREFIX_ARG), |
|
65
|
5 |
|
$input->getArgument(self::PATH_ARG), |
|
66
|
|
|
$logger |
|
67
|
|
|
); |
|
68
|
4 |
|
} finally { |
|
69
|
5 |
|
$logger->outputScopingEnd(); |
|
70
|
|
|
} |
|
71
|
4 |
|
} |
|
72
|
|
|
|
|
73
|
10 |
|
private function validatePrefix(InputInterface $input) |
|
74
|
|
|
{ |
|
75
|
10 |
|
$prefix = trim($input->getArgument(self::PREFIX_ARG)); |
|
76
|
|
|
|
|
77
|
10 |
|
if (1 === preg_match('/(?<prefix>.*?)\\\\*$/', $prefix, $matches)) { |
|
78
|
10 |
|
$prefix = $matches['prefix']; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
10 |
|
if ('' === $prefix) { |
|
82
|
5 |
|
throw new RuntimeException( |
|
83
|
|
|
sprintf( |
|
84
|
5 |
|
'Expected "%s" argument to be a non empty string.', |
|
85
|
5 |
|
self::PREFIX_ARG |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
5 |
|
$input->setArgument(self::PREFIX_ARG, $prefix); |
|
91
|
5 |
|
} |
|
92
|
|
|
|
|
93
|
5 |
|
private function validatePaths(InputInterface $input) |
|
94
|
|
|
{ |
|
95
|
5 |
|
$cwd = getcwd(); |
|
96
|
5 |
|
$fileSystem = $this->fileSystem; |
|
97
|
|
|
|
|
98
|
5 |
|
$paths = array_map( |
|
99
|
5 |
|
function (string $path) use ($cwd, $fileSystem) { |
|
100
|
5 |
|
if (false === $fileSystem->isAbsolutePath($path)) { |
|
101
|
1 |
|
return $cwd.DIRECTORY_SEPARATOR.$path; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
5 |
|
return $path; |
|
105
|
5 |
|
}, |
|
106
|
5 |
|
$input->getArgument(self::PATH_ARG) |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
5 |
|
$input->setArgument(self::PATH_ARG, $paths); |
|
110
|
5 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|