You are here

public function ThemeCompilerController::serve in Theme Compiler 2.0.x

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

Serve a theme compiler asset as a cacheable response.

For more information about how to alter the asset response.

Parameters

\Drupal\compiler\CompilerContextInterface $theme_compiler_context: A compiler context used to define a compilation.

Return value

\Drupal\Core\Cache\CacheableResponse A cacheable response for the requested resource.

Overrides ThemeCompilerControllerInterface::serve

See also

hook_theme_compiler_asset_alter()

File

src/Controller/ThemeCompilerController.php, line 81

Class

ThemeCompilerController
Defines the controller used to compile assets provided by themes.

Namespace

Drupal\theme_compiler\Controller

Code

public function serve(CompilerContextInterface $theme_compiler_context) : CacheableResponse {

  // The default response should be HTTP 404 (Not Found).
  $response = new CacheableResponse('', CacheableResponse::HTTP_NOT_FOUND);
  try {
    $metadata = $theme_compiler_context
      ->getOption('theme_compiler');
    if (!is_array($metadata) || !array_key_exists('theme', $metadata) || !array_key_exists('path', $metadata)) {
      throw new \InvalidArgumentException('The compiler context is missing the required theme compiler metadata');
    }

    // Prepare the asset path for retrieval from the file system.
    $target = $this->compiler
      ->normalizeAndResolveTargetPath("{$metadata['theme']}/{$metadata['path']}");

    // Ensure that the asset could be read from the file system.
    if (($asset = @file_get_contents($target)) !== FALSE) {

      // Assume that the file content retrieval went according to plan.
      $status = CacheableResponse::HTTP_OK;
      if (strlen($asset) === 0) {

        // If the file is empty, status '204 No Content' is more appropriate.
        $status = CacheableResponse::HTTP_NO_CONTENT;
      }

      // Replace the original response with a new one containing the contents.
      $response = new CacheableResponse($asset, $status, [
        'Content-Type' => self::DEFAULT_MIME_TYPE,
      ]);
    }
  } catch (\Exception $previous) {
    watchdog_exception('theme_compiler', $previous);
  }

  // Define the default cacheability for this response.
  $hash = hash('sha384', $target);
  $cacheability = new CacheableMetadata();
  $cacheability
    ->addCacheTags([
    'theme_compiler_asset',
    "theme_compiler_asset:{$hash}",
  ]);

  // Set the initial cacheability for this response.
  $response
    ->addCacheableDependency($cacheability);

  // Allow modules & themes to alter the compiler response.
  $this->moduleHandler
    ->alter([
    'theme_compiler_response',
    "theme_compiler_response_{$theme_compiler_context->getCompiler()}",
  ], $response);
  $this->themeManager
    ->alter([
    'theme_compiler_response',
    "theme_compiler_response_{$theme_compiler_context->getCompiler()}",
  ], $response);
  return $response;
}