You are here

public function ThemeCompilerController::serve in Theme Compiler 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/ThemeCompilerController.php \Drupal\theme_compiler\Controller\ThemeCompilerController::serve()

Compiles and serves a response containing the requested compiler target.

If the request for the compiler target incurs a cache miss, this method will be called to generate a cacheable response. Otherwise, Drupal will serve the cached response until expiration or invalidation.

Optional parameters are defined entirely by each concrete plugin implementation and should be written as distinct parameters on this method.

Parameters

\Drupal\theme_compiler\Plugin\ThemeCompilerTargetContext $context: The compiler context containing common properties used by most (if not all) compilers.

Return value

\Drupal\Core\Cache\CacheableResponseInterface A cacheable response for the requested compiler target.

Overrides ThemeCompilerPluginInterface::serve

File

src/Controller/ThemeCompilerController.php, line 51

Class

ThemeCompilerController
Defines the theme compiler controller.

Namespace

Drupal\theme_compiler\Controller

Code

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);
}