You are here

RouteHelper.php in Theme Compiler 8

Same filename and directory in other branches
  1. 2.0.x src/Routing/RouteHelper.php

File

src/Routing/RouteHelper.php
View source
<?php

namespace Drupal\theme_compiler\Routing;

use Drupal\Core\Discovery\YamlDiscovery;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\theme_compiler\Plugin\ThemeCompilerTargetContext;
use Symfony\Component\Routing\Route;

/**
 * Builds the dynamic routes provided by this theme.
 */
class RouteHelper {
  use StringTranslationTrait;
  const COMPILER_CONTROLLER = 'theme_compiler.controller:serve';

  /**
   * A YAML discovery instance to find 'theme_compiler' configuration.
   *
   * @var \Drupal\Core\Plugin\Discovery\YamlDiscovery
   */
  protected $discovery;

  /**
   * Constructs a new RouteHelper used for building dynamic compiler routes.
   *
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
   *   The theme handler service provided by Drupal.
   */
  public function __construct(ThemeHandlerInterface $theme_handler) {

    // Retrieve a list of file system paths to theme directories.
    $theme_directories = $theme_handler
      ->getThemeDirectories();

    // Create a new YAML discovery plugin for 'theme_compiler' configuration.
    $this->discovery = new YamlDiscovery('theme_compiler', $theme_directories);
  }

  /**
   * Generate a list of routes for a specific theme compiler's targets.
   *
   * @param string $theme
   *   The machine name of the theme for which routes should be generated.
   * @param string $compiler
   *   The machine name of the compiler for which routes should be generated.
   * @param array $targets
   *   An array of theme compiler target options keyed by a theme-relative
   *   target path.
   *
   * @return \Symfony\Component\Routing\Route[]
   *   A collection of routes for the provided theme compiler's targets.
   */
  protected function getThemeCompilerRoutes(string $theme, string $compiler, array $targets) {

    // Iterate over each target for this theme compiler for processing.
    foreach ($targets as $target => $options) {

      // Construct the theme compiler target context used for execution.
      $context = new ThemeCompilerTargetContext($compiler, $theme, $target, $options);

      // Build the route using the resulting context.
      $route = $this
        ->getThemeCompilerTargetRoute($context);

      // Generate a named array entry for this route.
      (yield "theme_compiler.{$context->getTargetId()}" => $route);
    }
  }

  /**
   * Generate a route to execute a specific theme compiler target.
   *
   * @param \Drupal\theme_compiler\Plugin\ThemeCompilerTargetContext $context
   *   The theme compiler context that contains common theme compiler route
   *   attributes and custom options necessary for execution.
   *
   * @return \Symfony\Component\Routing\Route
   *   A route to execute a specific theme compiler target.
   */
  protected function getThemeCompilerTargetRoute(ThemeCompilerTargetContext $context) {
    return new Route($context
      ->getTargetUri(), [
      '_controller' => self::COMPILER_CONTROLLER,
      'context' => $context,
    ], [
      '_access' => 'TRUE',
    ], [
      '_maintenance_access' => 'TRUE',
    ]);
  }

  /**
   * Generate a list of routes for a specific theme's compiler configuration.
   *
   * @param string $theme
   *   The machine name of the theme for which routes should be generated.
   * @param array $compilers
   *   An associative array of compiler target configurations keyed by the
   *   desired theme compiler's plugin identifier.
   *
   * @return \Symfony\Component\Routing\Route[]
   *   A collection of routes for the provided theme's compiler configuration.
   */
  protected function getThemeRoutes(string $theme, array $compilers) {
    foreach ($compilers as $compiler => $targets) {
      yield from $this
        ->getThemeCompilerRoutes($theme, $compiler, $targets);
    }
  }

  /**
   * Generate a list of routes for all applicable theme compiler configurations.
   *
   * @return \Symfony\Component\Routing\Route[]
   *   A collection of routes produced by this module.
   */
  public function routes() {
    foreach ($this->discovery
      ->findAll() as $theme => $compilers) {
      yield from $this
        ->getThemeRoutes($theme, $compilers);
    }
  }

}

Classes

Namesort descending Description
RouteHelper Builds the dynamic routes provided by this theme.