Yaffs site version 1.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         // Make sure all modules are loaded.
120         $this->container->get('module_handler')->loadAll();
121
122         $io = new DrupalStyle($input, $output);
123
124         $systemData = $this->getSystemData();
125         $connectionData = $this->getConnectionData();
126         $themeInfo = $this->getThemeData();
127         $directoryData = $this->getDirectoryData();
128
129         $siteData = array_merge(
130             $systemData,
131             $connectionData,
132             $themeInfo,
133             $directoryData
134         );
135
136         $format = $input->getOption('format');
137
138         if ('table' === $format) {
139             $this->showDataAsTable($io, $siteData);
140         }
141
142         if ('json' === $format) {
143             $output->writeln(json_encode($siteData, JSON_PRETTY_PRINT));
144         }
145     }
146
147     protected function getSystemData()
148     {
149         if (!$this->systemManager) {
150             return [];
151         }
152
153         $requirements = $this->systemManager->listRequirements();
154         $systemData = [];
155
156         foreach ($requirements as $key => $requirement) {
157             if ($requirement['title'] instanceof \Drupal\Core\StringTranslation\TranslatableMarkup) {
158                 $title = $requirement['title']->render();
159             } else {
160                 $title = $requirement['title'];
161             }
162
163             $systemData['system'][$title] = strip_tags($requirement['value']);
164         }
165
166         if ($this->settings) {
167             try {
168                 $hashSalt = $this->settings->getHashSalt();
169             } catch (\Exception $e) {
170                 $hashSalt = '';
171             }
172             $systemData['system'][$this->trans('commands.site.status.messages.hash_salt')] = $hashSalt;
173             $systemData['system'][$this->trans('commands.site.status.messages.console')] = $this->getApplication()->getVersion();
174         }
175
176         return $systemData;
177     }
178
179     protected function getConnectionData()
180     {
181         $connectionInfo = Database::getConnectionInfo();
182
183         $connectionData = [];
184         foreach ($this->connectionInfoKeys as $connectionInfoKey) {
185             if ("password" == $connectionInfoKey) {
186                 continue;
187             }
188
189             $connectionKey = $this->trans('commands.site.status.messages.'.$connectionInfoKey);
190             $connectionData['database'][$connectionKey] = $connectionInfo['default'][$connectionInfoKey];
191         }
192
193         $connectionData['database'][$this->trans('commands.site.status.messages.connection')] = sprintf(
194             '%s//%s:%s@%s%s/%s',
195             $connectionInfo['default']['driver'],
196             $connectionInfo['default']['username'],
197             $connectionInfo['default']['password'],
198             $connectionInfo['default']['host'],
199             $connectionInfo['default']['port'] ? ':'.$connectionInfo['default']['port'] : '',
200             $connectionInfo['default']['database']
201         );
202
203         return $connectionData;
204     }
205
206     protected function getThemeData()
207     {
208         $config = $this->configFactory->get('system.theme');
209
210         return [
211           'theme' => [
212             'theme_default' => $config->get('default'),
213             'theme_admin' => $config->get('admin'),
214           ],
215         ];
216     }
217
218     protected function getDirectoryData()
219     {
220         $systemTheme = $this->configFactory->get('system.theme');
221
222         $themeDefaultDirectory = '';
223         $themeAdminDirectory = '';
224         try {
225             $themeDefault = $this->themeHandler->getTheme(
226                 $systemTheme->get('default')
227             );
228             $themeDefaultDirectory = sprintf('/%s', $themeDefault->getpath());
229
230             $themeAdmin = $this->themeHandler->getTheme(
231                 $systemTheme->get('admin')
232             );
233             $themeAdminDirectory = sprintf('/%s', $themeAdmin->getpath());
234         } catch (\Exception $e) {
235         }
236
237         $systemFile = $this->configFactory->get('system.file');
238
239         return [
240           'directory' => [
241             $this->trans('commands.site.status.messages.directory_root') => $this->appRoot,
242             $this->trans('commands.site.status.messages.directory_temporary') => $systemFile->get('path.temporary'),
243             $this->trans('commands.site.status.messages.directory_theme_default') => $themeDefaultDirectory,
244             $this->trans('commands.site.status.messages.directory_theme_admin') => $themeAdminDirectory,
245           ],
246         ];
247     }
248
249     protected function showDataAsTable(DrupalStyle $io, $siteData)
250     {
251         if (empty($siteData)) {
252             return [];
253         }
254         $io->newLine();
255         foreach ($this->groups as $group) {
256             $tableRows = [];
257             $groupData = $siteData[$group];
258             $io->comment($this->trans('commands.site.status.messages.'.$group));
259
260             foreach ($groupData as $key => $item) {
261                 $tableRows[] = [$key, $item];
262             }
263
264             $io->table([], $tableRows, 'compact');
265         }
266     }
267 }