5 * Contains \Drupal\Console\Command\Rest\EnableCommand.
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\rest\RestResourceConfigInterface;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Command\Shared\RestTrait;
19 use Drupal\rest\Plugin\Type\ResourcePluginManager;
20 use Drupal\Core\Authentication\AuthenticationCollector;
21 use Drupal\Core\Config\ConfigFactory;
22 use Drupal\Core\Entity\EntityManager;
27 * extensionType = "module"
30 class EnableCommand extends Command
36 * @var ResourcePluginManager $pluginManagerRest
38 protected $pluginManagerRest;
41 * @var AuthenticationCollector $authenticationCollector
43 protected $authenticationCollector;
48 protected $configFactory;
51 * The available serialization formats.
60 * @var \Drupal\Core\Entity\EntityManagerInterface
62 protected $entityManager;
65 * EnableCommand constructor.
67 * @param ResourcePluginManager $pluginManagerRest
68 * @param AuthenticationCollector $authenticationCollector
69 * @param ConfigFactory $configFactory
70 * @param array $formats
71 * The available serialization formats.
72 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
75 public function __construct(
76 ResourcePluginManager $pluginManagerRest,
77 AuthenticationCollector $authenticationCollector,
78 ConfigFactory $configFactory,
80 EntityManager $entity_manager
82 $this->pluginManagerRest = $pluginManagerRest;
83 $this->authenticationCollector = $authenticationCollector;
84 $this->configFactory = $configFactory;
85 $this->formats = $formats;
86 $this->entityManager = $entity_manager;
87 parent::__construct();
90 protected function configure()
93 ->setName('rest:enable')
94 ->setDescription($this->trans('commands.rest.enable.description'))
97 InputArgument::OPTIONAL,
98 $this->trans('commands.rest.debug.arguments.resource-id')
102 protected function execute(InputInterface $input, OutputInterface $output)
104 $io = new DrupalStyle($input, $output);
106 $resource_id = $input->getArgument('resource-id');
107 $rest_resources = $this->getRestResources();
108 $rest_resources_ids = array_merge(
109 array_keys($rest_resources['enabled']),
110 array_keys($rest_resources['disabled'])
113 $resource_id = $io->choiceNoList(
114 $this->trans('commands.rest.enable.arguments.resource-id'),
119 $this->validateRestResource(
124 $input->setArgument('resource-id', $resource_id);
126 // Calculate states available by resource and generate the question.
127 $plugin = $this->pluginManagerRest->getInstance(['id' => $resource_id]);
129 $methods = $plugin->availableMethods();
130 $method = $io->choice(
131 $this->trans('commands.rest.enable.arguments.methods'),
135 $this->trans('commands.rest.enable.messages.selected-method') . ' ' . $method
138 $format = $io->choice(
139 $this->trans('commands.rest.enable.arguments.formats'),
143 $this->trans('commands.rest.enable.messages.selected-format') . ' ' . $format
146 // Get Authentication Provider and generate the question
147 $authenticationProviders = $this->authenticationCollector->getSortedProviders();
149 $authenticationProvidersSelected = $io->choice(
150 $this->trans('commands.rest.enable.messages.authentication-providers'),
151 array_keys($authenticationProviders),
157 $this->trans('commands.rest.enable.messages.selected-authentication-providers') . ' ' . implode(
159 $authenticationProvidersSelected
163 $format_resource_id = str_replace(':', '.', $resource_id);
164 $config = $this->entityManager->getStorage('rest_resource_config')->load($format_resource_id);
166 $config = $this->entityManager->getStorage('rest_resource_config')->create(
168 'id' => $format_resource_id,
169 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
170 'configuration' => []
174 $configuration = $config->get('configuration') ?: [];
175 $configuration[$method] = [
176 'supported_formats' => [$format],
177 'supported_auth' => $authenticationProvidersSelected,
179 $config->set('configuration', $configuration);
181 $message = sprintf($this->trans('commands.rest.enable.messages.success'), $resource_id);