1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the ekino Drupal Debug project. |
7
|
|
|
* |
8
|
|
|
* (c) ekino |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Ekino\Drupal\Debug\Composer\Helper; |
15
|
|
|
|
16
|
|
|
use Composer\Composer; |
17
|
|
|
use Composer\IO\IOInterface; |
18
|
|
|
use Ekino\Drupal\Debug\ActionMetadata\ActionMetadataManager; |
19
|
|
|
use Ekino\Drupal\Debug\Composer\Command\DumpReferenceConfigurationFileCommand; |
20
|
|
|
use Ekino\Drupal\Debug\Configuration\ActionsConfiguration; |
21
|
|
|
use Ekino\Drupal\Debug\Configuration\ConfigurationManager; |
22
|
|
|
use Ekino\Drupal\Debug\Configuration\DefaultsConfiguration; |
23
|
|
|
use Ekino\Drupal\Debug\Configuration\Model\DefaultsConfiguration as DefaultsConfigurationModel; |
24
|
|
|
use Ekino\Drupal\Debug\Configuration\SubstituteOriginalDrupalKernelConfiguration; |
25
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
26
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
27
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
28
|
|
|
use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper; |
29
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
30
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
31
|
|
|
use Symfony\Component\Yaml\Dumper; |
32
|
|
|
use Symfony\Component\Yaml\Parser; |
33
|
|
|
|
34
|
|
|
class ManageConfigurationHelper |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var Composer |
38
|
|
|
*/ |
39
|
|
|
private $composer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var IOInterface |
43
|
|
|
*/ |
44
|
|
|
private $IO; |
45
|
|
|
|
46
|
|
|
private $configurationManager; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Composer $composer |
50
|
|
|
* @param IOInterface $IO |
51
|
|
|
*/ |
52
|
21 |
|
public function __construct(Composer $composer, IOInterface $IO) |
53
|
|
|
{ |
54
|
21 |
|
$this->composer = $composer; |
55
|
21 |
|
$this->IO = $IO; |
56
|
|
|
|
57
|
21 |
|
$this->configurationManager = ConfigurationManager::getInstance(); |
58
|
21 |
|
} |
59
|
|
|
|
60
|
5 |
|
public function dumpReferenceConfigurationFile(): bool |
61
|
|
|
{ |
62
|
5 |
|
$configurationFilePath = $this->configurationManager->getConfigurationFilePath(); |
63
|
5 |
|
if ($configurationFilePathExists = $this->configurationManager->doesConfigurationFilePathExists()) { |
64
|
3 |
|
$this->IO->write(array( |
65
|
3 |
|
'<comment>An existing drupal-debug configuration file has been found at the following location:</comment>', |
66
|
3 |
|
\sprintf('<comment>--> "%s"</comment>', \realpath($configurationFilePath)), |
67
|
3 |
|
'', |
68
|
|
|
)); |
69
|
|
|
|
70
|
3 |
|
if (!$this->IO->askConfirmation('<question>Would you like to overwrite it?</question>')) { |
71
|
1 |
|
$this->IO->write(array( |
72
|
1 |
|
'', |
73
|
|
|
"<info>OK, let's keep it like this then!</info>", |
74
|
|
|
)); |
75
|
|
|
|
76
|
1 |
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
$this->IO->write(''); |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
return $this->dumpConfigurationFile($configurationFilePath, $this->getReferenceConfigurationContent(), !$configurationFilePathExists); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// TODO: a real configuration update path |
86
|
2 |
|
public function warnAboutPotentialConfigurationChanges(): bool |
87
|
|
|
{ |
88
|
2 |
|
if (!$this->configurationManager->doesConfigurationFilePathExists()) { |
89
|
1 |
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
$this->IO->write(array( |
93
|
1 |
|
'<comment>A custom drupal-debug configuration file has been found at the following location:</comment>', |
94
|
1 |
|
\sprintf('<comment>--> "%s"</comment>', \realpath($this->configurationManager->getConfigurationFilePath())), |
95
|
1 |
|
'', |
96
|
1 |
|
'<comment>The drupal-debug configuration might have change in the freshly updated code.</comment>', |
97
|
1 |
|
'', |
98
|
1 |
|
'<comment>If you encounter any problem after this update, it will surely be related to configuration. Please refer to the documentation and the release changelog to fix it.</comment>', |
99
|
1 |
|
'', |
100
|
1 |
|
\sprintf('<comment>You can alternatively dump the reference configuration file with the dedicated command "%s".</comment>', DumpReferenceConfigurationFileCommand::NAME), |
101
|
|
|
)); |
102
|
|
|
|
103
|
1 |
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
public function askForConfigurationFileDeletion(): bool |
107
|
|
|
{ |
108
|
4 |
|
if (!$this->configurationManager->doesConfigurationFilePathExists()) { |
109
|
1 |
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
3 |
|
$this->IO->write(array( |
113
|
3 |
|
'<comment>The drupal-debug configuration file is going to be useless: it should be deleted.</comment>', |
114
|
3 |
|
'', |
115
|
3 |
|
'<info>It has been found at the following location:</info>', |
116
|
3 |
|
\sprintf('<info>--> "%s"</info>', \realpath($configurationFilePath = $this->configurationManager->getConfigurationFilePath())), |
117
|
3 |
|
'', |
118
|
|
|
)); |
119
|
|
|
|
120
|
3 |
|
if (!$this->IO->askConfirmation('Would you like to delete it?')) { |
121
|
1 |
|
$this->IO->write(array( |
122
|
1 |
|
'', |
123
|
|
|
"<info>OK, let's keep it!</info>", |
124
|
|
|
)); |
125
|
|
|
|
126
|
1 |
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
$this->IO->write(''); |
130
|
|
|
|
131
|
2 |
|
if (!@\unlink($configurationFilePath)) { |
132
|
1 |
|
$this->IO->writeError('<error>The drupal-debug configuration file could not be deleted.</error>'); |
133
|
|
|
|
134
|
1 |
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
$this->IO->write('<info>The drupal-debug configuration file has been successfully deleted.</info>'); |
138
|
|
|
|
139
|
1 |
|
return true; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param bool $enabled |
144
|
|
|
*/ |
145
|
9 |
|
public function toggleOriginalDrupalKernelSubstitution(bool $enabled): bool |
146
|
|
|
{ |
147
|
9 |
|
$configurationFilePath = $this->configurationManager->getConfigurationFilePath(); |
148
|
|
|
|
149
|
9 |
|
if ($this->configurationManager->doesConfigurationFilePathExists()) { |
150
|
5 |
|
$configurationFileContent = $this->getCurrentConfigurationContent($configurationFilePath); |
151
|
5 |
|
if (\is_array($configurationFileContent) && isset($configurationFileContent['drupal-debug'])) { |
152
|
2 |
|
if (isset($configurationFileContent['drupal-debug']['substitute_original_drupal_kernel']) && \is_array($configurationFileContent['drupal-debug']['substitute_original_drupal_kernel'])) { |
153
|
|
|
$configurationFileContent['drupal-debug']['substitute_original_drupal_kernel']['enabled'] = $enabled; |
154
|
|
|
} else { |
155
|
2 |
|
$configurationFileContent['drupal-debug']['substitute_original_drupal_kernel'] = array( |
156
|
2 |
|
'enabled' => $enabled, |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
} else { |
160
|
3 |
|
$configurationFileContent = null; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
9 |
|
if (!isset($configurationFileContent)) { |
165
|
|
|
$configurationFileContent = array( |
166
|
|
|
'drupal-debug' => array( |
167
|
|
|
'substitute_original_drupal_kernel' => array( |
168
|
7 |
|
'enabled' => $enabled, |
169
|
|
|
), |
170
|
|
|
), |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
9 |
|
return $this->dumpConfigurationFile($configurationFilePath, $configurationFileContent, true); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
private function getReferenceConfigurationContent(): array |
181
|
|
|
{ |
182
|
|
|
return (new Parser())->parse((new YamlReferenceDumper())->dump(new class($this->configurationManager) implements ConfigurationInterface { |
183
|
|
|
private $configurationManager; |
184
|
|
|
|
185
|
4 |
|
public function __construct(ConfigurationManager $configurationManager) |
186
|
|
|
{ |
187
|
4 |
|
$this->configurationManager = $configurationManager; |
188
|
4 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
4 |
|
public function getConfigTreeBuilder(): TreeBuilder |
194
|
|
|
{ |
195
|
4 |
|
$treeBuilder = new TreeBuilder(); |
196
|
|
|
/** @var ArrayNodeDefinition $rootNode */ |
197
|
4 |
|
$rootNode = $treeBuilder->root(ConfigurationManager::ROOT_KEY); |
198
|
|
|
$rootNode |
199
|
4 |
|
->children() |
200
|
4 |
|
->append((new DefaultsConfiguration())->getArrayNodeDefinition(new TreeBuilder())) |
201
|
4 |
|
->append((new ActionsConfiguration((new ActionMetadataManager())->all(), $defaultsConfiguration = new DefaultsConfigurationModel($this->configurationManager->getProcessedDefaultsConfiguration(array()))))->getArrayNodeDefinition(new TreeBuilder())) |
202
|
4 |
|
->append((new SubstituteOriginalDrupalKernelConfiguration($defaultsConfiguration))->getArrayNodeDefinition(new TreeBuilder())) |
203
|
4 |
|
->end(); |
204
|
|
|
|
205
|
4 |
|
return $treeBuilder; |
206
|
|
|
} |
207
|
|
|
})); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $configurationFilePath |
212
|
|
|
* |
213
|
|
|
* @return mixed |
214
|
|
|
*/ |
215
|
5 |
|
private function getCurrentConfigurationContent(string $configurationFilePath) |
216
|
|
|
{ |
217
|
5 |
|
return (new Parser())->parseFile($configurationFilePath); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $configurationFilePath |
222
|
|
|
* @param array $configurationFileContent |
223
|
|
|
* @param bool $displayLocation |
224
|
|
|
*/ |
225
|
13 |
|
private function dumpConfigurationFile(string $configurationFilePath, array $configurationFileContent, bool $displayLocation): bool |
226
|
|
|
{ |
227
|
|
|
try { |
228
|
13 |
|
(new Filesystem())->dumpFile($configurationFilePath, (new Dumper())->dump($configurationFileContent, 5)); |
229
|
4 |
|
} catch (IOException $e) { |
230
|
4 |
|
$this->IO->writeError('<error>The drupal-debug configuration file could not be dumped.</error>'); |
231
|
|
|
|
232
|
4 |
|
return false; |
233
|
|
|
} |
234
|
|
|
|
235
|
9 |
|
if ($displayLocation) { |
236
|
8 |
|
$this->IO->write(array( |
237
|
8 |
|
'<info>The drupal-debug configuration file has been successfully dumped at the following location:</info>', |
238
|
8 |
|
\sprintf('<info>--> "%s"</info>', \realpath($configurationFilePath)), |
239
|
|
|
)); |
240
|
|
|
} else { |
241
|
1 |
|
$this->IO->write('<info>The drupal-debug configuration file has been successfully dumped.</info>'); |
242
|
|
|
} |
243
|
|
|
|
244
|
9 |
|
return true; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|