Version 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     /**
58      * @DrupalCommand(
59      *     dependencies = {
60      *         â€śrest"
61      *     }
62      * )
63      */
64     protected function configure()
65     {
66         $this
67             ->setName('rest:disable')
68             ->setDescription($this->trans('commands.rest.disable.description'))
69             ->addArgument(
70                 'resource-id',
71                 InputArgument::OPTIONAL,
72                 $this->trans('commands.rest.debug.arguments.resource-id')
73             );
74     }
75
76     protected function execute(InputInterface $input, OutputInterface $output)
77     {
78         $io = new DrupalStyle($input, $output);
79
80         $resource_id = $input->getArgument('resource-id');
81         $rest_resources = $this->getRestResources();
82         $rest_resources_ids = array_merge(
83             array_keys($rest_resources['enabled']),
84             array_keys($rest_resources['disabled'])
85         );
86
87         if (!$resource_id) {
88             $resource_id = $io->choice(
89                 $this->trans('commands.rest.disable.arguments.resource-id'),
90                 $rest_resources_ids
91             );
92         }
93
94         $this->validateRestResource(
95             $resource_id,
96             $rest_resources_ids,
97             $this->translator
98         );
99         $resources = \Drupal::service('entity_type.manager')
100             ->getStorage('rest_resource_config')->loadMultiple();
101         if ($resources[$this->getResourceKey($resource_id)]) {
102             $routeBuilder = \Drupal::service('router.builder');
103             $resources[$this->getResourceKey($resource_id)]->delete();
104             // Rebuild routing cache.
105             $routeBuilder->rebuild();
106
107             $io->success(
108                 sprintf(
109                     $this->trans('commands.rest.disable.messages.success'),
110                     $resource_id
111                 )
112             );
113             return true;
114         }
115         $message = sprintf($this->trans('commands.rest.disable.messages.already-disabled'), $resource_id);
116         $io->info($message);
117         return true;
118     }
119
120     /**
121      * The key used in the form.
122      *
123      * @param string $resource_id
124      *   The resource ID.
125      *
126      * @return string
127      *   The resource key in the form.
128      */
129     protected function getResourceKey($resource_id)
130     {
131         return str_replace(':', '.', $resource_id);
132     }
133 }