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