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();
64 protected function configure()
67 ->setName('rest:disable')
68 ->setDescription($this->trans('commands.rest.disable.description'))
71 InputArgument::OPTIONAL,
72 $this->trans('commands.rest.debug.arguments.resource-id')
76 protected function execute(InputInterface $input, OutputInterface $output)
78 $io = new DrupalStyle($input, $output);
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'])
88 $resource_id = $io->choice(
89 $this->trans('commands.rest.disable.arguments.resource-id'),
94 $this->validateRestResource(
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();
109 $this->trans('commands.rest.disable.messages.success'),
115 $message = sprintf($this->trans('commands.rest.disable.messages.already-disabled'), $resource_id);
121 * The key used in the form.
123 * @param string $resource_id
127 * The resource key in the form.
129 protected function getResourceKey($resource_id)
131 return str_replace(':', '.', $resource_id);