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