forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestDebugCommand.php
More file actions
126 lines (103 loc) · 4.43 KB
/
Copy pathRestDebugCommand.php
File metadata and controls
126 lines (103 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\RestDebugCommand.
*/
namespace Drupal\AppConsole\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Component\Serialization\Yaml;
class RestDebugCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('rest:debug')
->setDescription($this->trans('commands.rest.debug.description'))
->addArgument('resource-id', InputArgument::OPTIONAL,
$this->trans('commands.rest.debug.arguments.resource-id'))
->addOption('autorization', '', InputOption::VALUE_OPTIONAL,
$this->trans('commands.rest.debug.options.status'));
$this->addDependency('rest');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$resource_id = $input->getArgument('resource-id');
$status = $input->getOption('autorization');
$table = $this->getHelperSet()->get('table');
$table->setlayout($table::LAYOUT_COMPACT);
if ($resource_id) {
$this->getRestByID($output, $table, $resource_id);
} else {
$this->getAllRestResources($status, $output, $table);
}
}
/**
* @param $output OutputInterface
* @param $table TableHelper
* @param $config_name String
*/
private function getRestByID($output, $table, $resource_id)
{
// Get the list of enabled and disabled resources.
$config = $this->getRestDrupalConfig();
$resourcePluginManager = $this->getPluginManagerRest();
$plugin = $resourcePluginManager->getInstance(array('id' => $resource_id));
if (empty($plugin)) {
$output->writeln('[+] <error>' . sprintf($this->trans('commands.rest.debug.messages.not-found'),
$resource_id) . '</error>');
return false;
}
$resource = $plugin->getPluginDefinition();
$configuration = array();
$configuration[$this->trans('commands.rest.debug.messages.id')] = $resource['id'];
$configuration[$this->trans('commands.rest.debug.messages.label')] = (string)$resource['label'];
$configuration[$this->trans('commands.rest.debug.messages.canonical_url')] = $resource['uri_paths']['canonical'];
$configuration[$this->trans('commands.rest.debug.messages.status')] = (isset($config[$resource['id']])) ? $this->trans('commands.rest.debug.messages.enabled') : $this->trans('commands.rest.debug.messages.disabled');
$configuration[$this->trans('commands.rest.debug.messages.provider')] = $resource['provider'];
$configurationEncoded = Yaml::encode($configuration);
$output->writeln($configurationEncoded);
$table->render($output);
$table->setHeaders(
[
$this->trans('commands.rest.debug.messages.rest-state'),
$this->trans('commands.rest.debug.messages.supported-formats'),
$this->trans('commands.rest.debug.messages.supported_auth'),
]);
foreach ($config[$resource['id']] as $method => $settings) {
$table->addRow([
$method,
implode(', ', $settings['supported_formats']),
implode(', ', $settings['supported_auth'])
]);
}
$table->render($output);
}
protected function getAllRestResources($status, $output, $table)
{
$rest_resources = $this->getRestResources($status);
$table->setHeaders(
[
$this->trans('commands.rest.debug.messages.id'),
$this->trans('commands.rest.debug.messages.label'),
$this->trans('commands.rest.debug.messages.canonical_url'),
$this->trans('commands.rest.debug.messages.status'),
$this->trans('commands.rest.debug.messages.provider'),
]);
$table->setlayout($table::LAYOUT_COMPACT);
foreach ($rest_resources as $status => $resources) {
foreach ($resources as $id => $resource) {
$table->addRow([
$id,
$resource['label'],
$resource['uri_paths']['canonical'],
$status,
$resource['provider']
]);
}
}
$table->render($output);
}
}