b1776756255a45e16d7b9395b805a50b61c11840
[yaffs-website] / vendor / drupal / console / src / Bootstrap / Drupal.php
1 <?php
2
3 namespace Drupal\Console\Bootstrap;
4
5 use Doctrine\Common\Annotations\AnnotationRegistry;
6 use Symfony\Component\Console\Input\ArrayInput;
7 use Symfony\Component\Console\Output\ConsoleOutput;
8 use Symfony\Component\HttpFoundation\Request;
9 use Drupal\Console\Core\Style\DrupalStyle;
10 use Drupal\Console\Core\Utils\ArgvInputReader;
11 use Drupal\Console\Core\Bootstrap\DrupalConsoleCore;
12 use Drupal\Console\Utils\ExtendExtensionManager;
13
14 class Drupal
15 {
16     protected $autoload;
17     protected $root;
18     protected $appRoot;
19
20     /**
21      * Drupal constructor.
22      *
23      * @param $autoload
24      * @param $root
25      * @param $appRoot
26      */
27     public function __construct($autoload, $root, $appRoot)
28     {
29         $this->autoload = $autoload;
30         $this->root = $root;
31         $this->appRoot = $appRoot;
32     }
33
34     public function boot($debug)
35     {
36         $output = new ConsoleOutput();
37         $input = new ArrayInput([]);
38         $io = new DrupalStyle($input, $output);
39         $argvInputReader = new ArgvInputReader();
40
41         if (!class_exists('Drupal\Core\DrupalKernel')) {
42             $io->error('Class Drupal\Core\DrupalKernel do not exists.');
43             $drupal = new DrupalConsoleCore($this->root, $this->appRoot);
44             return $drupal->boot();
45         }
46
47         try {
48             // Add support for Acquia Dev Desktop sites.
49             // Try both Mac and Windows home locations.
50             $home = getenv('HOME');
51             if (empty($home)) {
52                 $home = getenv('USERPROFILE');
53             }
54             if (!empty($home)) {
55                 $devDesktopSettingsDir = $home . "/.acquia/DevDesktop/DrupalSettings";
56                 if (file_exists($devDesktopSettingsDir)) {
57                     $_SERVER['DEVDESKTOP_DRUPAL_SETTINGS_DIR'] = $devDesktopSettingsDir;
58                 }
59             }
60             $argvInputReader = new ArgvInputReader();
61             $command = $argvInputReader->get('command');
62             $rebuildServicesFile = false;
63             if ($command=='cache:rebuild' || $command=='cr') {
64                 $rebuildServicesFile = true;
65             }
66
67             if ($debug) {
68                 $io->writeln('➤ Creating request');
69             }
70             $uri = $argvInputReader->get('uri');
71             if ($uri && $uri != 'http://default') {
72                 if (substr($uri, -1) != '/') {
73                     $uri .= '/';
74                 }
75                 $uri .= 'index.php';
76                 $request = Request::create($uri, 'GET', [], [], [], ['SCRIPT_NAME' => $this->appRoot . '/index.php']);
77             } else {
78                 $request = Request::createFromGlobals();
79             }
80
81             if ($debug) {
82                 $io->writeln("\r\033[K\033[1A\r<info>✔</info>");
83                 $io->writeln('➤ Creating Drupal kernel');
84             }
85             $drupalKernel = DrupalKernel::createFromRequest(
86                 $request,
87                 $this->autoload,
88                 'prod',
89                 false,
90                 $this->appRoot
91             );
92             if ($debug) {
93                 $io->writeln("\r\033[K\033[1A\r<info>✔</info>");
94                 $io->writeln('➤ Registering dynamic services');
95             }
96
97             $drupalKernel->addServiceModifier(
98                 new DrupalServiceModifier(
99                     $this->root,
100                     $this->appRoot,
101                     'drupal.command',
102                     'drupal.generator',
103                     $rebuildServicesFile
104                 )
105             );
106             if ($debug) {
107                 $io->writeln("\r\033[K\033[1A\r<info>✔</info>");
108                 $io->writeln('➤ Rebuilding container');
109             }
110             $drupalKernel->invalidateContainer();
111             $drupalKernel->rebuildContainer();
112             $drupalKernel->boot();
113
114             if ($debug) {
115                 $io->writeln("\r\033[K\033[1A\r<info>✔</info>");
116             }
117
118             $container = $drupalKernel->getContainer();
119             $container->set('console.root', $this->root);
120
121             AnnotationRegistry::registerLoader([$this->autoload, "loadClass"]);
122
123             $configuration = $container->get('console.configuration_manager')
124                 ->getConfiguration();
125
126             $container->get('console.translator_manager')
127                 ->loadCoreLanguage(
128                     $configuration->get('application.language'),
129                     $this->root
130                 );
131
132             $consoleExtendConfigFile = $this->root . DRUPAL_CONSOLE .'/extend.console.config.yml';
133             if (file_exists($consoleExtendConfigFile)) {
134                 $container->get('console.configuration_manager')
135                     ->importConfigurationFile($consoleExtendConfigFile);
136             }
137
138             $container->get('console.renderer')
139                 ->setSkeletonDirs(
140                     [
141                         $this->root.DRUPAL_CONSOLE.'/templates/',
142                         $this->root.DRUPAL_CONSOLE_CORE.'/templates/'
143                     ]
144                 );
145
146             return $container;
147         } catch (\Exception $e) {
148             if ($argvInputReader->get('command') == 'list') {
149                 $io->error($e->getMessage());
150             }
151             $drupal = new DrupalConsoleCore($this->root, $this->appRoot);
152             $container = $drupal->boot();
153             $container->set('class_loader', $this->autoload);
154             return $container;
155         }
156     }
157 }