Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / drush / drush / src / Boot / DrupalBoot8.php
1 <?php
2
3 namespace Drush\Boot;
4
5 use Consolidation\AnnotatedCommand\AnnotationData;
6 use Drush\Log\DrushLog;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
9 use Drupal\Core\DrupalKernel;
10 use Drush\Drush;
11 use Drush\Drupal\DrushServiceModifier;
12
13 use Drush\Log\LogLevel;
14
15 class DrupalBoot8 extends DrupalBoot implements AutoloaderAwareInterface
16 {
17     use AutoloaderAwareTrait;
18
19     /**
20      * @var \Drupal\Core\DrupalKernelInterface
21      */
22     protected $kernel;
23
24     /**
25      * @var \Symfony\Component\HttpFoundation\Request
26      */
27     protected $request;
28
29     /**
30      * @return \Symfony\Component\HttpFoundation\Request
31      */
32     public function getRequest()
33     {
34         return $this->request;
35     }
36
37     /**
38      * @param \Symfony\Component\HttpFoundation\Request $request
39      */
40     public function setRequest($request)
41     {
42         $this->request = $request;
43     }
44
45     /**
46      * @return \Drupal\Core\DrupalKernelInterface
47      */
48     public function getKernel()
49     {
50         return $this->kernel;
51     }
52
53     public function validRoot($path)
54     {
55         if (!empty($path) && is_dir($path) && file_exists($path . '/autoload.php')) {
56             // Additional check for the presence of core/composer.json to
57             // grant it is not a Drupal 7 site with a base folder named "core".
58             $candidate = 'core/includes/common.inc';
59             if (file_exists($path . '/' . $candidate) && file_exists($path . '/core/core.services.yml')) {
60                 if (file_exists($path . '/core/misc/drupal.js') || file_exists($path . '/core/assets/js/drupal.js')) {
61                     return $candidate;
62                 }
63             }
64         }
65     }
66
67     public function getVersion($drupal_root)
68     {
69         // Are the class constants available?
70         if (!$this->hasAutoloader()) {
71             throw new \Exception('Cannot access Drupal 8 class constants - Drupal autoloader not loaded yet.');
72         }
73         // Drush depends on bootstrap being loaded at this point.
74         require_once $drupal_root .'/core/includes/bootstrap.inc';
75         if (defined('\Drupal::VERSION')) {
76             return \Drupal::VERSION;
77         }
78     }
79
80     public function confPath($require_settings = true, $reset = false)
81     {
82
83         if (\Drupal::hasService('kernel')) {
84             $site_path = \Drupal::service('kernel')->getSitePath();
85         }
86         if (!isset($site_path) || empty($site_path)) {
87             $site_path = DrupalKernel::findSitePath($this->getRequest(), $require_settings);
88         }
89         return $site_path;
90     }
91
92     public function addLogger()
93     {
94         // Provide a logger which sends
95         // output to drush_log(). This should catch every message logged through every
96         // channel.
97         $container = \Drupal::getContainer();
98         $parser = $container->get('logger.log_message_parser');
99
100         $drushLogger = Drush::logger();
101         $logger = new DrushLog($parser, $drushLogger);
102         $container->get('logger.factory')->addLogger($logger);
103     }
104
105     public function bootstrapDrupalCore($drupal_root)
106     {
107         $core = DRUPAL_ROOT . '/core';
108
109         return $core;
110     }
111
112     public function bootstrapDrupalSiteValidate()
113     {
114         parent::bootstrapDrupalSiteValidate();
115         // Account for users who omit the http:// prefix.
116         if (!parse_url($this->uri, PHP_URL_SCHEME)) {
117             $this->uri = 'http://' . $this->uri;
118         }
119         $request = Request::create($this->uri, 'GET', [], [], [], ['SCRIPT_NAME' => '/index.php']);
120         $this->setRequest($request);
121         $confPath = drush_bootstrap_value('confPath', $this->confPath(true, true));
122         drush_bootstrap_value('site', $request->getHttpHost());
123         return true;
124     }
125
126     public function bootstrapDrupalConfigurationValidate()
127     {
128         $conf_file = $this->confPath() . '/settings.php';
129         if (!file_exists($conf_file)) {
130             $msg = dt("Could not find a Drupal settings.php file at !file.", ['!file' => $conf_file]);
131             $this->logger->debug($msg);
132             // Cant do this because site:install deliberately bootstraps to configure without a settings.php file.
133             // return drush_set_error($msg);
134         }
135         return true;
136     }
137
138     public function bootstrapDrupalDatabaseValidate()
139     {
140         return parent::bootstrapDrupalDatabaseValidate() && $this->bootstrapDrupalDatabaseHasTable('key_value');
141     }
142
143     public function bootstrapDrupalDatabase()
144     {
145         // D8 omits this bootstrap level as nothing special needs to be done.
146         parent::bootstrapDrupalDatabase();
147     }
148
149     public function bootstrapDrupalConfiguration(AnnotationData $annotationData = null)
150     {
151         // Default to the standard kernel.
152         $kernel = Kernels::DRUPAL;
153         if (!empty($annotationData)) {
154             $kernel = $annotationData->get('kernel', Kernels::DRUPAL);
155         }
156         $classloader = $this->autoloader();
157         $request = $this->getRequest();
158         $kernel_factory = Kernels::getKernelFactory($kernel);
159         /** @var \Drupal\Core\DrupalKernelInterface kernel */
160         $this->kernel = $kernel_factory($request, $classloader, 'prod');
161         // Include Drush services in the container.
162         // @see Drush\Drupal\DrupalKernel::addServiceModifier()
163         $this->kernel->addServiceModifier(new DrushServiceModifier());
164
165         // Unset drupal error handler and restore Drush's one.
166         restore_error_handler();
167
168         // Disable automated cron if the module is enabled.
169         $GLOBALS['config']['automated_cron.settings']['interval'] = 0;
170
171         parent::bootstrapDrupalConfiguration();
172     }
173
174     public function bootstrapDrupalFull()
175     {
176         $this->logger->debug(dt('Start bootstrap of the Drupal Kernel.'));
177         $this->kernel->boot();
178         $this->kernel->prepareLegacyRequest($this->getRequest());
179         $this->logger->debug(dt('Finished bootstrap of the Drupal Kernel.'));
180
181         parent::bootstrapDrupalFull();
182         $this->addLogger();
183
184         // Get a list of the modules to ignore
185         $ignored_modules = drush_get_option_list('ignored-modules', []);
186
187         $application = Drush::getApplication();
188         $runner = Drush::runner();
189
190         // We have to get the service command list from the container, because
191         // it is constructed in an indirect way during the container initialization.
192         // The upshot is that the list of console commands is not available
193         // until after $kernel->boot() is called.
194         $container = \Drupal::getContainer();
195
196         // Set the command info alterers.
197         if ($container->has(DrushServiceModifier::DRUSH_COMMAND_INFO_ALTERER_SERVICES)) {
198             $serviceCommandInfoAltererlist = $container->get(DrushServiceModifier::DRUSH_COMMAND_INFO_ALTERER_SERVICES);
199             $commandFactory = Drush::commandFactory();
200             foreach ($serviceCommandInfoAltererlist->getCommandList() as $altererHandler) {
201                 $commandFactory->addCommandInfoAlterer($altererHandler);
202                 $this->logger->debug(dt('Commands are potentially altered in !class.', ['!class' => get_class($altererHandler)]));
203             }
204         }
205
206         $serviceCommandlist = $container->get(DrushServiceModifier::DRUSH_CONSOLE_SERVICES);
207         if ($container->has(DrushServiceModifier::DRUSH_CONSOLE_SERVICES)) {
208             foreach ($serviceCommandlist->getCommandList() as $command) {
209                 if (!$this->commandIgnored($command, $ignored_modules)) {
210                     $this->inflect($command);
211                     $this->logger->log(LogLevel::DEBUG_NOTIFY, dt('Add a command: !name', ['!name' => $command->getName()]));
212                     $application->add($command);
213                 }
214             }
215         }
216         // Do the same thing with the annotation commands.
217         if ($container->has(DrushServiceModifier::DRUSH_COMMAND_SERVICES)) {
218             $serviceCommandlist = $container->get(DrushServiceModifier::DRUSH_COMMAND_SERVICES);
219             foreach ($serviceCommandlist->getCommandList() as $commandHandler) {
220                 if (!$this->commandIgnored($commandHandler, $ignored_modules)) {
221                     $this->inflect($commandHandler);
222                     $this->logger->log(LogLevel::DEBUG_NOTIFY, dt('Add a commandfile class: !name', ['!name' => get_class($commandHandler)]));
223                     $runner->registerCommandClass($application, $commandHandler);
224                 }
225             }
226         }
227     }
228
229     public function commandIgnored($command, $ignored_modules)
230     {
231         if (empty($ignored_modules)) {
232             return false;
233         }
234         $ignored_regex = '#\\\\(' . implode('|', $ignored_modules) . ')\\\\#';
235         $class = new \ReflectionClass($command);
236         $commandNamespace = $class->getNamespaceName();
237         return preg_match($ignored_regex, $commandNamespace);
238     }
239
240     /**
241      * {@inheritdoc}
242      */
243     public function terminate()
244     {
245         parent::terminate();
246
247         if ($this->kernel) {
248             $response = Response::create('');
249             $this->kernel->terminate($this->getRequest(), $response);
250         }
251     }
252 }