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