|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Drush\Commands\core; |
| 6 | + |
| 7 | +use Drupal\Core\Config\ConfigFactoryInterface; |
| 8 | +use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; |
| 9 | +use Drush\Attributes as CLI; |
| 10 | +use Drush\Commands\AutowireTrait; |
| 11 | +use Drush\Commands\DrushCommands; |
| 12 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 13 | + |
| 14 | +final class ThemeDevCommands extends DrushCommands |
| 15 | +{ |
| 16 | + use AutowireTrait; |
| 17 | + |
| 18 | + const DEV = 'theme:dev'; |
| 19 | + |
| 20 | + public function __construct( |
| 21 | + // @todo Can we avoid the autowire attribute here? |
| 22 | + #[Autowire(service: 'keyvalue')] |
| 23 | + protected KeyValueFactoryInterface $keyValueFactory, |
| 24 | + protected ConfigFactoryInterface $configFactory |
| 25 | + ) { |
| 26 | + parent::__construct(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Toggle Twig development and cache aggregation settings. |
| 31 | + * |
| 32 | + * When enabled: |
| 33 | + * - Disables render cache, dynamic page cache, and page cache. |
| 34 | + * - Enables Twig debug mode (e.g., `dump()` function, template suggestions). |
| 35 | + * - Disables Twig cache (templates always recompiled). |
| 36 | + * - Disables CSS and JS aggregation. |
| 37 | + * |
| 38 | + * When disabled, restores default performance-oriented settings. |
| 39 | + * |
| 40 | + * Clears all Drupal caches to apply changes immediately. |
| 41 | + */ |
| 42 | + #[CLI\Command(name: self::DEV, aliases: ['thdev'])] |
| 43 | + #[CLI\Version(version: '13.6')] |
| 44 | + #[CLI\Argument(name: 'mode', description: '"on" or "off"', suggestedValues: ['on', 'off'])] |
| 45 | + #[CLI\Usage(name: 'drush theme:dev on', description: 'Disables CSS/JS aggregation and enables Twig debug settings.')] |
| 46 | + #[CLI\Usage(name: 'drush theme:dev off', description: 'Enables CSS/JS aggregation and disables Twig debug settings.')] |
| 47 | + public function toggleDevMode(string $mode): void |
| 48 | + { |
| 49 | + $devMode = match ($mode) { |
| 50 | + 'on' => true, |
| 51 | + 'off' => false, |
| 52 | + default => throw new \InvalidArgumentException("Invalid mode. Use 'on' or 'off'."), |
| 53 | + }; |
| 54 | + |
| 55 | + $this->keyValueFactory->get('development_settings')->setMultiple([ |
| 56 | + 'disable_rendered_output_cache_bins' => $devMode, |
| 57 | + 'twig_debug' => $devMode, |
| 58 | + 'twig_cache_disable' => $devMode, |
| 59 | + ]); |
| 60 | + |
| 61 | + $this->configFactory->getEditable('system.performance') |
| 62 | + ->set('css.preprocess', !$devMode) |
| 63 | + ->set('js.preprocess', !$devMode) |
| 64 | + ->save(); |
| 65 | + |
| 66 | + drupal_flush_all_caches(); |
| 67 | + |
| 68 | + $this->logger()->success(sprintf( |
| 69 | + 'Developer mode %s: CSS/JS aggregation %s, Twig debug settings %s.', |
| 70 | + $devMode ? 'enabled' : 'disabled', |
| 71 | + $devMode ? 'disabled' : 'enabled', |
| 72 | + $devMode ? 'enabled' : 'disabled' |
| 73 | + )); |
| 74 | + } |
| 75 | +} |
0 commit comments