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 Drupal\Console\Core\Command\ContainerAwareCommand;
14 use Drupal\Console\Annotations\DrupalCommand;
15 use Drupal\rest\RestResourceConfigInterface;
16 use Drupal\Console\Command\Shared\RestTrait;
17 use Drupal\rest\Plugin\Type\ResourcePluginManager;
18 use Drupal\Core\Authentication\AuthenticationCollector;
19 use Drupal\Core\Config\ConfigFactory;
20 use Drupal\Core\Entity\EntityManager;
25 * extensionType = "module"
28 class EnableCommand extends ContainerAwareCommand
33 * @var ResourcePluginManager $pluginManagerRest
35 protected $pluginManagerRest;
38 * @var AuthenticationCollector $authenticationCollector
40 protected $authenticationCollector;
45 protected $configFactory;
52 protected $entityManager;
55 * EnableCommand constructor.
57 * @param ResourcePluginManager $pluginManagerRest
58 * @param AuthenticationCollector $authenticationCollector
59 * @param ConfigFactory $configFactory
60 * @param EntityManager $entity_manager
63 public function __construct(
64 ResourcePluginManager $pluginManagerRest,
65 AuthenticationCollector $authenticationCollector,
66 ConfigFactory $configFactory,
67 EntityManager $entity_manager
69 $this->pluginManagerRest = $pluginManagerRest;
70 $this->authenticationCollector = $authenticationCollector;
71 $this->configFactory = $configFactory;
72 $this->entityManager = $entity_manager;
73 parent::__construct();
77 protected function configure()
80 ->setName('rest:enable')
81 ->setDescription($this->trans('commands.rest.enable.description'))
84 InputArgument::OPTIONAL,
85 $this->trans('commands.rest.debug.arguments.resource-id')
87 ->setAliases(['ree']);
90 protected function execute(InputInterface $input, OutputInterface $output)
92 $resource_id = $input->getArgument('resource-id');
93 $rest_resources = $this->getRestResources();
94 $rest_resources_ids = array_merge(
95 array_keys($rest_resources['enabled']),
96 array_keys($rest_resources['disabled'])
99 $resource_id = $this->getIo()->choiceNoList(
100 $this->trans('commands.rest.enable.arguments.resource-id'),
105 $this->validateRestResource(
110 $input->setArgument('resource-id', $resource_id);
112 // Calculate states available by resource and generate the question.
113 $plugin = $this->pluginManagerRest->getInstance(['id' => $resource_id]);
115 $methods = $plugin->availableMethods();
116 $method = $this->getIo()->choice(
117 $this->trans('commands.rest.enable.arguments.methods'),
120 $this->getIo()->writeln(
121 $this->trans('commands.rest.enable.messages.selected-method') . ' ' . $method
124 $format = $this->getIo()->choice(
125 $this->trans('commands.rest.enable.arguments.formats'),
126 $this->container->getParameter('serializer.formats')
129 $this->getIo()->writeln(
130 $this->trans('commands.rest.enable.messages.selected-format') . ' ' . $format
133 // Get Authentication Provider and generate the question
134 $authenticationProviders = $this->authenticationCollector->getSortedProviders();
136 $authenticationProvidersSelected = $this->getIo()->choice(
137 $this->trans('commands.rest.enable.messages.authentication-providers'),
138 array_keys($authenticationProviders),
143 $this->getIo()->writeln(
144 $this->trans('commands.rest.enable.messages.selected-authentication-providers') . ' ' . implode(
146 $authenticationProvidersSelected
150 $format_resource_id = str_replace(':', '.', $resource_id);
151 $config = $this->entityManager->getStorage('rest_resource_config')->load($format_resource_id);
153 $config = $this->entityManager->getStorage('rest_resource_config')->create(
155 'id' => $format_resource_id,
156 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
157 'configuration' => []
161 $configuration = $config->get('configuration') ?: [];
162 $configuration[$method] = [
163 'supported_formats' => [$format],
164 'supported_auth' => $authenticationProvidersSelected,
166 $config->set('configuration', $configuration);
168 $message = sprintf($this->trans('commands.rest.enable.messages.success'), $resource_id);
169 $this->getIo()->info($message);