f427571b378d345e23b82b39850c80478f51194d
[yaffs-website] / vendor / drush / drush / lib / Drush / Boot / DrupalBoot8.php
1 <?php
2
3 namespace Drush\Boot;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\Response;
7 use Psr\Log\LoggerInterface;
8 use Drupal\Core\DrupalKernel;
9 use Drush\Drupal\DrupalKernel as DrushDrupalKernel;
10 use Drush\Drupal\DrushServiceModfier;
11 use Symfony\Component\DependencyInjection\Reference;
12
13 use Drush\Log\LogLevel;
14
15 class DrupalBoot8 extends DrupalBoot {
16
17   /**
18    * @var \Drupal\Core\DrupalKernelInterface
19    */
20   protected $kernel;
21
22   /**
23    * @var \Symfony\Component\HttpFoundation\Request
24    */
25   protected $request;
26
27   function valid_root($path) {
28     if (!empty($path) && is_dir($path) && file_exists($path . '/autoload.php')) {
29       // Additional check for the presence of core/composer.json to
30       // grant it is not a Drupal 7 site with a base folder named "core".
31       $candidate = 'core/includes/common.inc';
32       if (file_exists($path . '/' . $candidate) && file_exists($path . '/core/core.services.yml')) {
33         if (file_exists($path . '/core/misc/drupal.js') || file_exists($path . '/core/assets/js/drupal.js')) {
34           return $candidate;
35         }
36       }
37     }
38   }
39
40   function get_version($drupal_root) {
41     // Load the autoloader so we can access the class constants.
42     drush_drupal_load_autoloader($drupal_root);
43     // Drush depends on bootstrap being loaded at this point.
44     require_once $drupal_root .'/core/includes/bootstrap.inc';
45     if (defined('\Drupal::VERSION')) {
46       return \Drupal::VERSION;
47     }
48   }
49
50   function get_profile() {
51     return drupal_get_profile();
52   }
53
54   function conf_path($require_settings = TRUE, $reset = FALSE, Request $request = NULL) {
55     if (!isset($request)) {
56       if (\Drupal::hasRequest()) {
57         $request = \Drupal::request();
58       }
59       // @todo Remove once external CLI scripts (Drush) are updated.
60       else {
61         $request = Request::createFromGlobals();
62       }
63     }
64     if (\Drupal::hasService('kernel')) {
65       $site_path = \Drupal::service('kernel')->getSitePath();
66     }
67     if (!isset($site_path) || empty($site_path)) {
68       $site_path = DrupalKernel::findSitePath($request, $require_settings);
69     }
70     return $site_path;
71   }
72
73   function add_logger() {
74     // If we're running on Drupal 8 or later, we provide a logger which will send
75     // output to drush_log(). This should catch every message logged through every
76     // channel.
77     $container = \Drupal::getContainer();
78     $parser = $container->get('logger.log_message_parser');
79     $drushLogger = drush_get_context('DRUSH_LOG_CALLBACK');
80     $logger = new \Drush\Log\DrushLog($parser, $drushLogger);
81     $container->get('logger.factory')->addLogger($logger);
82   }
83
84   function contrib_modules_paths() {
85     return array(
86       $this->conf_path() . '/modules',
87       'sites/all/modules',
88       'modules',
89     );
90   }
91
92   /**
93    * @return array of strings - paths to directories where contrib
94    * themes can be found
95    */
96   function contrib_themes_paths() {
97     return array(
98       $this->conf_path() . '/themes',
99       'sites/all/themes',
100       'themes',
101     );
102   }
103
104   function bootstrap_drupal_core($drupal_root) {
105     $core = DRUPAL_ROOT . '/core';
106
107     return $core;
108   }
109
110   function bootstrap_drupal_database_validate() {
111     return parent::bootstrap_drupal_database_validate() && $this->bootstrap_drupal_database_has_table('key_value');
112   }
113
114   function bootstrap_drupal_database() {
115     // D8 omits this bootstrap level as nothing special needs to be done.
116     parent::bootstrap_drupal_database();
117   }
118
119   function bootstrap_drupal_configuration() {
120     $this->request = Request::createFromGlobals();
121     $classloader = drush_drupal_load_autoloader(DRUPAL_ROOT);
122     // @todo - use Request::create() and then no need to set PHP superglobals
123     $kernelClass = new \ReflectionClass('\Drupal\Core\DrupalKernel');
124     if ($kernelClass->hasMethod('addServiceModifier')) {
125       $this->kernel = DrupalKernel::createFromRequest($this->request, $classloader, 'prod');
126     }
127     else {
128       $this->kernel = DrushDrupalKernel::createFromRequest($this->request, $classloader, 'prod');
129     }
130     // @see Drush\Drupal\DrupalKernel::addServiceModifier()
131     $this->kernel->addServiceModifier(new DrushServiceModfier());
132
133     // Unset drupal error handler and restore Drush's one.
134     restore_error_handler();
135
136     // Disable automated cron if the module is enabled.
137     $GLOBALS['config']['automated_cron.settings']['interval'] = 0;
138
139     parent::bootstrap_drupal_configuration();
140   }
141
142   function bootstrap_drupal_full() {
143     drush_log(dt('About to bootstrap the Drupal 8 Kernel.'), LogLevel::DEBUG);
144     // TODO: do we need to do ob_start any longer?
145     if (!drush_get_context('DRUSH_QUIET', FALSE)) {
146       ob_start();
147     }
148     $this->kernel->invalidateContainer();
149     $this->kernel->boot();
150     $this->kernel->prepareLegacyRequest($this->request);
151     if (!drush_get_context('DRUSH_QUIET', FALSE)) {
152       ob_end_clean();
153     }
154     drush_log(dt('Finished bootstraping the Drupal 8 Kernel.'), LogLevel::DEBUG);
155
156     parent::bootstrap_drupal_full();
157
158     // Get a list of the modules to ignore
159     $ignored_modules = drush_get_option_list('ignored-modules', array());
160
161     // We have to get the service command list from the container, because
162     // it is constructed in an indirect way during the container initialization.
163     // The upshot is that the list of console commands is not available
164     // until after $kernel->boot() is called.
165     $container = \Drupal::getContainer();
166     $serviceCommandlist = $container->get('drush.service.consolecommands');
167     foreach ($serviceCommandlist->getCommandList() as $command) {
168       if (!$this->commandIgnored($command, $ignored_modules)) {
169         drush_log(dt('Add a command: !name', ['!name' => $command->getName()]), LogLevel::DEBUG);
170         annotationcommand_adapter_cache_module_console_commands($command);
171       }
172     }
173     // Do the same thing with the annotation commands.
174     $serviceCommandlist = $container->get('drush.service.consolidationcommands');
175     foreach ($serviceCommandlist->getCommandList() as $commandhandler) {
176       if (!$this->commandIgnored($commandhandler, $ignored_modules)) {
177         drush_log(dt('Add a commandhandler: !name', ['!name' => get_class($commandhandler)]), LogLevel::DEBUG);
178         annotationcommand_adapter_cache_module_service_commands($commandhandler);
179       }
180     }
181   }
182
183   public function commandIgnored($command, $ignored_modules) {
184     if (empty($ignored_modules)) {
185       return false;
186     }
187     $ignored_regex = '#\\\\(' . implode('|', $ignored_modules) . ')\\\\#';
188     $class = new \ReflectionClass($command);
189     $commandNamespace = $class->getNamespaceName();
190     return preg_match($ignored_regex, $commandNamespace);
191   }
192
193   /**
194    * {@inheritdoc}
195    */
196   public function terminate() {
197     parent::terminate();
198
199     if ($this->kernel) {
200       $response = Response::create('');
201       $this->kernel->terminate($this->request, $response);
202     }
203   }
204 }