/
TwigCommands.php
133 lines (120 loc) · 4.24 KB
/
TwigCommands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Drush\Drupal\Commands\core;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Template\TwigEnvironment;
use Drush\Commands\DrushCommands;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drush\Drush;
use Drush\Utils\StringUtils;
use Symfony\Component\Finder\Finder;
use Webmozart\PathUtil\Path;
class TwigCommands extends DrushCommands
{
/**
* @var \Drupal\Core\Template\TwigEnvironment
*/
protected $twig;
/**
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* @return \Drupal\Core\Template\TwigEnvironment
*/
public function getTwig()
{
return $this->twig;
}
/**
* @return \Drupal\Core\Extension\ModuleHandlerInterface
*/
public function getModuleHandler()
{
return $this->moduleHandler;
}
/**
* @param \Drupal\Core\Template\TwigEnvironment $twig
* @param ModuleHandlerInterface $moduleHandler
*/
public function __construct(TwigEnvironment $twig, ModuleHandlerInterface $moduleHandler)
{
$this->twig = $twig;
$this->moduleHandler = $moduleHandler;
}
/**
* Find potentially unused Twig templates.
*
* Immediately before running this command, crawl your entire web site. Or
* use your Production PHPStorage dir for comparison.
*
* @param $searchpaths A comma delimited list of paths to recursively search
* @usage drush twig:unused --field=template /var/www/mass.local/docroot/modules/custom,/var/www/mass.local/docroot/themes/custom
* Output a simple list of potentially unused templates.
* @table-style default
* @field-labels
* template: Template
* compiled: Compiled
* @default-fields template,compiled
* @filter-output
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
*
* @command twig:unused
*/
public function unused($searchpaths)
{
$unused = [];
$phpstorage = PhpStorageFactory::get('twig');
// Find all templates in the codebase.
$files = Finder::create()
->files()
->name('*.html.twig')
->exclude('tests')
->in(StringUtils::csvToArray($searchpaths));
$this->logger()->notice(dt('Found !count templates', ['!count' => count($files)]));
// Check to see if a compiled equivalent exists in PHPStorage
foreach ($files as $file) {
$relative = Path::makeRelative($file->getRealPath(), Drush::bootstrapManager()->getRoot());
$mainCls = $this->getTwig()->getTemplateClass($relative);
$key = $this->getTwig()->getCache()->generateKey($relative, $mainCls);
if (!$phpstorage->exists($key)) {
$unused[$key] = [
'template' => $relative,
'compiled' => $key,
];
}
}
$this->logger()->notice(dt('Found !count unused', ['!count' => count($unused)]));
return new RowsOfFields($unused);
}
/**
* Compile all Twig template(s).
*
* @command twig:compile
* @aliases twigc,twig-compile
*/
public function twigCompile()
{
require_once DRUSH_DRUPAL_CORE . "/themes/engines/twig/twig.engine";
// Scan all enabled modules and themes.
$modules = array_keys($this->getModuleHandler()->getModuleList());
foreach ($modules as $module) {
$searchpaths[] = drupal_get_path('module', $module);
}
$themes = \Drupal::service('theme_handler')->listInfo();
foreach ($themes as $name => $theme) {
$searchpaths[] = $theme->getPath();
}
$files = Finder::create()
->files()
->name('*.html.twig')
->exclude('tests')
->in($searchpaths);
foreach ($files as $file) {
$relative = Path::makeRelative($file->getRealPath(), Drush::bootstrapManager()->getRoot());
// @todo Dynamically disable twig debugging since there is no good info there anyway.
twig_render_template($relative, ['theme_hook_original' => '']);
$this->logger()->success(dt('Compiled twig template !path', ['!path' => $relative]));
}
}
}