8044367df66c63b165dd15c62c8497efff53d8b9
[yaffs-website] / vendor / drupal / console / src / Command / Rest / DisableCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Rest\DisableCommand.
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\Command;
14 use Drupal\Console\Annotations\DrupalCommand;
15 use Drupal\Console\Command\Shared\RestTrait;
16 use Drupal\Core\Config\ConfigFactory;
17 use Drupal\rest\Plugin\Type\ResourcePluginManager;
18
19 /**
20  * @DrupalCommand(
21  *     extension = "rest",
22  *     extensionType = "module"
23  * )
24  */
25 class DisableCommand extends Command
26 {
27     use RestTrait;
28
29     /**
30      * @var ConfigFactory
31      */
32     protected $configFactory;
33
34     /**
35      * @var ResourcePluginManager
36      */
37     protected $pluginManagerRest;
38
39     /**
40      * DisableCommand constructor.
41      *
42      * @param ConfigFactory         $configFactory
43      * @param ResourcePluginManager $pluginManagerRest
44      */
45     public function __construct(
46         ConfigFactory $configFactory,
47         ResourcePluginManager $pluginManagerRest
48     ) {
49         $this->configFactory = $configFactory;
50         $this->pluginManagerRest = $pluginManagerRest;
51         parent::__construct();
52     }
53
54     protected function configure()
55     {
56         $this
57             ->setName('rest:disable')
58             ->setDescription($this->trans('commands.rest.disable.description'))
59             ->addArgument(
60                 'resource-id',
61                 InputArgument::OPTIONAL,
62                 $this->trans('commands.rest.debug.arguments.resource-id')
63             )
64             ->setAliases(['red']);
65     }
66
67     protected function execute(InputInterface $input, OutputInterface $output)
68     {
69         $resource_id = $input->getArgument('resource-id');
70         $rest_resources = $this->getRestResources();
71         $rest_resources_ids = array_merge(
72             array_keys($rest_resources['enabled']),
73             array_keys($rest_resources['disabled'])
74         );
75
76         if (!$resource_id) {
77             $resource_id = $this->getIo()->choice(
78                 $this->trans('commands.rest.disable.arguments.resource-id'),
79                 $rest_resources_ids
80             );
81         }
82
83         $this->validateRestResource(
84             $resource_id,
85             $rest_resources_ids,
86             $this->translator
87         );
88         $resources = \Drupal::service('entity_type.manager')
89             ->getStorage('rest_resource_config')->loadMultiple();
90         if ($resources[$this->getResourceKey($resource_id)]) {
91             $routeBuilder = \Drupal::service('router.builder');
92             $resources[$this->getResourceKey($resource_id)]->delete();
93             // Rebuild routing cache.
94             $routeBuilder->rebuild();
95
96             $this->getIo()->success(
97                 sprintf(
98                     $this->trans('commands.rest.disable.messages.success'),
99                     $resource_id
100                 )
101             );
102             return true;
103         }
104         $message = sprintf($this->trans('commands.rest.disable.messages.already-disabled'), $resource_id);
105         $this->getIo()->info($message);
106         return true;
107     }
108
109     /**
110      * The key used in the form.
111      *
112      * @param string $resource_id
113      *   The resource ID.
114      *
115      * @return string
116      *   The resource key in the form.
117      */
118     protected function getResourceKey($resource_id)
119     {
120         return str_replace(':', '.', $resource_id);
121     }
122 }