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