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 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;
24 * extensionType = "module"
27 class DisableCommand extends Command
35 protected $configFactory;
38 * @var ResourcePluginManager
40 protected $pluginManagerRest;
43 * DisableCommand constructor.
45 * @param ConfigFactory $configFactory
46 * @param ResourcePluginManager $pluginManagerRest
48 public function __construct(
49 ConfigFactory $configFactory,
50 ResourcePluginManager $pluginManagerRest
52 $this->configFactory = $configFactory;
53 $this->pluginManagerRest = $pluginManagerRest;
54 parent::__construct();
57 protected function configure()
60 ->setName('rest:disable')
61 ->setDescription($this->trans('commands.rest.disable.description'))
64 InputArgument::OPTIONAL,
65 $this->trans('commands.rest.debug.arguments.resource-id')
69 protected function execute(InputInterface $input, OutputInterface $output)
71 $io = new DrupalStyle($input, $output);
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'])
81 $resource_id = $io->choice(
82 $this->trans('commands.rest.disable.arguments.resource-id'),
87 $this->validateRestResource(
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();
102 $this->trans('commands.rest.disable.messages.success'),
108 $message = sprintf($this->trans('commands.rest.disable.messages.already-disabled'), $resource_id);
114 * The key used in the form.
116 * @param string $resource_id
120 * The resource key in the form.
122 protected function getResourceKey($resource_id)
124 return str_replace(':', '.', $resource_id);