forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestEnableCommand.php
More file actions
106 lines (85 loc) · 3.87 KB
/
Copy pathRestEnableCommand.php
File metadata and controls
106 lines (85 loc) · 3.87 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
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\RestEnableCommand.
*/
namespace Drupal\AppConsole\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Output\OutputInterface;
class RestEnableCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('rest:enable')
->setDescription($this->trans('commands.rest.enable.description'))
->addArgument('resource-id', InputArgument::OPTIONAL,
$this->trans('commands.rest.debug.arguments.resource-id'));
$this->addDependency('rest');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$questionHelper = $this->getQuestionHelper();
$resource_id = $input->getArgument('resource-id');
$rest_resources = $this->getRestResources();
$rest_resources_ids = array_merge(array_keys($rest_resources['enabled']),
array_keys($rest_resources['disabled']));
if (!$resource_id) {
$resource_id = $dialog->askAndValidate(
$output,
$dialog->getQuestion($this->trans('commands.rest.enable.arguments.resource-id'), ''),
function ($resource_id) use ($rest_resources_ids) {
return $this->validateRestResource($resource_id, $rest_resources_ids, $this->getTranslator());
},
false,
'',
$rest_resources_ids
);
}
$this->validateRestResource($resource_id, $rest_resources_ids, $this->getTranslator());
$input->setArgument('resource-id', $resource_id);
// Calculate states available by resource and generate the question
$resourcePluginManager = $this->getPluginManagerRest();
$plugin = $resourcePluginManager->getInstance(array('id' => $resource_id));
$states = $plugin->availableMethods();
$question = new ChoiceQuestion(
$this->trans('commands.rest.enable.arguments.states'),
$states,
'0'
);
$state = $questionHelper->ask($input, $output, $question);
$output->writeln($this->trans('commands.rest.enable.messages.selected-state') . ' ' . $state);
// Get serializer formats available and generate the question.
$formats = $this->getSerializerFormats();
$question = new ChoiceQuestion(
$this->trans('commands.rest.enable.messages.formats'),
$formats,
'0'
);
$question->setMultiselect(true);
$formats = $questionHelper->ask($input, $output, $question);
$output->writeln($this->trans('commands.rest.enable.messages.selected-formats') . ' ' . implode(', ',
$formats));
// Get Authentication Provider and generate the question
$authentication_providers = $this->getAuthenticationProviders();
$question = new ChoiceQuestion(
$this->trans('commands.rest.enable.messages.authentication-providers'),
array_keys($authentication_providers),
'0'
);
$question->setMultiselect(true);
$authentication_providers = $questionHelper->ask($input, $output, $question);
$output->writeln($this->trans('commands.rest.enable.messages.selected-authentication-providers') . ' ' . implode(', ',
$authentication_providers));
$rest_settings = $this->getRestDrupalConfig();
$rest_settings[$resource_id][$state]['supported_formats'] = $formats;
$rest_settings[$resource_id][$state]['supported_auth'] = $authentication_providers;
$config = $this->getConfigFactory()
->getEditable('rest.settings');
$config->set('resources', $rest_settings);
$config->save();
}
}