Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Rest / EnableCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Rest\EnableCommand.
6  */
7
8 namespace Drupal\Console\Command\Rest;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Core\Command\Shared\CommandTrait;
15 use Drupal\Console\Annotations\DrupalCommand;
16 use Drupal\rest\RestResourceConfigInterface;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Command\Shared\RestTrait;
19 use Drupal\rest\Plugin\Type\ResourcePluginManager;
20 use Drupal\Core\Authentication\AuthenticationCollector;
21 use Drupal\Core\Config\ConfigFactory;
22 use Drupal\Core\Entity\EntityManager;
23
24 /**
25  * @DrupalCommand(
26  *     extension = "rest",
27  *     extensionType = "module"
28  * )
29  */
30 class EnableCommand extends Command
31 {
32     use CommandTrait;
33     use RestTrait;
34
35     /**
36      * @var ResourcePluginManager $pluginManagerRest
37      */
38     protected $pluginManagerRest;
39
40     /**
41      * @var AuthenticationCollector $authenticationCollector
42      */
43     protected $authenticationCollector;
44
45     /**
46      * @var ConfigFactory
47      */
48     protected $configFactory;
49
50     /**
51      * The available serialization formats.
52      *
53      * @var array
54      */
55     protected $formats;
56
57     /**
58      * The entity manager.
59      *
60      * @var \Drupal\Core\Entity\EntityManagerInterface
61      */
62     protected $entityManager;
63
64     /**
65      * EnableCommand constructor.
66      *
67      * @param ResourcePluginManager                      $pluginManagerRest
68      * @param AuthenticationCollector                    $authenticationCollector
69      * @param ConfigFactory                              $configFactory
70      * @param array                                      $formats
71      *   The available serialization formats.
72      * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
73      *   The entity manager.
74      */
75     public function __construct(
76         ResourcePluginManager $pluginManagerRest,
77         AuthenticationCollector $authenticationCollector,
78         ConfigFactory $configFactory,
79         array $formats,
80         EntityManager $entity_manager
81     ) {
82         $this->pluginManagerRest = $pluginManagerRest;
83         $this->authenticationCollector = $authenticationCollector;
84         $this->configFactory = $configFactory;
85         $this->formats = $formats;
86         $this->entityManager = $entity_manager;
87         parent::__construct();
88     }
89
90     protected function configure()
91     {
92         $this
93             ->setName('rest:enable')
94             ->setDescription($this->trans('commands.rest.enable.description'))
95             ->addArgument(
96                 'resource-id',
97                 InputArgument::OPTIONAL,
98                 $this->trans('commands.rest.debug.arguments.resource-id')
99             );
100     }
101
102     protected function execute(InputInterface $input, OutputInterface $output)
103     {
104         $io = new DrupalStyle($input, $output);
105
106         $resource_id = $input->getArgument('resource-id');
107         $rest_resources = $this->getRestResources();
108         $rest_resources_ids = array_merge(
109             array_keys($rest_resources['enabled']),
110             array_keys($rest_resources['disabled'])
111         );
112         if (!$resource_id) {
113             $resource_id = $io->choiceNoList(
114                 $this->trans('commands.rest.enable.arguments.resource-id'),
115                 $rest_resources_ids
116             );
117         }
118
119         $this->validateRestResource(
120             $resource_id,
121             $rest_resources_ids,
122             $this->translator
123         );
124         $input->setArgument('resource-id', $resource_id);
125
126         // Calculate states available by resource and generate the question.
127         $plugin = $this->pluginManagerRest->getInstance(['id' => $resource_id]);
128
129         $methods = $plugin->availableMethods();
130         $method = $io->choice(
131             $this->trans('commands.rest.enable.arguments.methods'),
132             $methods
133         );
134         $io->writeln(
135             $this->trans('commands.rest.enable.messages.selected-method') . ' ' . $method
136         );
137
138         $format = $io->choice(
139             $this->trans('commands.rest.enable.arguments.formats'),
140             $this->formats
141         );
142         $io->writeln(
143             $this->trans('commands.rest.enable.messages.selected-format') . ' ' . $format
144         );
145
146         // Get Authentication Provider and generate the question
147         $authenticationProviders = $this->authenticationCollector->getSortedProviders();
148
149         $authenticationProvidersSelected = $io->choice(
150             $this->trans('commands.rest.enable.messages.authentication-providers'),
151             array_keys($authenticationProviders),
152             0,
153             true
154         );
155
156         $io->writeln(
157             $this->trans('commands.rest.enable.messages.selected-authentication-providers') . ' ' . implode(
158                 ', ',
159                 $authenticationProvidersSelected
160             )
161         );
162
163         $format_resource_id = str_replace(':', '.', $resource_id);
164         $config = $this->entityManager->getStorage('rest_resource_config')->load($format_resource_id);
165         if (!$config) {
166             $config = $this->entityManager->getStorage('rest_resource_config')->create(
167                 [
168                 'id' => $format_resource_id,
169                 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
170                 'configuration' => []
171                 ]
172             );
173         }
174         $configuration = $config->get('configuration') ?: [];
175         $configuration[$method] = [
176           'supported_formats' => [$format],
177           'supported_auth' => $authenticationProvidersSelected,
178         ];
179         $config->set('configuration', $configuration);
180         $config->save();
181         $message = sprintf($this->trans('commands.rest.enable.messages.success'), $resource_id);
182         $io->info($message);
183         return true;
184     }
185 }