75e4781b7f5573b33373bdc030447722ab5aec16
[yaffs-website] / vendor / drush / drush / src / Drupal / Commands / core / DrupalCommands.php
1 <?php
2 namespace Drush\Drupal\Commands\core;
3
4 use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
5 use Drupal\Core\CronInterface;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drush\Commands\DrushCommands;
8 use Drush\Drupal\DrupalUtil;
9 use Drush\Drush;
10 use Drush\Utils\StringUtils;
11 use Symfony\Component\Finder\Finder;
12 use Webmozart\PathUtil\Path;
13
14 class DrupalCommands extends DrushCommands
15 {
16
17     /**
18      * @var \Drupal\Core\CronInterface
19      */
20     protected $cron;
21
22     /**
23      * @var \Drupal\Core\Extension\ModuleHandlerInterface
24      */
25     protected $moduleHandler;
26
27     /**
28      * @return \Drupal\Core\CronInterface
29      */
30     public function getCron()
31     {
32         return $this->cron;
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\CronInterface $cron
45      */
46     public function __construct(CronInterface $cron, ModuleHandlerInterface $moduleHandler)
47     {
48         $this->cron = $cron;
49         $this->moduleHandler = $moduleHandler;
50     }
51
52     /**
53      * Run all cron hooks in all active modules for specified site.
54      *
55      * @command core:cron
56      * @aliases cron,core-cron
57      * @topics docs:cron
58      */
59     public function cron()
60     {
61         $result = $this->getCron()->run();
62         if (!$result) {
63             throw new \Exception(dt('Cron run failed.'));
64         }
65     }
66
67     /**
68      * Compile all Twig template(s).
69      *
70      * @command twig:compile
71      * @aliases twigc,twig-compile
72      */
73     public function twigCompile()
74     {
75         require_once DRUSH_DRUPAL_CORE . "/themes/engines/twig/twig.engine";
76         // Scan all enabled modules and themes.
77         $modules = array_keys($this->getModuleHandler()->getModuleList());
78         foreach ($modules as $module) {
79             $searchpaths[] = drupal_get_path('module', $module);
80         }
81
82         $themes = \Drupal::service('theme_handler')->listInfo();
83         foreach ($themes as $name => $theme) {
84             $searchpaths[] = $theme->getPath();
85         }
86
87         $files = Finder::create()
88             ->files()
89             ->name('*.html.twig')
90             ->exclude('tests')
91             ->in($searchpaths);
92         foreach ($files as $file) {
93             $relative = Path::makeRelative($file->getRealPath(), Drush::bootstrapManager()->getRoot());
94             // @todo Dynamically disable twig debugging since there is no good info there anyway.
95             twig_render_template($relative, ['theme_hook_original' => '']);
96             $this->logger()->success(dt('Compiled twig template !path', ['!path' => $relative]));
97         }
98     }
99
100     /**
101      * Information about things that may be wrong in your Drupal installation.
102      *
103      * @command core:requirements
104      * @option severity Only show status report messages with a severity greater than or equal to the specified value.
105      * @option ignore Comma-separated list of requirements to remove from output. Run with --format=yaml to see key values to use.
106      * @aliases status-report,rq,core-requirements
107      * @usage drush core:requirements
108      *   Show all status lines from the Status Report admin page.
109      * @usage drush core:requirements --severity=2
110      *   Show only the red lines from the Status Report admin page.
111      * @table-style default
112      * @field-labels
113      *   title: Title
114      *   severity: Severity
115      *   sid: SID
116      *   description: Description
117      *   value: Summary
118      * @default-fields title,severity,value
119      * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
120      */
121     public function requirements($options = ['format' => 'table', 'severity' => -1, 'ignore' => ''])
122     {
123         include_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
124         $severities = [
125             REQUIREMENT_INFO => dt('Info'),
126             REQUIREMENT_OK => dt('OK'),
127             REQUIREMENT_WARNING => dt('Warning'),
128             REQUIREMENT_ERROR => dt('Error'),
129         ];
130
131         drupal_load_updates();
132
133         $requirements = $this->getModuleHandler()->invokeAll('requirements', ['runtime']);
134         // If a module uses "$requirements[] = " instead of
135         // "$requirements['label'] = ", then build a label from
136         // the title.
137         foreach ($requirements as $key => $info) {
138             if (is_numeric($key)) {
139                 unset($requirements[$key]);
140                 $new_key = strtolower(str_replace(' ', '_', $info['title']));
141                 $requirements[$new_key] = $info;
142             }
143         }
144         $ignore_requirements = StringUtils::csvToArray($options['ignore']);
145         foreach ($ignore_requirements as $ignore) {
146             unset($requirements[$ignore]);
147         }
148         ksort($requirements);
149
150         $min_severity = $options['severity'];
151         $i = 0;
152         foreach ($requirements as $key => $info) {
153             $severity = array_key_exists('severity', $info) ? $info['severity'] : -1;
154             $rows[$i] = [
155                 'title' => (string) $info['title'],
156                 'value' => (string) $info['value'],
157                 'description' => DrupalUtil::drushRender($info['description']),
158                 'sid' => $severity,
159                 'severity' => @$severities[$severity]
160             ];
161             if ($severity < $min_severity) {
162                 unset($rows[$i]);
163             }
164             $i++;
165         }
166         $result = new RowsOfFields($rows);
167         return $result;
168     }
169 }