class ThemeCompilerController in Theme Compiler 2.0.x
Same name and namespace in other branches
- 8 src/Controller/ThemeCompilerController.php \Drupal\theme_compiler\Controller\ThemeCompilerController
Defines the controller used to compile assets provided by themes.
Copyright (C) 2021 Library Solutions, LLC (et al.).
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
@internal
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\theme_compiler\Controller\ThemeCompilerController implements ThemeCompilerControllerInterface
Expanded class hierarchy of ThemeCompilerController
1 string reference to 'ThemeCompilerController'
1 service uses ThemeCompilerController
File
- src/
Controller/ ThemeCompilerController.php, line 28
Namespace
Drupal\theme_compiler\ControllerView source
class ThemeCompilerController extends ControllerBase implements ThemeCompilerControllerInterface {
const DEFAULT_MIME_TYPE = 'application/octet-stream';
/**
* The compiler service.
*
* @var \Drupal\theme_compiler\Compiler
*/
protected $compiler;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The theme manager service.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface
*/
protected $themeManager;
/**
* Constructs a ThemeCompilerController object.
*
* @param \Drupal\theme_compiler\Compiler $compiler
* The compiler service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
* The theme manager service.
*/
public function __construct(Compiler $compiler, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager) {
$this->compiler = $compiler;
$this->moduleHandler = $module_handler;
$this->themeManager = $theme_manager;
}
/**
* Serve a theme compiler asset as a cacheable response.
*
* @param \Drupal\compiler\CompilerContextInterface $theme_compiler_context
* A compiler context used to define a compilation.
*
* @see hook_theme_compiler_asset_alter()
* For more information about how to alter the asset response.
*
* @return \Drupal\Core\Cache\CacheableResponse
* A cacheable response for the requested resource.
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
46 |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
ThemeCompilerController:: |
protected | property | The compiler service. | |
ThemeCompilerController:: |
protected | property |
The module handler service. Overrides ControllerBase:: |
|
ThemeCompilerController:: |
protected | property | The theme manager service. | |
ThemeCompilerController:: |
constant | |||
ThemeCompilerController:: |
public | function |
Serve a theme compiler asset as a cacheable response. Overrides ThemeCompilerControllerInterface:: |
|
ThemeCompilerController:: |
public | function | Constructs a ThemeCompilerController object. |