Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Rest / DebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Rest\DebugCommand.
6  */
7
8 namespace Drupal\Console\Command\Rest;
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 Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Annotations\DrupalCommand;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Command\Shared\RestTrait;
19 use Drupal\rest\Plugin\Type\ResourcePluginManager;
20
21 /**
22  * @DrupalCommand(
23  *     extension = "rest",
24  *     extensionType = "module"
25  * )
26  */
27 class DebugCommand extends Command
28 {
29     use CommandTrait;
30     use RestTrait;
31
32
33     /**
34      * @var ResourcePluginManager $pluginManagerRest
35      */
36     protected $pluginManagerRest;
37
38     /**
39      * DebugCommand constructor.
40      *
41      * @param ResourcePluginManager $pluginManagerRest
42      */
43     public function __construct(ResourcePluginManager $pluginManagerRest)
44     {
45         $this->pluginManagerRest = $pluginManagerRest;
46         parent::__construct();
47     }
48
49     protected function configure()
50     {
51         $this
52             ->setName('rest:debug')
53             ->setDescription($this->trans('commands.rest.debug.description'))
54             ->addArgument(
55                 'resource-id',
56                 InputArgument::OPTIONAL,
57                 $this->trans('commands.rest.debug.arguments.resource-id')
58             )
59             ->addOption(
60                 'authorization',
61                 null,
62                 InputOption::VALUE_OPTIONAL,
63                 $this->trans('commands.rest.debug.options.status')
64             );
65     }
66
67     protected function execute(InputInterface $input, OutputInterface $output)
68     {
69         $io = new DrupalStyle($input, $output);
70
71         $resource_id = $input->getArgument('resource-id');
72         $status = $input->getOption('authorization');
73
74         if ($resource_id) {
75             $this->restDetail($io, $resource_id);
76         } else {
77             $this->restList($io, $status);
78         }
79
80         return 0;
81     }
82
83     private function restDetail(DrupalStyle $io, $resource_id)
84     {
85         $config = $this->getRestDrupalConfig();
86
87         $plugin = $this->pluginManagerRest->getInstance(['id' => $resource_id]);
88
89         if (empty($plugin)) {
90             $io->error(
91                 sprintf(
92                     $this->trans('commands.rest.debug.messages.not-found'),
93                     $resource_id
94                 )
95             );
96
97             return false;
98         }
99
100         $resource = $plugin->getPluginDefinition();
101
102         $configuration = [];
103         $configuration[] = [
104           $this->trans('commands.rest.debug.messages.id'),
105           $resource['id']
106         ];
107         $configuration[] = [
108           $this->trans('commands.rest.debug.messages.label'),
109           (string) $resource['label']
110         ];
111         $configuration[] = [
112           $this->trans('commands.rest.debug.messages.canonical_url'),
113           $resource['uri_paths']['canonical']
114         ];
115         $configuration[] = [
116           $this->trans('commands.rest.debug.messages.status'),
117           (isset($config[$resource['id']])) ? $this->trans('commands.rest.debug.messages.enabled') : $this->trans('commands.rest.debug.messages.disabled')];
118         $configuration[] = [
119           $this->trans(
120               sprintf(
121                   'commands.rest.debug.messages.provider',
122                   $resource['provider']
123               )
124           )
125         ];
126
127         $io->comment($resource_id);
128         $io->newLine();
129
130         $io->table([], $configuration, 'compact');
131
132         $tableHeader = [
133           $this->trans('commands.rest.debug.messages.rest-state'),
134           $this->trans('commands.rest.debug.messages.supported-formats'),
135           $this->trans('commands.rest.debug.messages.supported_auth'),
136         ];
137
138         $tableRows = [];
139         foreach ($config[$resource['id']] as $method => $settings) {
140             $tableRows[] = [
141               $method,
142               implode(', ', $settings['supported_formats']),
143               implode(', ', $settings['supported_auth']),
144             ];
145         }
146
147         $io->table($tableHeader, $tableRows);
148     }
149
150     protected function restList(DrupalStyle $io, $status)
151     {
152         $rest_resources = $this->getRestResources($status);
153
154         $tableHeader = [
155           $this->trans('commands.rest.debug.messages.id'),
156           $this->trans('commands.rest.debug.messages.label'),
157           $this->trans('commands.rest.debug.messages.canonical_url'),
158           $this->trans('commands.rest.debug.messages.status'),
159           $this->trans('commands.rest.debug.messages.provider'),
160         ];
161
162         $tableRows = [];
163         foreach ($rest_resources as $status => $resources) {
164             foreach ($resources as $id => $resource) {
165                 $tableRows[] =[
166                   $id,
167                   $resource['label'],
168                   $resource['uri_paths']['canonical'],
169                   $status,
170                   $resource['provider'],
171                 ];
172             }
173         }
174         $io->table($tableHeader, $tableRows, 'compact');
175     }
176 }