|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the humbug/php-scoper package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
|
9
|
|
|
* Pádraic Brady <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper\Console\Command; |
|
16
|
|
|
|
|
17
|
|
|
use Fidry\Console\Command\Command; |
|
18
|
|
|
use Fidry\Console\Command\Configuration; |
|
19
|
|
|
use Fidry\Console\Command\Configuration as CommandConfiguration; |
|
20
|
|
|
use Fidry\Console\IO; |
|
21
|
|
|
use Symfony\Component\Console\Helper\FormatterHelper; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
23
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
24
|
|
|
use function file_exists; |
|
25
|
|
|
use function Safe\getcwd; |
|
26
|
|
|
use function Safe\sprintf; |
|
27
|
|
|
use const DIRECTORY_SEPARATOR; |
|
28
|
|
|
|
|
29
|
|
|
final class InitCommand implements Command |
|
30
|
|
|
{ |
|
31
|
|
|
private const CONFIG_FILE_OPT = 'config'; |
|
32
|
|
|
private const CONFIG_FILE_TEMPLATE = __DIR__.'/../../scoper.inc.php.tpl'; |
|
33
|
|
|
private const CONFIG_FILE_DEFAULT = 'scoper.inc.php'; |
|
34
|
|
|
|
|
35
|
|
|
private Filesystem $fileSystem; |
|
36
|
|
|
private FormatterHelper $formatterHelper; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct( |
|
39
|
|
|
Filesystem $fileSystem, |
|
40
|
|
|
FormatterHelper $formatterHelper |
|
41
|
|
|
) { |
|
42
|
|
|
$this->fileSystem = $fileSystem; |
|
43
|
|
|
$this->formatterHelper = $formatterHelper; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getConfiguration(): CommandConfiguration |
|
47
|
|
|
{ |
|
48
|
|
|
return new Configuration( |
|
49
|
|
|
'init', |
|
50
|
|
|
'Generates a configuration file.', |
|
51
|
|
|
'', |
|
52
|
|
|
[], |
|
53
|
|
|
[ |
|
54
|
|
|
ChangeableDirectory::createOption(), |
|
55
|
|
|
new InputOption( |
|
56
|
|
|
self::CONFIG_FILE_OPT, |
|
57
|
|
|
'c', |
|
58
|
|
|
InputOption::VALUE_REQUIRED, |
|
59
|
|
|
sprintf( |
|
60
|
|
|
'Configuration file. Will use "%s" if found by default.', |
|
61
|
|
|
self::CONFIG_FILE_DEFAULT |
|
62
|
|
|
), |
|
63
|
|
|
null, |
|
64
|
|
|
), |
|
65
|
|
|
], |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function execute(IO $io): int |
|
70
|
|
|
{ |
|
71
|
|
|
ChangeableDirectory::changeWorkingDirectory($io); |
|
72
|
|
|
|
|
73
|
|
|
$io->writeln(''); |
|
74
|
|
|
$io->writeln( |
|
75
|
|
|
$this->formatterHelper->formatSection( |
|
76
|
|
|
'PHP-Scoper configuration generate', |
|
77
|
|
|
'Welcome!' |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$configFile = $this->retrieveConfig($io); |
|
82
|
|
|
|
|
83
|
|
|
if (null === $configFile) { |
|
84
|
|
|
$io->writeln('Skipping configuration file generator.'); |
|
85
|
|
|
|
|
86
|
|
|
return 0; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->fileSystem->copy(self::CONFIG_FILE_TEMPLATE, $configFile); |
|
90
|
|
|
|
|
91
|
|
|
$io->writeln([ |
|
92
|
|
|
'', |
|
93
|
|
|
sprintf( |
|
94
|
|
|
'Generated the configuration file "<comment>%s</comment>".', |
|
95
|
|
|
$configFile |
|
96
|
|
|
), |
|
97
|
|
|
'', |
|
98
|
|
|
]); |
|
99
|
|
|
|
|
100
|
|
|
return 0; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function retrieveConfig(IO $io): ?string |
|
104
|
|
|
{ |
|
105
|
|
|
$configFile = $io->getNullableStringOption(self::CONFIG_FILE_OPT); |
|
106
|
|
|
|
|
107
|
|
|
$configFile = (null === $configFile) |
|
108
|
|
|
? $this->makeAbsolutePath(self::CONFIG_FILE_DEFAULT) |
|
109
|
|
|
: $this->makeAbsolutePath($configFile) |
|
110
|
|
|
; |
|
111
|
|
|
|
|
112
|
|
|
if (file_exists($configFile)) { |
|
113
|
|
|
$canDeleteFile = $io->confirm( |
|
114
|
|
|
sprintf( |
|
115
|
|
|
'The configuration file "<comment>%s</comment>" already exists. Are you sure you want to ' |
|
116
|
|
|
.'replace it?', |
|
117
|
|
|
$configFile |
|
118
|
|
|
), |
|
119
|
|
|
false |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
if (false === $canDeleteFile) { |
|
123
|
|
|
$io->writeln('Skipped file generation.'); |
|
124
|
|
|
|
|
125
|
|
|
return $configFile; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$this->fileSystem->remove($configFile); |
|
129
|
|
|
} else { |
|
130
|
|
|
$createConfig = $io->confirm('No configuration file found. Do you want to create one?'); |
|
131
|
|
|
|
|
132
|
|
|
if (false === $createConfig) { |
|
133
|
|
|
return null; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $configFile; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
private function makeAbsolutePath(string $path): string |
|
141
|
|
|
{ |
|
142
|
|
|
if (false === $this->fileSystem->isAbsolutePath($path)) { |
|
143
|
|
|
$path = getcwd().DIRECTORY_SEPARATOR.$path; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $path; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|