You are here

ThemeCompilerController.php in Theme Compiler 8

Same filename and directory in other branches
  1. 2.0.x src/Controller/ThemeCompilerController.php

File

src/Controller/ThemeCompilerController.php
View source
<?php

namespace Drupal\theme_compiler\Controller;

use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\theme_compiler\Plugin\ThemeCompilerControllerInterface;
use Drupal\theme_compiler\Plugin\ThemeCompilerPluginManager;
use Drupal\theme_compiler\Plugin\ThemeCompilerTargetContext;

/**
 * Defines the theme compiler controller.
 */
class ThemeCompilerController extends ControllerBase implements ThemeCompilerControllerInterface {

  /**
   * The theme compiler plugin manager service provided by this module.
   *
   * @var \Drupal\theme_compiler\Plugin\ThemeCompilerPluginManager
   */
  protected $compilerPluginManager;

  /**
   * Drupal's configuration factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a ThemeCompilerController.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Drupal's configuration factory service.
   * @param \Drupal\theme_compiler\Plugin\ThemeCompilerPluginManager $compiler_plugin_manager
   *   The theme compiler plugin manager service provided by this module.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ThemeCompilerPluginManager $compiler_plugin_manager) {
    $this->compilerPluginManager = $compiler_plugin_manager;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function serve(ThemeCompilerTargetContext $context) {

    // Retrieve the compiler configured for this target context.
    $compiler = $context
      ->getCompiler();

    // Attempt to load the specified compiler plugin using the target context.
    if ($plugin = $this->compilerPluginManager
      ->createInstance($compiler)) {

      // Delegate this controller method to the compiler plugin.
      $response = $plugin
        ->serve($context);

      // Fetch the theme's configuration so that it can be added as a cacheable
      // dependency to the response.
      $global = $this->configFactory
        ->get('system.theme.global');
      $theme = $this->configFactory
        ->get("{$context->getTheme()}.settings");

      // Configure additional cacheability metadata common to all responses.
      $response
        ->getCacheableMetadata()
        ->addCacheableDependency($global)
        ->addCacheableDependency($theme)
        ->addCacheTags([
        'theme_compiler',
        "theme_compiler:{$context->getTheme()}",
        "theme_compiler:{$context->getTheme()}.{$context->getCompiler()}",
      ]);
      return $response;
    }

    // If the plugin can't be found, return an error code for this request.
    return new CacheableResponse('', CacheableResponse::HTTP_NOT_FOUND);
  }

}

Classes

Namesort descending Description
ThemeCompilerController Defines the theme compiler controller.