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