Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Debug / ViewsCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Debug\ViewsCommand.
6  */
7
8 namespace Drupal\Console\Command\Debug;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Core\Command\Command;
15 use Drupal\views\Entity\View;
16 use Drupal\Core\Entity\EntityTypeManagerInterface;
17 use Drupal\Component\Plugin\PluginManagerInterface;
18 use Drupal\Console\Annotations\DrupalCommand;
19
20 /**
21  * Class ViewsCommand
22  *
23  * @DrupalCommand(
24  *     extension = "views",
25  *     extensionType = "module"
26  * )
27  *
28  * @package Drupal\Console\Command\Debug
29  */
30 class ViewsCommand extends Command
31 {
32     /**
33      * @var EntityTypeManagerInterface
34      */
35     protected $entityTypeManager;
36     /**
37      * @var PluginManagerInterface
38      */
39     protected $viewsDisplayManager;
40
41     /**
42      * DebugCommand constructor.
43      *
44      * @param EntityTypeManagerInterface $entityTypeManager
45      * @param PluginManagerInterface     $viewsDisplayManager
46      */
47     public function __construct(EntityTypeManagerInterface $entityTypeManager, PluginManagerInterface $viewsDisplayManager)
48     {
49         $this->entityTypeManager = $entityTypeManager;
50         $this->viewsDisplayManager = $viewsDisplayManager;
51         parent::__construct();
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     protected function configure()
58     {
59         $this
60             ->setName('debug:views')
61             ->setDescription($this->trans('commands.debug.views.description'))
62             ->addArgument(
63                 'view-id',
64                 InputArgument::OPTIONAL,
65                 $this->trans('commands.debug.views.arguments.view-id')
66             )
67             ->addOption(
68                 'tag',
69                 null,
70                 InputOption::VALUE_OPTIONAL,
71                 $this->trans('commands.debug.views.arguments.view-tag')
72             )->addOption(
73                 'status',
74                 null,
75                 InputOption::VALUE_OPTIONAL,
76                 $this->trans('commands.debug.views.arguments.view-status')
77             )
78             ->setAliases(['vde']);
79     }
80
81     /**
82      * {@inheritdoc}
83      */
84     protected function execute(InputInterface $input, OutputInterface $output)
85     {
86         $view_id = $input->getArgument('view-id');
87         $view_tag = $input->getOption('tag');
88         $view_status = $input->getOption('status');
89
90         if ($view_status == $this->trans('commands.common.status.enabled')) {
91             $view_status = 1;
92         } elseif ($view_status == $this->trans('commands.common.status.disabled')) {
93             $view_status = 0;
94         } else {
95             $view_status = -1;
96         }
97
98         if ($view_id) {
99             $this->viewDetail($view_id);
100         } else {
101             $this->viewList($view_tag, $view_status);
102         }
103     }
104
105
106     /**
107      * @param $view_id
108      * @return bool
109      */
110     private function viewDetail($view_id)
111     {
112         $view = $this->entityTypeManager->getStorage('view')->load($view_id);
113
114         if (empty($view)) {
115             $this->getIo()->error(sprintf($this->trans('commands.debug.views.messages.not-found'), $view_id));
116
117             return false;
118         }
119
120         $configuration = [];
121         $configuration [] = [$this->trans('commands.debug.views.messages.view-id'), $view->get('id')];
122         $configuration [] = [$this->trans('commands.debug.views.messages.view-name'), (string) $view->get('label')];
123         $configuration [] = [$this->trans('commands.debug.views.messages.tag'), $view->get('tag')];
124         $configuration [] = [$this->trans('commands.debug.views.messages.status'), $view->status() ? $this->trans('commands.common.status.enabled') : $this->trans('commands.common.status.disabled')];
125         $configuration [] = [$this->trans('commands.debug.views.messages.description'), $view->get('description')];
126
127         $this->getIo()->comment($view_id);
128
129         $this->getIo()->table([], $configuration);
130
131         $tableHeader = [
132           $this->trans('commands.debug.views.messages.display-id'),
133           $this->trans('commands.debug.views.messages.display-name'),
134           $this->trans('commands.debug.views.messages.display-description'),
135           $this->trans('commands.debug.views.messages.display-paths'),
136         ];
137         $displays = $this->viewDisplayList($view);
138
139         $this->getIo()->info(sprintf($this->trans('commands.debug.views.messages.display-list'), $view_id));
140
141         $tableRows = [];
142         foreach ($displays as $display_id => $display) {
143             $tableRows[] = [
144               $display_id,
145               $display['name'],
146               $display['description'],
147               $this->viewDisplayPaths($view, $display_id),
148             ];
149         }
150
151         $this->getIo()->table($tableHeader, $tableRows);
152     }
153
154     /**
155      * @param $tag
156      * @param $status
157      */
158     protected function viewList($tag, $status)
159     {
160         $views = $this->entityTypeManager->getStorage('view')->loadMultiple();
161
162         $tableHeader = [
163           $this->trans('commands.debug.views.messages.view-id'),
164           $this->trans('commands.debug.views.messages.view-name'),
165           $this->trans('commands.debug.views.messages.tag'),
166           $this->trans('commands.debug.views.messages.status'),
167           $this->trans('commands.debug.views.messages.path')
168         ];
169
170         $tableRows = [];
171         foreach ($views as $view) {
172             if ($status != -1 && $view->status() != $status) {
173                 continue;
174             }
175
176             if (isset($tag) && $view->get('tag') != $tag) {
177                 continue;
178             }
179             $tableRows[] = [
180               $view->get('id'),
181               $view->get('label'),
182               $view->get('tag'),
183               $view->status() ? $this->trans('commands.common.status.enabled') : $this->trans('commands.common.status.disabled'),
184               $this->viewDisplayPaths($view),
185             ];
186         }
187         $this->getIo()->table($tableHeader, $tableRows, 'compact');
188     }
189
190
191     /**
192      * @param \Drupal\views\Entity\View $view
193      * @param null                      $display_id
194      * @return string
195      */
196     protected function viewDisplayPaths(View $view, $display_id = null)
197     {
198         $all_paths = [];
199         $executable = $view->getExecutable();
200         $executable->initDisplay();
201         foreach ($executable->displayHandlers as $display) {
202             if ($display->hasPath()) {
203                 $path = $display->getPath();
204                 if (strpos($path, '%') === false) {
205                     //  @see Views should expect and store a leading /. See:
206                     //  https://www.drupal.org/node/2423913
207                     $all_paths[] = '/'.$path;
208                 } else {
209                     $all_paths[] = '/'.$path;
210                 }
211
212                 if ($display_id !== null && $display_id == $display->getBaseId()) {
213                     return '/'.$path;
214                 }
215             }
216         }
217
218         return implode(', ', array_unique($all_paths));
219     }
220
221     /**
222      * @param \Drupal\views\Entity\View $view
223      * @return array
224      */
225     protected function viewDisplayList(View $view)
226     {
227         $displays = [];
228         foreach ($view->get('display') as $display) {
229             $definition = $this->viewsDisplayManager->getDefinition($display['display_plugin']);
230             if (!empty($definition['admin'])) {
231                 // Cast the admin label to a string since it is an object.
232                 $displays[$definition['id']]['name'] = (string) $definition['admin'];
233                 $displays[$definition['id']]['description'] = (string) $definition['help'];
234             }
235         }
236         asort($displays);
237
238         return $displays;
239     }
240 }