5 * Contains \Drupal\Console\Command\Rest\DisableCommand.
8 namespace Drupal\Console\Command\Rest;
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;
22 * extensionType = "module"
25 class DisableCommand extends Command
32 protected $configFactory;
35 * @var ResourcePluginManager
37 protected $pluginManagerRest;
40 * DisableCommand constructor.
42 * @param ConfigFactory $configFactory
43 * @param ResourcePluginManager $pluginManagerRest
45 public function __construct(
46 ConfigFactory $configFactory,
47 ResourcePluginManager $pluginManagerRest
49 $this->configFactory = $configFactory;
50 $this->pluginManagerRest = $pluginManagerRest;
51 parent::__construct();
54 protected function configure()
57 ->setName('rest:disable')
58 ->setDescription($this->trans('commands.rest.disable.description'))
61 InputArgument::OPTIONAL,
62 $this->trans('commands.rest.debug.arguments.resource-id')
64 ->setAliases(['red']);
67 protected function execute(InputInterface $input, OutputInterface $output)
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'])
77 $resource_id = $this->getIo()->choice(
78 $this->trans('commands.rest.disable.arguments.resource-id'),
83 $this->validateRestResource(
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();
96 $this->getIo()->success(
98 $this->trans('commands.rest.disable.messages.success'),
104 $message = sprintf($this->trans('commands.rest.disable.messages.already-disabled'), $resource_id);
105 $this->getIo()->info($message);
110 * The key used in the form.
112 * @param string $resource_id
116 * The resource key in the form.
118 protected function getResourceKey($resource_id)
120 return str_replace(':', '.', $resource_id);