Skip to content

Commit b4a991a

Browse files
committed
Cleaned up
1 parent 779bd28 commit b4a991a

File tree

2 files changed

+32
-47
lines changed

2 files changed

+32
-47
lines changed

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ parameters:
66
ignoreErrors:
77
- "#Method [a-zA-Z0-9\\_\\\\:\\(\\)]+ has parameter \\$[a-zA-Z0-9_]+ with no value type specified in iterable type array#"
88
- "#Method [a-zA-Z0-9\\_\\\\:\\(\\)]+ return type has no value type specified in iterable type array#"
9+
- "#Calls to function fnmatch should not exist.#"
910

1011
# Local Variables:
1112
# mode: yaml

src/Drush/Commands/ConfigHelperCommands.php

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Drupal\Core\Config\ConfigFactoryInterface;
66
use Drupal\Core\Extension\ModuleHandlerInterface;
77
use Drupal\Core\File\FileSystemInterface;
8-
use Drupal\Core\Site\Settings;
8+
use Drupal\Core\Serialization\Yaml;
99
use Drush\Attributes as CLI;
1010
use Drush\Commands\AutowireTrait;
1111
use Drush\Commands\DrushCommands;
@@ -38,7 +38,7 @@ public function __construct(
3838
#[CLI\Usage(name: 'drush config_helper:list "views.view.*"', description: 'List all views.')]
3939
public function list(
4040
array $patterns,
41-
) {
41+
): void {
4242
$names = $this->getConfigNames($patterns);
4343

4444
foreach ($names as $name) {
@@ -89,23 +89,22 @@ public function enforceModuleDependencies(
8989
}
9090

9191
/**
92-
* Move config info config folder in a module.
92+
* Write config info config folder in module.
9393
*/
94-
#[CLI\Command(name: 'config_helper:move-module-config')]
94+
#[CLI\Command(name: 'config_helper:write-module-config')]
9595
#[CLI\Argument(name: 'module', description: 'The module name.')]
9696
#[CLI\Argument(name: 'configNames', description: 'The config names.')]
9797
#[CLI\Option(name: 'optional', description: 'Create as optional config.')]
9898
#[CLI\Option(name: 'enforced', description: 'Move only config with enforced dependency on the module.')]
9999
#[CLI\Option(name: 'dry-run', description: 'Dry run.')]
100-
#[CLI\Usage(name: 'drush config_helper:move-module-config my_module', description: '')]
101-
#[CLI\Usage(name: 'drush config_helper:move-module-config --source=sites/all/config my_module', description: '')]
102-
public function moveModuleConfig(
100+
#[CLI\Usage(name: 'drush config_helper:write-module-config my_module', description: '')]
101+
#[CLI\Usage(name: 'drush config_helper:write-module-config --source=sites/all/config my_module', description: '')]
102+
public function writeModuleConfig(
103103
string $module,
104104
array $configNames,
105105
array $options = [
106106
'optional' => FALSE,
107107
'enforced' => FALSE,
108-
'dry-run' => FALSE,
109108
]
110109
): void {
111110
if (!$this->moduleHandler->moduleExists($module)) {
@@ -116,46 +115,31 @@ public function moveModuleConfig(
116115
$configNames = $this->getEnforcedModuleConfigNames($module);
117116
}
118117
else {
119-
if (empty($configNames)) {
120-
$configNames = $this->getModuleConfigNames($module);
121-
}
118+
$configNames = empty($configNames)
119+
? $this->getModuleConfigNames($module)
120+
: $this->getConfigNames($configNames);
122121
}
123122

124-
$source = $options['source'] ?? Settings::get('config_sync_directory');
125-
if (NULL === $source) {
126-
throw new RuntimeException('Config source not defined');
127-
}
128-
$configPath = DRUPAL_ROOT . '/' . $source;
129-
if (!is_dir($configPath)) {
130-
throw new RuntimeException(sprintf('Config directory %s does not exist',
131-
$configPath));
132-
}
133123
$modulePath = $this->moduleHandler->getModule($module)->getPath();
134-
$moduleConfigPath = $modulePath . '/config/install';
135-
if (!$options['dry-run']) {
124+
$moduleConfigPath = $modulePath . '/config/' . ($options['optional'] ? 'optional' : 'install');
125+
126+
$question = sprintf("Write config\n * %s\n into %s?", implode("\n * ", $configNames), $moduleConfigPath);
127+
if ($this->io()->confirm($question)) {
136128
if (!is_dir($moduleConfigPath)) {
137129
$this->fileSystem->mkdir($moduleConfigPath, 0755, TRUE);
138130
}
139-
}
140-
141-
foreach ($configNames as $name) {
142-
$this->io()->writeln($name);
143131

144-
$filename = $name . '.yml';
145-
$source = $configPath . '/' . $filename;
146-
$destination = $moduleConfigPath . '/' . $filename;
132+
foreach ($configNames as $name) {
133+
$this->io()->writeln($name);
147134

148-
if (!file_exists($source)) {
149-
$this->output()->writeln(sprintf('Source file %s does not exist',
150-
$source));
151-
continue;
152-
}
135+
$filename = $name . '.yml';
136+
$destination = $moduleConfigPath . '/' . $filename;
137+
$config = $this->configFactory->get($name)->getRawData();
138+
// @see https://www.drupal.org/node/2087879#s-exporting-configuration
139+
unset($config['uuid'], $config['_core']);
153140

154-
if (!$options['dry-run']) {
155-
$this->fileSystem->move($source, $destination,
156-
FileSystemInterface::EXISTS_REPLACE);
141+
file_put_contents($destination, Yaml::encode($config));
157142
}
158-
$this->io()->writeln(sprintf('%s -> %s', $source, $destination));
159143
}
160144
}
161145

@@ -229,13 +213,6 @@ private function replaceKeysAndValues(
229213
return $return;
230214
}
231215

232-
/**
233-
* Check if a config name exists.
234-
*/
235-
private function configExists(string $name): bool {
236-
return in_array($name, $this->configFactory->listAll(), TRUE);
237-
}
238-
239216
/**
240217
* Get config names matching patterns.
241218
*/
@@ -249,6 +226,7 @@ private function getConfigNames(array $patterns): array {
249226
foreach ($patterns as $pattern) {
250227
$chunk = array_filter(
251228
$names,
229+
// phpcs:disable Drupal.Functions.DiscouragedFunctions.Discouraged -- https://www.php.net/manual/en/function.fnmatch.php#refsect1-function.fnmatch-notes
252230
static fn ($name) => fnmatch($pattern, $name),
253231
);
254232
if (empty($chunk)) {
@@ -274,17 +252,23 @@ private function getModuleConfigNames(string $module): array {
274252
* Get names of config that has an enforced dependency on a module.
275253
*/
276254
private function getEnforcedModuleConfigNames(string $module): array {
277-
return array_values(
255+
$configNames = array_values(
278256
array_filter(
279257
$this->configFactory->listAll(),
280-
static function ($name) use ($module) {
258+
function ($name) use ($module) {
281259
$config = $this->configFactory->get($name)->getRawData();
282260
$list = $config['dependencies']['enforced']['module'] ?? NULL;
283261

284262
return is_array($list) && in_array($module, $list, TRUE);
285263
}
286264
)
287265
);
266+
267+
if (empty($configNames)) {
268+
throw new RuntimeException(sprintf('No config has an enforced dependency on "%s".', $module));
269+
}
270+
271+
return $configNames;
288272
}
289273

290274
}

0 commit comments

Comments
 (0)