Skip to content
Snippets Groups Projects
Commit 538e9b09 authored by Adam G-H's avatar Adam G-H Committed by Jürgen Haas
Browse files

Issue #3501480 by phenaproxima, jurgenhaas: Create an action that can apply a...

Issue #3501480 by phenaproxima, jurgenhaas: Create an action that can apply a recipe, to enable self-reapplying recipes :smiling_imp:
parent dbf56854
No related branches found
Tags 8.x-1.11
1 merge request!463Allow Composer package name resolution
Pipeline #403505 passed with warnings
......@@ -2,6 +2,7 @@
namespace Drupal\eca_misc\Plugin\Action;
use Composer\InstalledVersions;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Recipe\Recipe;
......@@ -21,17 +22,38 @@ use Drupal\eca\Plugin\Action\ConfigurableActionBase;
*/
class ApplyRecipe extends ConfigurableActionBase {
/**
* Returns the path of the recipe pointed to by configuration.
*
* @return string|null
* Returns a string if the configured recipe Composer package name (after
* token replacement) is an existing directory path. If the path is empty,
* does not exist, or cannot be mapped to an installed package, NULL is
* returned.
*/
private function getRecipePath(): ?string {
$package_name = $this->tokenService->replace($this->configuration['recipe_package_name']);
// If the package name is the name of an installed Composer-managed package,
// resolve its installed path. `InstalledVersions` is part of Composer's
// runtime API and is available as soon as the autoloader is, which is the
// first thing Drupal loads when it boots up. This does not run the Composer
// executable or any Composer commands.
if ($package_name !== '' && InstalledVersions::isInstalled($package_name)) {
$path = InstalledVersions::getInstallPath($package_name);
return trim(rtrim($path, '/'));
}
// The package_name isn't anything we can work with; give up.
return NULL;
}
/**
* {@inheritdoc}
*/
public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE) {
$result = AccessResult::allowed();
$path = $this->tokenService->replace($this->configuration['recipe_path']);
if ($path === '') {
$result = AccessResult::forbidden('Path is empty.');
}
elseif (!is_dir($path)) {
$result = AccessResult::forbidden('Path does not exist.');
if ($this->getRecipePath() === NULL) {
$result = AccessResult::forbidden('The configured package name is invalid.');
}
return $return_as_object ? $result : $result->isAllowed();
}
......@@ -40,9 +62,7 @@ class ApplyRecipe extends ConfigurableActionBase {
* {@inheritdoc}
*/
public function execute(): void {
$path = trim($this->tokenService->replace($this->configuration['recipe_path']));
$path = trim(rtrim($path, '/'));
$recipe = Recipe::createFromDirectory($path);
$recipe = Recipe::createFromDirectory($this->getRecipePath());
RecipeRunner::processRecipe($recipe);
}
......@@ -51,7 +71,7 @@ class ApplyRecipe extends ConfigurableActionBase {
*/
public function defaultConfiguration(): array {
return [
'recipe_path' => '',
'recipe_package_name' => '',
] + parent::defaultConfiguration();
}
......@@ -59,10 +79,11 @@ class ApplyRecipe extends ConfigurableActionBase {
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form['recipe_path'] = [
$form['recipe_package_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Recipe path'),
'#default_value' => $this->configuration['recipe_path'],
'#title' => $this->t('Recipe package name'),
'#description' => $this->t('The Composer package name of the recipe, that should be applied, e.g. "drupal/drupal_cms_privacy_basic"'),
'#default_value' => $this->configuration['recipe_package_name'],
'#required' => TRUE,
'#eca_token_replacement' => TRUE,
];
......@@ -73,7 +94,7 @@ class ApplyRecipe extends ConfigurableActionBase {
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
$this->configuration['recipe_path'] = $form_state->getValue('recipe_path');
$this->configuration['recipe_package_name'] = $form_state->getValue('recipe_package_name');
parent::submitConfigurationForm($form, $form_state);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment