You are here

class RouteHelper in Theme Compiler 8

Same name and namespace in other branches
  1. 2.0.x src/Routing/RouteHelper.php \Drupal\theme_compiler\Routing\RouteHelper

Builds the dynamic routes provided by this theme.

Hierarchy

Expanded class hierarchy of RouteHelper

1 string reference to 'RouteHelper'
theme_compiler.services.yml in ./theme_compiler.services.yml
theme_compiler.services.yml
1 service uses RouteHelper
theme_compiler.route_helper in ./theme_compiler.services.yml
Drupal\theme_compiler\Routing\RouteHelper

File

src/Routing/RouteHelper.php, line 16

Namespace

Drupal\theme_compiler\Routing
View source
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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteHelper::$discovery protected property A YAML discovery instance to find 'theme_compiler' configuration.
RouteHelper::COMPILER_CONTROLLER constant
RouteHelper::getThemeCompilerRoutes protected function Generate a list of routes for a specific theme compiler's targets.
RouteHelper::getThemeCompilerTargetRoute protected function Generate a route to execute a specific theme compiler target.
RouteHelper::getThemeRoutes protected function Generate a list of routes for a specific theme's compiler configuration.
RouteHelper::routes public function Generate a list of routes for all applicable theme compiler configurations.
RouteHelper::__construct public function Constructs a new RouteHelper used for building dynamic compiler routes.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.