Version 1
[yaffs-website] / vendor / drupal / console / src / Command / Site / StatusCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Site\StatusCommand.
6  */
7
8 namespace Drupal\Console\Command\Site;
9
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Core\Database\Database;
15 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
17 use Drupal\system\SystemManager;
18 use Drupal\Core\Site\Settings;
19 use Drupal\Core\Config\ConfigFactory;
20 use Drupal\Core\Extension\ThemeHandler;
21
22 /**
23  *  This command provides a report of the current drupal installation.
24  *
25  *  @category site
26  */
27 class StatusCommand extends Command
28 {
29     use ContainerAwareCommandTrait;
30
31     /* @var $connectionInfoKeys array */
32     protected $connectionInfoKeys = [
33       'driver',
34       'host',
35       'database',
36       'port',
37       'username',
38       'password',
39     ];
40
41     protected $groups = [
42       'system',
43       'database',
44       'theme',
45       'directory',
46     ];
47
48     /**
49      * @var SystemManager
50      */
51     protected $systemManager;
52
53     /**
54      * @var Settings
55      */
56     protected $settings;
57
58     /**
59      * @var ConfigFactory
60      */
61     protected $configFactory;
62
63     /**
64      * @var ThemeHandler
65      */
66     protected $themeHandler;
67
68     /**
69      * @var string
70      */
71     protected $appRoot;
72
73     /**
74      * DebugCommand constructor.
75      *
76      * @param SystemManager $systemManager
77      * @param Settings      $settings
78      * @param ConfigFactory $configFactory
79      * @param ThemeHandler  $themeHandler
80      * @param $appRoot
81      */
82     public function __construct(
83         SystemManager $systemManager,
84         Settings $settings,
85         ConfigFactory $configFactory,
86         ThemeHandler $themeHandler,
87         $appRoot
88     ) {
89         $this->systemManager = $systemManager;
90         $this->settings = $settings;
91         $this->configFactory = $configFactory;
92         $this->themeHandler = $themeHandler;
93         $this->appRoot = $appRoot;
94         parent::__construct();
95     }
96
97     /**
98      * {@inheritdoc}
99      */
100     protected function configure()
101     {
102         $this
103             ->setName('site:status')
104             ->setDescription($this->trans('commands.site.status.description'))
105             ->addOption(
106                 'format',
107                 null,
108                 InputOption::VALUE_OPTIONAL,
109                 $this->trans('commands.site.status.options.format'),
110                 'table'
111             );
112     }
113
114     /**
115      * {@inheritdoc}
116      */
117     protected function execute(InputInterface $input, OutputInterface $output)
118     {
119         $io = new DrupalStyle($input, $output);
120
121         $systemData = $this->getSystemData();
122         $connectionData = $this->getConnectionData();
123         $themeInfo = $this->getThemeData();
124         $directoryData = $this->getDirectoryData();
125
126         $siteData = array_merge(
127             $systemData,
128             $connectionData,
129             $themeInfo,
130             $directoryData
131         );
132
133         $format = $input->getOption('format');
134
135         if ('table' === $format) {
136             $this->showDataAsTable($io, $siteData);
137         }
138
139         if ('json' === $format) {
140             $output->writeln(json_encode($siteData, JSON_PRETTY_PRINT));
141         }
142     }
143
144     protected function getSystemData()
145     {
146         if (!$this->systemManager) {
147             return [];
148         }
149
150         $requirements = $this->systemManager->listRequirements();
151         $systemData = [];
152
153         foreach ($requirements as $key => $requirement) {
154             if ($requirement['title'] instanceof \Drupal\Core\StringTranslation\TranslatableMarkup) {
155                 $title = $requirement['title']->render();
156             } else {
157                 $title = $requirement['title'];
158             }
159
160             $systemData['system'][$title] = strip_tags($requirement['value']);
161         }
162
163         if ($this->settings) {
164             try {
165                 $hashSalt = $this->settings->getHashSalt();
166             } catch (\Exception $e) {
167                 $hashSalt = '';
168             }
169             $systemData['system'][$this->trans('commands.site.status.messages.hash_salt')] = $hashSalt;
170             $systemData['system'][$this->trans('commands.site.status.messages.console')] = $this->getApplication()->getVersion();
171         }
172
173         return $systemData;
174     }
175
176     protected function getConnectionData()
177     {
178         $connectionInfo = Database::getConnectionInfo();
179
180         $connectionData = [];
181         foreach ($this->connectionInfoKeys as $connectionInfoKey) {
182             if ("password" == $connectionInfoKey) {
183                 continue;
184             }
185
186             $connectionKey = $this->trans('commands.site.status.messages.'.$connectionInfoKey);
187             $connectionData['database'][$connectionKey] = $connectionInfo['default'][$connectionInfoKey];
188         }
189
190         $connectionData['database'][$this->trans('commands.site.status.messages.connection')] = sprintf(
191             '%s//%s:%s@%s%s/%s',
192             $connectionInfo['default']['driver'],
193             $connectionInfo['default']['username'],
194             $connectionInfo['default']['password'],
195             $connectionInfo['default']['host'],
196             $connectionInfo['default']['port'] ? ':'.$connectionInfo['default']['port'] : '',
197             $connectionInfo['default']['database']
198         );
199
200         return $connectionData;
201     }
202
203     protected function getThemeData()
204     {
205         $config = $this->configFactory->get('system.theme');
206
207         return [
208           'theme' => [
209             'theme_default' => $config->get('default'),
210             'theme_admin' => $config->get('admin'),
211           ],
212         ];
213     }
214
215     protected function getDirectoryData()
216     {
217         $systemTheme = $this->configFactory->get('system.theme');
218
219         $themeDefaultDirectory = '';
220         $themeAdminDirectory = '';
221         try {
222             $themeDefault = $this->themeHandler->getTheme(
223                 $systemTheme->get('default')
224             );
225             $themeDefaultDirectory = sprintf('/%s', $themeDefault->getpath());
226
227             $themeAdmin = $this->themeHandler->getTheme(
228                 $systemTheme->get('admin')
229             );
230             $themeAdminDirectory = sprintf('/%s', $themeAdmin->getpath());
231         } catch (\Exception $e) {
232         }
233
234         $systemFile = $this->configFactory->get('system.file');
235
236         return [
237           'directory' => [
238             $this->trans('commands.site.status.messages.directory_root') => $this->appRoot,
239             $this->trans('commands.site.status.messages.directory_temporary') => $systemFile->get('path.temporary'),
240             $this->trans('commands.site.status.messages.directory_theme_default') => $themeDefaultDirectory,
241             $this->trans('commands.site.status.messages.directory_theme_admin') => $themeAdminDirectory,
242           ],
243         ];
244     }
245
246     protected function showDataAsTable(DrupalStyle $io, $siteData)
247     {
248         if (empty($siteData)) {
249             return [];
250         }
251         $io->newLine();
252         foreach ($this->groups as $group) {
253             $tableRows = [];
254             $groupData = $siteData[$group];
255             $io->comment($this->trans('commands.site.status.messages.'.$group));
256
257             foreach ($groupData as $key => $item) {
258                 $tableRows[] = [$key, $item];
259             }
260
261             $io->table([], $tableRows, 'compact');
262         }
263     }
264 }