You are here

protected function RouteHelper::getThemeCompilerContext in Theme Compiler 2.0.x

Construct a compiler context from a theme, compiler, path, & config.

The resulting compiler context will be serialized and stored as a default route parameter value. Theme-relative file paths will be resolved before being stored.

Parameters

string $theme: The machine name of the theme containing the context.

string $compiler: The machine name of the desired compiler plugin to use.

string $path: A theme-relative path used to build the target route that will be used to serve the result of the configured compilation.

array $config: Configuration values for the target route. The following keys are used directly by this module:

  • 'files': an array exclusively containing file path strings (required)
  • 'options': an array of options to pass to the compiler
  • 'data': miscellaneous user-defined data to pass to the compiler

Keys that do not appear in this list will be ignored.

Return value

\Drupal\compiler\RefineableCompilerContext A compiler context used to define a compilation.

1 call to RouteHelper::getThemeCompilerContext()
RouteHelper::getThemeCompilerRoutes in src/Routing/RouteHelper.php
Generate a list of routes for a specific compiler's targets.

File

src/Routing/RouteHelper.php, line 75

Class

RouteHelper
Builds the route(s) that facilitate compilation of theme-provided assets.

Namespace

Drupal\theme_compiler\Routing

Code

protected function getThemeCompilerContext(string $theme, string $compiler, string $path, array $config) : RefineableCompilerContext {
  if (empty($files = $config['files'] ?? []) || !is_array($files) || $files !== array_filter($files, 'is_string')) {
    throw new \InvalidArgumentException('"files" is required to be a non-empty array that exclusively contains strings of file paths');
  }
  if (!is_array($options = $config['options'] ?? [])) {
    throw new \InvalidArgumentException('"options" can either be undefined or an array');
  }

  // Store the theme name and the theme-relative target path as options.
  $options['theme_compiler']['path'] = $path;
  $options['theme_compiler']['theme'] = $theme;

  // Compute the target URI for this context and hash it for an ID.
  $options['theme_compiler']['uri'] = '/' . drupal_get_path('theme', $theme) . '/' . $path;
  $options['theme_compiler']['id'] = hash('sha384', $options['theme_compiler']['uri']);

  // Iterate over each defined theme-relative file path for processing.
  foreach ($files as $index => $file) {
    if (empty($result = realpath(\DRUPAL_ROOT . '/' . drupal_get_path('theme', $theme) . '/' . $file))) {
      throw new \InvalidArgumentException('Unable to resolve theme-relative file path at index ' . var_export($index, TRUE) . ': ' . var_export($file, TRUE));
    }
    $inputs[] = new CompilerInputFile($result);
  }
  $context = new RefineableCompilerContext($compiler, $options, $inputs ?? [], $config['data'] ?? NULL);
  return $context;
}