function GenerateTheme::loadStarterKitConfig

Same name in other branches
  1. 10 core/lib/Drupal/Core/Command/GenerateTheme.php \Drupal\Core\Command\GenerateTheme::loadStarterKitConfig()
1 call to GenerateTheme::loadStarterKitConfig()
GenerateTheme::execute in core/lib/Drupal/Core/Command/GenerateTheme.php

File

core/lib/Drupal/Core/Command/GenerateTheme.php, line 239

Class

GenerateTheme
Generates a new theme based on latest default markup.

Namespace

Drupal\Core\Command

Code

private static function loadStarterKitConfig(Extension $theme, string $version, string $name, string $description) : array {
    $starterkit_config_file = $theme->getPath() . '/' . $theme->getName() . '.starterkit.yml';
    if (!file_exists($starterkit_config_file)) {
        throw new \RuntimeException("Theme source theme {$theme->getName()} is not a valid starter kit.");
    }
    $starterkit_config_defaults = [
        'info' => [
            'name' => $name,
            'description' => $description,
            'core_version_requirement' => '^' . explode('.', \Drupal::VERSION)[0],
            'version' => '1.0.0',
            'generator' => "{$theme->getName()}:{$version}",
        ],
        'ignore' => [
            '/src/StarterKit.php',
            '/*.starterkit.yml',
        ],
        'no_edit' => [],
        'no_rename' => [],
    ];
    $starterkit_config = Yaml::decode(file_get_contents($starterkit_config_file));
    if (!is_array($starterkit_config)) {
        throw new \RuntimeException('Starterkit config is was not able to be parsed.');
    }
    if (!isset($starterkit_config['info'])) {
        $starterkit_config['info'] = [];
    }
    $starterkit_config['info'] = array_merge($starterkit_config_defaults['info'], $starterkit_config['info']);
    foreach ([
        'ignore',
        'no_edit',
        'no_rename',
    ] as $key) {
        if (!isset($starterkit_config[$key])) {
            $starterkit_config[$key] = $starterkit_config_defaults[$key];
        }
        if (!is_array($starterkit_config[$key])) {
            throw new \RuntimeException("{$key} in starterkit.yml must be an array");
        }
        $starterkit_config[$key] = array_map(static fn(string $path) => Glob::toRegex(trim($path, '/')), $starterkit_config[$key]);
        if (count($starterkit_config[$key]) > 0) {
            $files = self::createFilesFinder($theme->getPath())
                ->path($starterkit_config[$key]);
            $starterkit_config[$key] = array_map(static fn($file) => $file->getRelativePathname(), iterator_to_array($files));
            if (count($starterkit_config[$key]) === 0) {
                throw new \RuntimeException("Paths were defined `{$key}` but no files found.");
            }
        }
    }
    return $starterkit_config;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.