Commit afe5a85c authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3292275 by alexpott: Create Recipe class to read recipe.yml and...

Issue #3292275 by alexpott: Create Recipe class to read recipe.yml and composer.json to get information about a recipe
parent 12762b40
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Recipe;

class ConfigConfigurator {

  /**
   * @param array $config
   *   Config options for a recipe.
   */
  public function __construct(public readonly array $config) {
    // @todo https://www.drupal.org/project/distributions_recipes/issues/3292282
    // @todo https://www.drupal.org/project/distributions_recipes/issues/3292284
    // @todo https://www.drupal.org/project/distributions_recipes/issues/3292286
  }

}
+15 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Recipe;

class ContentConfigurator {

  /**
   * @param array $content
   *   Content options for a recipe.
   */
  public function __construct(public readonly array $content) {
    // @todo https://www.drupal.org/project/distributions_recipes/issues/3292287
  }

}
+18 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Recipe;

use Drupal\Component\Assertion\Inspector;

class InstallConfigurator {

  /**
   * @param string[] $extensions
   *   A list of extensions for a recipe to install.
   */
  public function __construct(public readonly array $extensions) {
    assert(Inspector::assertAllStrings($extensions), 'Extension names must be strings.');
    // @todo https://www.drupal.org/project/distributions_recipes/issues/3292281
  }

}
+46 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Recipe;

use Drupal\Core\Serialization\Yaml;

class Recipe {

  const COMPOSER_PROJECT_TYPE = 'drupal-recipe';

  public function __construct(
    public readonly string $name,
    public readonly string $description,
    public readonly string $type,
    public readonly InstallConfigurator $install,
    public readonly ConfigConfigurator $config,
    public readonly ContentConfigurator $content
  ) {
  }

  public static function createFromDirectory(string $path): static {
    if (!is_readable($path . '/recipe.yml')) {
      throw new RecipeFileException("There is no $path/recipe.yml file");
    }
    if (!is_readable($path . '/composer.json')) {
      throw new RecipeFileException("There is no $path/composer.json file");
    }

    $recipe_data = Yaml::decode(file_get_contents($path . '/recipe.yml')) + [
      'type' => '',
      'install' => [],
      'config' => [],
      'content' => [],
    ];

    $composer_data = json_decode(file_get_contents($path . '/composer.json'), TRUE);
    if (!isset($composer_data['type']) || $composer_data['type'] !== static::COMPOSER_PROJECT_TYPE) {
      throw new RecipeFileException("The composer project type must be: " . static::COMPOSER_PROJECT_TYPE);
    }
    $install = new InstallConfigurator($recipe_data['install']);
    $config = new ConfigConfigurator($recipe_data['config']);
    $content = new ContentConfigurator($recipe_data['content']);
    return new static($composer_data['name'], $composer_data['description'], $recipe_data['type'], $install, $config, $content);
  }

}
+7 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Recipe;

class RecipeFileException extends \RuntimeException {

}
Loading