-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathProjectConfigController.php
583 lines (507 loc) · 20.4 KB
/
ProjectConfigController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\console\controllers;
use Craft;
use craft\console\Controller;
use craft\events\ConfigEvent;
use craft\helpers\Console;
use craft\helpers\DateTimeHelper;
use craft\helpers\FileHelper;
use craft\helpers\ProjectConfig;
use craft\services\ProjectConfig as ProjectConfigService;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
use Throwable;
use yii\console\ExitCode;
/**
* Manages the Project Config.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.1.0
*/
class ProjectConfigController extends Controller
{
/**
* @var bool Whether every entry change should be force-applied.
*/
public bool $force = false;
/**
* @var bool Whether to reduce the command output.
* @since 4.4.0
*/
public bool $quiet = false;
/**
* @var bool Whether to treat the loaded project config as the source of truth, instead of the YAML files.
* @since 3.5.13
*/
public bool $invert = false;
/**
* @var bool Whether to pull values from the project config YAML files instead of the loaded config.
* @since 4.1.0
*/
public bool $external = false;
/**
* @var string|null A message describing the changes.
* @see \craft\services\ProjectConfig::set()
* @since 4.1.0
*/
public ?string $message = null;
/**
* @var bool Whether the `dateModified` value should be updated
* @see \craft\services\ProjectConfig::set()
* @since 4.1.0
*/
public bool $updateTimestamp = false;
/**
* @var bool Whether to overwrite an existing export file, if a specific file path is given.
* @since 4.2.1
*/
public bool $overwrite = false;
/**
* @var int Counter of the total paths that have been processed.
*/
private int $_pathCount = 0;
/**
* @var array The config paths that are currently being processed.
*/
private array $_processingPaths;
/**
* @var array The config paths that have finished being processed.
*/
private array $_completedPaths = [];
/**
* @inheritdoc
*/
public function options($actionID): array
{
$options = parent::options($actionID);
switch ($actionID) {
case 'apply':
case 'sync':
$options[] = 'force';
$options[] = 'quiet';
break;
case 'diff':
$options[] = 'invert';
break;
case 'get':
$options[] = 'external';
break;
case 'set':
$options[] = 'message';
$options[] = 'updateTimestamp';
$options[] = 'force';
break;
case 'export':
$options[] = 'external';
$options[] = 'overwrite';
break;
}
return $options;
}
/**
* Outputs a project config value.
*
* Example:
* ```
* php craft project-config/get system.edition
* ```
*
* The “path” syntax used here may be composed of directory and filenames (within your `config/project` folder), YAML object keys (including UUIDs for many Craft resources), and integers (referencing numerically-indexed arrays), joined by a dot (`.`): `path.to.nested.array.0.property`.
*
* @param string $path The config item path
* @return int
* @since 4.1.0
*/
public function actionGet(string $path): int
{
$projectConfig = Craft::$app->getProjectConfig();
$value = $projectConfig->get($path, $this->external);
$this->stdout(Yaml::dump($value));
$this->stdout(PHP_EOL);
return ExitCode::OK;
}
/**
* Sets a project config value.
*
* Example:
* ```
* php craft project-config/set some.nested.key
* ```
*
* See [get](#project-config-get) for the accepted key formats.
*
* ::: danger
* This should only be used when the equivalent change is not possible through the control panel or other Craft APIs. By directly modifying project config values, you are bypassing all validation and can easily destabilize configuration.
* :::
*
* Values are updated in the database *and* in your local YAML files, but the root `dateModified` project config property is only touched when using the [`--update-timestamp` flag](#project-config-set-options). If you do not update the timestamp along with the value, the change may not be detected or applied in other environments!
*
* @param string $path The config item path
* @param string $value The config item value as a valid YAML string
* @return int
* @since 4.1.0
*/
public function actionSet(string $path, string $value): int
{
try {
$parsedValue = Yaml::parse($value);
} catch (ParseException $e) {
$this->stderr('Input value must be valid YAML.' . PHP_EOL, Console::FG_RED);
return ExitCode::USAGE;
}
$projectConfig = Craft::$app->getProjectConfig();
$projectConfig->set(
$path,
$parsedValue,
$this->message,
$this->updateTimestamp,
$this->force,
);
$value = $projectConfig->get($path);
$dumpedValue = Yaml::dump($value);
$multiline = str_contains($dumpedValue, PHP_EOL);
$this->stdout('Project config path ');
$this->stdout($path, Console::FG_CYAN);
$this->stdout(' has been ');
if ($value === null) {
$this->stdout('removed', Console::FG_BLUE);
} else {
$this->stdout('set to' . ($multiline ? ':' . PHP_EOL : ' '));
$this->stdout($dumpedValue, Console::FG_BLUE);
}
$this->stdout(($multiline ? '' : '.') . PHP_EOL);
return ExitCode::OK;
}
/**
* Removes a project config value.
*
* Example:
* ```
* php craft project-config/remove some.nested.key
* ```
*
* ::: danger
* This should only be used when the equivalent change is not possible through the control panel or other Craft APIs. By directly modifying project config values, you are bypassing all validation and can easily destabilize configuration.
* :::
*
* As with [set](#project-config-set), removing values only updates the root `dateModified` key when using the [`--update-timestamp` flag](#project-config-set-options). If you do not include this flag, you must run `project-config/touch` before changes will be detected or applied in other environments!
*
* @param string $path The config item path
* @return int
* @since 4.1.0
*/
public function actionRemove(string $path): int
{
return $this->runAction('set', [$path, 'null']);
}
/**
* Outputs a diff of the pending project config YAML changes.
*
* @return int
* @since 3.5.6
*/
public function actionDiff(): int
{
$diff = ProjectConfig::diff($this->invert);
if ($diff === '') {
$this->stdout('No pending project config YAML changes.' . PHP_EOL, Console::FG_GREEN);
return ExitCode::OK;
}
if (!$this->isColorEnabled()) {
$this->stdout($diff . PHP_EOL . PHP_EOL);
return ExitCode::OK;
}
foreach (explode("\n", $diff) as $line) {
$firstChar = $line[0] ?? '';
switch ($firstChar) {
case '-':
$this->stdout($line . PHP_EOL, Console::FG_RED);
break;
case '+':
$this->stdout($line . PHP_EOL, Console::FG_GREEN);
break;
default:
$this->stdout($line . PHP_EOL);
break;
}
}
$this->stdout(PHP_EOL);
return ExitCode::OK;
}
/**
* Applies project config file changes.
*
* @return int
*/
public function actionApply(): int
{
$updatesService = Craft::$app->getUpdates();
if ($updatesService->getIsCraftUpdatePending() || $updatesService->getIsPluginUpdatePending()) {
$this->stderr('Craft has pending migrations. Please run `craft migrate/all` first.' . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
$projectConfig = Craft::$app->getProjectConfig();
$issues = [];
if (!$projectConfig->getAreConfigSchemaVersionsCompatible($issues)) {
$this->stderr("Your project config files were created for different versions of Craft and/or plugins than what’s currently installed." . PHP_EOL . PHP_EOL, Console::FG_YELLOW);
foreach ($issues as $issue) {
$this->stderr($issue['cause'], Console::FG_RED);
$this->stderr(' is installed with schema version of ', Console::FG_YELLOW);
$this->stderr($issue['existing'], Console::FG_RED);
$this->stderr(' while ', Console::FG_YELLOW);
$this->stderr($issue['incoming'], Console::FG_RED);
$this->stderr(' was expected.' . PHP_EOL, Console::FG_YELLOW);
}
$this->stderr(PHP_EOL . 'Try running `composer install` from your terminal to resolve.' . PHP_EOL, Console::FG_YELLOW);
return ExitCode::UNSPECIFIED_ERROR;
}
// Do we need to create a new config file?
if (!$projectConfig->getDoesExternalConfigExist()) {
$this->stdout("No project config files found. Generating them from internal config ... ", Console::FG_YELLOW);
$projectConfig->regenerateExternalConfig();
} else {
// Any plugins need to be installed/uninstalled?
$loadedConfigPlugins = array_keys($projectConfig->get(ProjectConfigService::PATH_PLUGINS) ?? []);
$yamlPlugins = array_keys($projectConfig->get(ProjectConfigService::PATH_PLUGINS, true) ?? []);
if (!$this->_installPlugins(array_diff($yamlPlugins, $loadedConfigPlugins))) {
$this->stdout('Aborting config apply process' . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
$this->_uninstallPlugins(array_diff($loadedConfigPlugins, $yamlPlugins));
$this->stdout('Applying changes from your project config files ...');
try {
$forceUpdate = $projectConfig->forceUpdate;
$projectConfig->forceUpdate = $this->force;
if (!$this->quiet) {
$this->_processingPaths = [];
$projectConfig->on(ProjectConfigService::EVENT_ADD_ITEM, [$this, 'onStartProcessingItem'], ['label' => 'adding'], false);
$projectConfig->on(ProjectConfigService::EVENT_ADD_ITEM, [$this, 'onFinishProcessingItem'], ['label' => 'adding'], true);
$projectConfig->on(ProjectConfigService::EVENT_REMOVE_ITEM, [$this, 'onStartProcessingItem'], ['label' => 'removing'], false);
$projectConfig->on(ProjectConfigService::EVENT_REMOVE_ITEM, [$this, 'onFinishProcessingItem'], ['label' => 'removing'], true);
$projectConfig->on(ProjectConfigService::EVENT_UPDATE_ITEM, [$this, 'onStartProcessingItem'], ['label' => 'updating'], false);
$projectConfig->on(ProjectConfigService::EVENT_UPDATE_ITEM, [$this, 'onFinishProcessingItem'], ['label' => 'updating'], true);
}
$projectConfig->applyExternalChanges();
$projectConfig->forceUpdate = $forceUpdate;
} catch (Throwable $e) {
$this->stderr("\nerror: " . $e->getMessage() . PHP_EOL, Console::FG_RED);
Craft::$app->getErrorHandler()->logException($e);
return ExitCode::UNSPECIFIED_ERROR;
}
}
$this->stdout("\nFinished applying changes\n", Console::FG_GREEN);
$projectConfig->off(ProjectConfigService::EVENT_ADD_ITEM, [$this, 'onStartProcessingItem']);
$projectConfig->off(ProjectConfigService::EVENT_ADD_ITEM, [$this, 'onFinishProcessingItem']);
$projectConfig->off(ProjectConfigService::EVENT_REMOVE_ITEM, [$this, 'onStartProcessingItem']);
$projectConfig->off(ProjectConfigService::EVENT_REMOVE_ITEM, [$this, 'onFinishProcessingItem']);
$projectConfig->off(ProjectConfigService::EVENT_UPDATE_ITEM, [$this, 'onStartProcessingItem']);
$projectConfig->off(ProjectConfigService::EVENT_UPDATE_ITEM, [$this, 'onFinishProcessingItem']);
return ExitCode::OK;
}
/**
* Called when a project config item has started getting processed.
*
* @param ConfigEvent $event
* @since 3.6.10
*/
public function onStartProcessingItem(ConfigEvent $event): void
{
if (isset($this->_processingPaths[$event->path]) || isset($this->_completedPaths[$event->path])) {
return;
}
$this->stdout("\n");
// Are we in the middle of processing another path(s)?
$otherPaths = count($this->_processingPaths);
if ($otherPaths !== 0) {
$this->stdout(str_repeat(' ', $otherPaths));
}
$this->stdout("- {$event->data['label']} ");
$this->stdout($event->path, Console::FG_CYAN);
$this->stdout(' ... ');
$this->_processingPaths[$event->path] = ++$this->_pathCount;
}
/**
* Called when a project config item has finished getting processed.
*
* @param ConfigEvent $event
* @since 3.6.10
*/
public function onFinishProcessingItem(ConfigEvent $event): void
{
if (!isset($this->_processingPaths[$event->path])) {
return;
}
// Have any other paths been processed since this one started?
if ($this->_processingPaths[$event->path] !== $this->_pathCount) {
$this->stdout("\n" . str_repeat(' ', count($this->_processingPaths) - 1) . ' ');
}
$this->stdout('done', Console::FG_GREEN);
unset($this->_processingPaths[$event->path]);
$this->_completedPaths[$event->path] = true;
}
/**
* DEPRECATED. Use `project-config/apply` instead.
*
* @return int
* @deprecated in 3.5.0. Use [[actionApply()]] instead.
*/
public function actionSync(): int
{
$this->stderr('project-config/sync has been renamed to project-config/apply. Running that instead...' . PHP_EOL, Console::FG_RED);
return $this->runAction('apply');
}
/**
* Writes out the currently-loaded project config as YAML files to the `config/project/` folder, discarding any pending YAML changes.
*
* @return int
* @since 3.5.13
*/
public function actionWrite(): int
{
$this->stdout('Writing out project config files ... ');
Craft::$app->getProjectConfig()->regenerateExternalConfig();
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
return ExitCode::OK;
}
/**
* Rebuilds the project config.
*
* @return int
* @since 3.1.20
*/
public function actionRebuild(): int
{
$projectConfig = Craft::$app->getProjectConfig();
if ($projectConfig->writeYamlAutomatically && !$projectConfig->getDoesExternalConfigExist()) {
$this->stdout("No project config files found. Generating them from internal config ... ", Console::FG_YELLOW);
$projectConfig->regenerateExternalConfig();
}
$this->stdout('Rebuilding the project config from the current state ... ', Console::FG_YELLOW);
try {
$projectConfig->rebuild();
} catch (Throwable $e) {
$this->stderr('error: ' . $e->getMessage() . PHP_EOL, Console::FG_RED);
Craft::$app->getErrorHandler()->logException($e);
return ExitCode::UNSPECIFIED_ERROR;
}
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
return ExitCode::OK;
}
/**
* Updates the `dateModified` value in `config/project/project.yaml`, attempting to resolve a Git conflict for it.
*
* @return int
*/
public function actionTouch(): int
{
$time = DateTimeHelper::currentTimeStamp();
ProjectConfig::touch($time);
$this->stdout("The dateModified value in project.yaml is now set to $time." . PHP_EOL, Console::FG_GREEN);
return ExitCode::OK;
}
/**
* Exports the entire project config to a single file.
*
* @param string|null $path The path the project config should be exported to.
* Can be any of the following:
*
* - A full file path
* - A folder path (export will be saved in there with a dynamically-generated name)
* - A filename (export will be saved in the working directory with the given name)
* - Blank (export will be saved in the working directly with a dynamically-generated name)
*
* @since 4.2.1
*/
public function actionExport(?string $path = null): int
{
if ($path !== null) {
// Prefix with the working directory if a relative path or no path is given
if (str_starts_with($path, '.') || !str_contains(FileHelper::normalizePath($path, '/'), '/')) {
$path = getcwd() . DIRECTORY_SEPARATOR . $path;
}
$path = FileHelper::normalizePath($path);
} else {
$path = getcwd();
}
if (is_dir($path)) {
$i = 0;
do {
$testPath = $path . DIRECTORY_SEPARATOR . 'project-config-' . ($this->external ? 'external' : 'internal') . '--' . date('Y-m-d') . ($i ? "--$i" : '') . '.yaml';
$i++;
} while (file_exists($testPath));
$path = $testPath;
} elseif (is_file($path)) {
if (!$this->overwrite) {
if (!$this->interactive) {
$this->stderr("$path already exists. Retry with the --overwrite flag to overwrite it." . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}
if (!$this->confirm("$path already exists. Overwrite?")) {
$this->stdout('Aborting' . PHP_EOL);
return ExitCode::OK;
}
}
unlink($path);
}
$this->stdout('Exporting the ' . ($this->external ? 'external' : 'loaded') . ' project config data ... ');
$config = Craft::$app->getProjectConfig()->get(null, $this->external);
$content = Yaml::dump(ProjectConfig::cleanupConfig($config), 20, 2);
FileHelper::writeToFile($path, $content);
$this->stdout("done\n", Console::FG_GREEN);
$size = Craft::$app->getFormatter()->asShortSize(filesize($path));
$this->stdout('Exported to: ');
$this->stdout($path, Console::FG_CYAN);
$this->stdout(" ($size)\n");
return ExitCode::OK;
}
/**
* Uninstalls plugins.
*
* @param string[] $handles
*/
private function _uninstallPlugins(array $handles): void
{
$pluginsService = Craft::$app->getPlugins();
foreach ($handles as $handle) {
$this->stdout('Uninstalling plugin ', Console::FG_YELLOW);
$this->stdout("\"$handle\"", Console::FG_CYAN);
$this->stdout(' ... ', Console::FG_YELLOW);
ob_start();
$pluginsService->uninstallPlugin($handle, true);
ob_end_clean();
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
}
}
/**
* Installs plugins.
*
* @param string[] $handles
* @return bool
*/
private function _installPlugins(array $handles): bool
{
$pluginsService = Craft::$app->getPlugins();
foreach ($handles as $handle) {
$this->stdout('Installing plugin ', Console::FG_YELLOW);
$this->stdout("\"$handle\"", Console::FG_CYAN);
$this->stdout(' ... ', Console::FG_YELLOW);
ob_start();
try {
$pluginsService->installPlugin($handle);
ob_end_clean();
$this->stdout('done' . PHP_EOL, Console::FG_GREEN);
} catch (Throwable $e) {
ob_end_clean();
$this->stdout('error: ' . $e->getMessage() . PHP_EOL, Console::FG_RED);
Craft::$app->getErrorHandler()->logException($e);
return false;
}
}
return true;
}
}