Version 1
[yaffs-website] / vendor / drupal / console / src / Utils / Site.php
1 <?php
2
3 namespace Drupal\Console\Utils;
4
5 use Symfony\Component\DependencyInjection\Reference;
6 use Symfony\Component\Filesystem\Filesystem;
7 use Symfony\Component\Finder\Finder;
8 use Drupal\Core\DependencyInjection\ContainerBuilder;
9 use Drupal\Core\Logger\LoggerChannelFactory;
10 use Drupal\Core\Language\LanguageManager;
11 use Drupal\Core\Language\Language;
12 use Drupal\Core\Site\Settings;
13 use Drupal\Console\Core\Utils\ConfigurationManager;
14
15 class Site
16 {
17     /**
18      * @var string
19      */
20     protected $appRoot;
21
22     /**
23      * @var ConfigurationManager
24      */
25     protected $configurationManager;
26
27     /**
28      * @var string
29      */
30     protected $cacheDirectory;
31
32     /**
33      * Site constructor.
34      *
35      * @param string               $appRoot
36      * @param ConfigurationManager $configurationManager
37      */
38     public function __construct(
39         $appRoot,
40         ConfigurationManager $configurationManager
41     )
42     {
43         $this->appRoot = $appRoot;
44         $this->configurationManager = $configurationManager;
45     }
46
47     public function loadLegacyFile($legacyFile, $relative = true)
48     {
49         if ($relative) {
50             $legacyFile = realpath(
51                 sprintf('%s/%s', $this->appRoot, $legacyFile)
52             );
53         }
54
55         if (file_exists($legacyFile)) {
56             include_once $legacyFile;
57
58             return true;
59         }
60
61         return false;
62     }
63
64     /**
65      * @return array
66      */
67     public function getStandardLanguages()
68     {
69         $standardLanguages = LanguageManager::getStandardLanguageList();
70         $languages = [];
71         foreach ($standardLanguages as $langcode => $standardLanguage) {
72             $languages[$langcode] = $standardLanguage[0];
73         }
74
75         return $languages;
76     }
77
78     /**
79      * @return array
80      */
81     public function getDatabaseTypes()
82     {
83         $this->loadLegacyFile('/core/includes/install.inc');
84         $this->setMinimalContainerPreKernel();
85
86         $driverDirectories = [
87             $this->appRoot . '/core/lib/Drupal/Core/Database/Driver',
88             $this->appRoot . '/drivers/lib/Drupal/Driver/Database'
89         ];
90
91         $driverDirectories = array_filter(
92             $driverDirectories,
93             function ($directory) {
94                 return is_dir($directory);
95             }
96         );
97
98         $finder = new Finder();
99         $finder->directories()
100             ->in($driverDirectories)
101             ->depth('== 0');
102
103         $databases = [];
104         foreach ($finder as $driver_folder) {
105             if (file_exists($driver_folder->getRealpath() . '/Install/Tasks.php')) {
106                 $driver  = $driver_folder->getBasename();
107                 $installer = db_installer_object($driver);
108                 // Verify is database is installable
109                 if ($installer->installable()) {
110                     $reflection = new \ReflectionClass($installer);
111                     $install_namespace = $reflection->getNamespaceName();
112                     // Cut the trailing \Install from namespace.
113                     $driver_class = substr($install_namespace, 0, strrpos($install_namespace, '\\'));
114                     $databases[$driver] = ['namespace' => $driver_class, 'name' =>$installer->name()];
115                 }
116             }
117         }
118
119         return $databases;
120     }
121
122     protected function setMinimalContainerPreKernel()
123     {
124         // Create a minimal mocked container to support calls to t() in the pre-kernel
125         // base system verification code paths below. The strings are not actually
126         // used or output for these calls.
127         $container = new ContainerBuilder();
128         $container->setParameter('language.default_values', Language::$defaultValues);
129         $container
130             ->register('language.default', 'Drupal\Core\Language\LanguageDefault')
131             ->addArgument('%language.default_values%');
132         $container
133             ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
134             ->addArgument(new Reference('language.default'));
135
136         // Register the stream wrapper manager.
137         $container
138             ->register('stream_wrapper_manager', 'Drupal\Core\StreamWrapper\StreamWrapperManager')
139             ->addMethodCall('setContainer', [new Reference('service_container')]);
140         $container
141             ->register('file_system', 'Drupal\Core\File\FileSystem')
142             ->addArgument(new Reference('stream_wrapper_manager'))
143             ->addArgument(Settings::getInstance())
144             ->addArgument((new LoggerChannelFactory())->get('file'));
145
146         \Drupal::setContainer($container);
147     }
148
149     public function getDatabaseTypeDriver($driver)
150     {
151         // We cannot use Database::getConnection->getDriverClass() here, because
152         // the connection object is not yet functional.
153         $task_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Install\\Tasks";
154         if (class_exists($task_class)) {
155             return new $task_class();
156         } else {
157             $task_class = "Drupal\\Driver\\Database\\{$driver}\\Install\\Tasks";
158             return new $task_class();
159         }
160     }
161
162     /**
163      * @return mixed
164      */
165     public function getAutoload()
166     {
167         $autoLoadFile = $this->appRoot.'/autoload.php';
168
169         return include $autoLoadFile;
170     }
171
172     /**
173      * @return boolean
174      */
175     public function multisiteMode($uri)
176     {
177         if ($uri != 'default') {
178             return true;
179         }
180
181         return false;
182     }
183
184     /**
185      * @return boolean
186      */
187     public function validMultisite($uri)
188     {
189         $multiSiteFile = sprintf(
190             '%s/sites/sites.php',
191             $this->appRoot
192         );
193
194         if (file_exists($multiSiteFile)) {
195             include $multiSiteFile;
196         } else {
197             return false;
198         }
199
200         if (isset($sites[$uri]) && is_dir($this->appRoot . "/sites/" . $sites[$uri])) {
201             return true;
202         }
203
204         return false;
205     }
206
207     public function getCacheDirectory()
208     {
209         if ($this->cacheDirectory) {
210             return $this->cacheDirectory;
211         }
212
213         $configFactory = \Drupal::configFactory();
214         $siteId = $configFactory->get('system.site')
215             ->get('uuid');
216         $pathTemporary = $configFactory->get('system.file')
217             ->get('path.temporary');
218         $configuration = $this->configurationManager->getConfiguration();
219         $cacheDirectory = $configuration->get('application.cache.directory')?:'';
220         if ($cacheDirectory) {
221             if (strpos($cacheDirectory, '/') != 0) {
222                 $cacheDirectory = $this->configurationManager
223                         ->getApplicationDirectory() . '/' . $cacheDirectory;
224             }
225             $cacheDirectories[] = $cacheDirectory . '/' . $siteId . '/';
226         }
227         $cacheDirectories[] = sprintf(
228             '%s/cache/%s/',
229             $this->configurationManager->getConsoleDirectory(),
230             $siteId
231         );
232         $cacheDirectories[] = $pathTemporary . '/console/cache/' . $siteId . '/';
233
234         foreach ($cacheDirectories as $cacheDirectory) {
235             if ($this->isValidDirectory($cacheDirectory)) {
236                 $this->cacheDirectory = $cacheDirectory;
237                 break;
238             }
239         }
240
241         return $this->cacheDirectory;
242     }
243
244     private function isValidDirectory($path)
245     {
246         $fileSystem = new Filesystem();
247         if ($fileSystem->exists($path)) {
248             return true;
249         }
250         try {
251             $fileSystem->mkdir($path);
252
253             return true;
254         } catch (\Exception $e) {
255             return false;
256         }
257     }
258 }