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
|
<?php
declare(strict_types=1);
namespace Drupal\Core\Recipe;
use Drupal\Core\Config\Action\ConfigActionException;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DefaultContent\Existing;
use Drupal\Core\DefaultContent\Importer;
use Drupal\Core\DefaultContent\Finder;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* Applies a recipe.
*
* This class is currently static and use \Drupal::service() in order to put off
* having to solve issues caused by container rebuilds during module install and
* configuration import.
*
* @internal
* This API is experimental.
*
* @todo https://www.drupal.org/i/3439717 Determine if there is a better to
* inject and re-inject services.
*/
final class RecipeRunner {
/**
* @param \Drupal\Core\Recipe\Recipe $recipe
* The recipe to apply.
*/
public static function processRecipe(Recipe $recipe): void {
static::processRecipes($recipe->recipes);
static::processInstall($recipe->install, $recipe->config->getConfigStorage());
static::processConfiguration($recipe);
static::processContent($recipe->content);
static::triggerEvent($recipe);
}
/**
* Triggers the RecipeAppliedEvent.
*
* @param \Drupal\Core\Recipe\Recipe $recipe
* The recipe to apply.
* @param array<mixed>|null $context
* The batch context if called by a batch.
*/
public static function triggerEvent(Recipe $recipe, ?array &$context = NULL): void {
$event = new RecipeAppliedEvent($recipe);
\Drupal::service(EventDispatcherInterface::class)->dispatch($event);
$context['message'] = t('Applied %recipe recipe.', ['%recipe' => $recipe->name]);
$context['results']['recipe'][] = $recipe->name;
}
/**
* Applies any recipes listed by the recipe.
*
* @param \Drupal\Core\Recipe\RecipeConfigurator $recipes
* The list of recipes to apply.
*/
protected static function processRecipes(RecipeConfigurator $recipes): void {
foreach ($recipes->recipes as $recipe) {
static::processRecipe($recipe);
}
}
/**
* Installs the extensions.
*
* @param \Drupal\Core\Recipe\InstallConfigurator $install
* The list of extensions to install.
* @param \Drupal\Core\Config\StorageInterface $recipeConfigStorage
* The recipe's configuration storage. Used to override extension provided
* configuration.
*/
protected static function processInstall(InstallConfigurator $install, StorageInterface $recipeConfigStorage): void {
foreach ($install->modules as $name) {
static::installModule($name, $recipeConfigStorage);
}
// Themes can depend on modules so have to be installed after modules.
foreach ($install->themes as $name) {
static::installTheme($name, $recipeConfigStorage);
}
}
/**
* Creates configuration and applies configuration actions.
*
* @param \Drupal\Core\Recipe\Recipe $recipe
* The recipe being applied.
*/
protected static function processConfiguration(Recipe $recipe): void {
/** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
$config_manager = \Drupal::service(ConfigManagerInterface::class);
$config_installer = new RecipeConfigInstaller(
\Drupal::service('config.factory'),
\Drupal::service('config.storage'),
\Drupal::service('config.typed'),
$config_manager,
\Drupal::service('event_dispatcher'),
NULL,
\Drupal::service('extension.path.resolver'));
$config = $recipe->config;
// Create configuration that is either supplied by the recipe or listed in
// the config.import section that does not exist.
$config_installer->installRecipeConfig($config);
if (!empty($config->config['actions'])) {
$values = $recipe->input->getValues();
// Wrap the replacement strings with `${` and `}`, which is a fairly
// common style of placeholder.
$keys = array_map(fn ($k) => sprintf('${%s}', $k), array_keys($values));
$replace = array_combine($keys, $values);
// Process the actions.
/** @var \Drupal\Core\Config\Action\ConfigActionManager $config_action_manager */
$config_action_manager = \Drupal::service('plugin.manager.config_action');
foreach ($config->config['actions'] as $config_name => $actions) {
// If this config name contains an input value, it must begin with the
// config prefix of a known entity type.
if (str_contains($config_name, '${') && empty($config_manager->getEntityTypeIdByName($config_name))) {
throw new ConfigActionException("The entity type for the config name '$config_name' could not be identified.");
}
$config_name = str_replace($keys, $replace, $config_name);
foreach ($actions as $action_id => $data) {
$config_action_manager->applyAction($action_id, $config_name, static::replaceInputValues($data, $replace));
}
}
}
}
/**
* Creates content contained in a recipe.
*
* @param \Drupal\Core\DefaultContent\Finder $content
* The content finder object for the recipe.
*/
protected static function processContent(Finder $content): void {
/** @var \Drupal\Core\DefaultContent\Importer $importer */
$importer = \Drupal::service(Importer::class);
$importer->setLogger(\Drupal::logger('recipe'));
$importer->importContent($content, Existing::Skip);
}
/**
* Converts a recipe into a series of batch operations.
*
* @param \Drupal\Core\Recipe\Recipe $recipe
* The recipe to convert to batch operations.
*
* @return array<int, array{0: callable, 1: array{mixed}}>
* The array of batch operations. Each value is an array with two values.
* The first value is a callable and the second value are the arguments to
* pass to the callable.
*
* @see \Drupal\Core\Batch\BatchBuilder::addOperation()
*/
public static function toBatchOperations(Recipe $recipe): array {
$modules = [];
$themes = [];
$recipes = [];
return static::toBatchOperationsRecipe($recipe, $recipes, $modules, $themes);
}
/**
* Helper method to convert a recipe to batch operations.
*
* @param \Drupal\Core\Recipe\Recipe $recipe
* The recipe to convert to batch operations.
* @param string[] $recipes
* The paths of the recipes that have already been converted to batch
* operations.
* @param string[] $modules
* The modules that will already be installed due to previous recipes in the
* batch.
* @param string[] $themes
* The themes that will already be installed due to previous recipes in the
* batch.
*
* @return array<int, array{0: callable, 1: array{mixed}}>
* The array of batch operations. Each value is an array with two values.
* The first value is a callable and the second value are the arguments to
* pass to the callable.
*/
protected static function toBatchOperationsRecipe(Recipe $recipe, array $recipes, array &$modules, array &$themes): array {
if (in_array($recipe->path, $recipes, TRUE)) {
return [];
}
$steps = [];
$recipes[] = $recipe->path;
foreach ($recipe->recipes->recipes as $sub_recipe) {
$steps = array_merge($steps, static::toBatchOperationsRecipe($sub_recipe, $recipes, $modules, $themes));
}
$steps = array_merge($steps, static::toBatchOperationsInstall($recipe, $modules, $themes));
if ($recipe->config->hasTasks()) {
$steps[] = [[RecipeRunner::class, 'installConfig'], [$recipe]];
}
if (!empty($recipe->content->data)) {
$steps[] = [[RecipeRunner::class, 'installContent'], [$recipe]];
}
$steps[] = [[RecipeRunner::class, 'triggerEvent'], [$recipe]];
return $steps;
}
/**
* Converts a recipe's install tasks to batch operations.
*
* @param \Drupal\Core\Recipe\Recipe $recipe
* The recipe to convert install tasks to batch operations.
* @param string[] $modules
* The modules that will already be installed due to previous recipes in the
* batch.
* @param string[] $themes
* The themes that will already be installed due to previous recipes in the
* batch.
*
* @return array<int, array{0: callable, 1: array{mixed}}>
* The array of batch operations. Each value is an array with two values.
* The first value is a callable and the second value are the arguments to
* pass to the callable.
*/
protected static function toBatchOperationsInstall(Recipe $recipe, array &$modules, array &$themes): array {
foreach ($recipe->install->modules as $name) {
if (in_array($name, $modules, TRUE)) {
continue;
}
$modules[] = $name;
$steps[] = [[RecipeRunner::class, 'installModule'], [$name, $recipe]];
}
foreach ($recipe->install->themes as $name) {
if (in_array($name, $themes, TRUE)) {
continue;
}
$themes[] = $name;
$steps[] = [[RecipeRunner::class, 'installTheme'], [$name, $recipe]];
}
return $steps ?? [];
}
/**
* Installs a module for a recipe.
*
* @param string $module
* The name of the module to install.
* @param \Drupal\Core\Config\StorageInterface|\Drupal\Core\Recipe\Recipe $recipeConfigStorage
* The recipe or recipe's config storage.
* @param array<mixed>|null $context
* The batch context if called by a batch.
*/
public static function installModule(string $module, StorageInterface|Recipe $recipeConfigStorage, ?array &$context = NULL): void {
if ($recipeConfigStorage instanceof Recipe) {
$recipeConfigStorage = $recipeConfigStorage->config->getConfigStorage();
}
// Disable configuration entity install but use the config directory from
// the module.
\Drupal::service('config.installer')->setSyncing(TRUE);
$default_install_path = \Drupal::service('extension.list.module')->get($module)->getPath() . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
// Allow the recipe to override simple configuration from the module.
$storage = new RecipeOverrideConfigStorage(
$recipeConfigStorage,
new FileStorage($default_install_path, StorageInterface::DEFAULT_COLLECTION)
);
\Drupal::service('config.installer')->setSourceStorage($storage);
\Drupal::service('module_installer')->install([$module]);
\Drupal::service('config.installer')->setSyncing(FALSE);
$context['message'] = t('Installed %module module.', ['%module' => \Drupal::service('extension.list.module')->getName($module)]);
$context['results']['module'][] = $module;
}
/**
* Installs a theme for a recipe.
*
* @param string $theme
* The name of the theme to install.
* @param \Drupal\Core\Config\StorageInterface|\Drupal\Core\Recipe\Recipe $recipeConfigStorage
* The recipe or recipe's config storage.
* @param array<mixed>|null $context
* The batch context if called by a batch.
*/
public static function installTheme(string $theme, StorageInterface|Recipe $recipeConfigStorage, ?array &$context = NULL): void {
if ($recipeConfigStorage instanceof Recipe) {
$recipeConfigStorage = $recipeConfigStorage->config->getConfigStorage();
}
// Disable configuration entity install.
\Drupal::service('config.installer')->setSyncing(TRUE);
$default_install_path = \Drupal::service('extension.list.theme')->get($theme)->getPath() . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
// Allow the recipe to override simple configuration from the theme.
$storage = new RecipeOverrideConfigStorage(
$recipeConfigStorage,
new FileStorage($default_install_path, StorageInterface::DEFAULT_COLLECTION)
);
\Drupal::service('config.installer')->setSourceStorage($storage);
\Drupal::service('theme_installer')->install([$theme]);
\Drupal::service('config.installer')->setSyncing(FALSE);
$context['message'] = t('Installed %theme theme.', ['%theme' => \Drupal::service('extension.list.theme')->getName($theme)]);
$context['results']['theme'][] = $theme;
}
/**
* Installs a config for a recipe.
*
* @param \Drupal\Core\Recipe\Recipe $recipe
* The recipe to install config for.
* @param array<mixed>|null $context
* The batch context if called by a batch.
*/
public static function installConfig(Recipe $recipe, ?array &$context = NULL): void {
static::processConfiguration($recipe);
$context['message'] = t('Installed configuration for %recipe recipe.', ['%recipe' => $recipe->name]);
$context['results']['config'][] = $recipe->name;
}
/**
* Installs a content for a recipe.
*
* @param \Drupal\Core\Recipe\Recipe $recipe
* The recipe to install content for.
* @param array<mixed>|null $context
* The batch context if called by a batch.
*/
public static function installContent(Recipe $recipe, ?array &$context = NULL): void {
static::processContent($recipe->content);
$context['message'] = t('Created content for %recipe recipe.', ['%recipe' => $recipe->name]);
$context['results']['content'][] = $recipe->name;
}
/**
* @param mixed $data
* The data that will have placeholders replaced.
* @param array<string, mixed> $replace
* An array whose keys are the placeholders to be replaced, and whose values
* are the replacements.
*
* @return mixed
* The passed data, with placeholders replaced.
*/
private static function replaceInputValues(mixed $data, array $replace): mixed {
if (is_string($data)) {
$data = str_replace(array_keys($replace), $replace, $data);
}
elseif (is_array($data)) {
foreach ($data as $key => $value) {
$data[$key] = static::replaceInputValues($value, $replace);
}
}
return $data;
}
}
|