Skip to content

Commit 570a05d

Browse files
Keshav2207KPL22weitzman
authored
Command to toogle Twig debugging and CSS/JS aggregation (#6268)
* Command to toogle Twig debugging and CSS/JS aggregation * Code style fixes * Changed command alias * Renamed file and code improvements * Fix CLI version error * Add autowire * remove twig:debug. it is broken anyway * Use match --------- Co-authored-by: KPL22 <[email protected]> Co-authored-by: Moshe Weitzman <[email protected]>
1 parent dff90b2 commit 570a05d

File tree

3 files changed

+84
-31
lines changed

3 files changed

+84
-31
lines changed

src/Commands/LegacyCommands.php

+9
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,13 @@ public function secphp(): void
4343
public function sec(): void
4444
{
4545
}
46+
47+
/**
48+
* twig:debug has been removed. Please use the `theme:dev` command.
49+
*/
50+
#[CLI\Command(name: 'twig:debug', aliases: ['twig-debug'])]
51+
#[CLI\Obsolete]
52+
public function twigDebug(): void
53+
{
54+
}
4655
}
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}

src/Commands/core/TwigCommands.php

-31
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
namespace Drush\Commands\core;
66

77
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
8-
use Drupal\Core\DrupalKernelInterface;
98
use Drupal\Core\Extension\ModuleExtensionList;
109
use Drupal\Core\Extension\ModuleHandlerInterface;
1110
use Drupal\Core\PhpStorage\PhpStorageFactory;
12-
use Drupal\Core\State\StateInterface;
1311
use Drupal\Core\Template\TwigEnvironment;
1412
use Drush\Attributes as CLI;
1513
use Drush\Commands\AutowireTrait;
@@ -25,14 +23,11 @@ final class TwigCommands extends DrushCommands
2523

2624
const UNUSED = 'twig:unused';
2725
const COMPILE = 'twig:compile';
28-
const DEBUG = 'twig:debug';
2926

3027
public function __construct(
3128
protected TwigEnvironment $twig,
3229
protected ModuleHandlerInterface $moduleHandler,
3330
private readonly ModuleExtensionList $extensionList,
34-
private readonly StateInterface $state,
35-
private readonly DrupalKernelInterface $kernel
3631
) {
3732
}
3833

@@ -112,30 +107,4 @@ public function twigCompile(): void
112107
$this->logger()->success(dt('Compiled twig template !path', ['!path' => $relative]));
113108
}
114109
}
115-
116-
/**
117-
* Enables Twig debug and disables caching Twig templates.
118-
*
119-
* @see \Drupal\system\Form\DevelopmentSettingsForm::submitForm()
120-
*/
121-
#[CLI\Command(name: self::DEBUG, aliases: ['twig-debug'])]
122-
#[CLI\Argument(name: 'mode', description: 'Debug mode. Recognized values: <info>on</info>, <info>off</info>.', suggestedValues: ['on', 'off'])]
123-
#[CLI\Version(version: '12.1')]
124-
public function twigDebug(string $mode): void
125-
{
126-
$mode = match ($mode) {
127-
'on' => true,
128-
'off' => false,
129-
default => throw new \Exception('Twig debug mode must be either "on" or "off".'),
130-
};
131-
$twig_development = [
132-
'twig_debug' => $mode,
133-
'twig_cache_disable' => $mode,
134-
];
135-
$this->state->setMultiple($twig_development);
136-
$this->kernel->invalidateContainer();
137-
$this->io()->success(
138-
dt('{operation} twig debug.', ['operation' => $mode ? 'Enabled' : 'Disabled']),
139-
);
140-
}
141110
}

0 commit comments

Comments
 (0)