Skip to content
Permalink
Browse files

Update rest resource command (#3869)

* [plugin:rest:resource] Delete plugin id and update template

* [rest:enable] Fix translation key

* [rest:commands] Update and fix bug in rest commands

* [rest:enable] Fixed translation strings
  • Loading branch information...
harold20 authored and enzolutions committed May 15, 2019
1 parent 5fdb488 commit 6f42420740ed9607dd36484a689893609812305e
@@ -130,7 +130,7 @@ services:
- { name: drupal.command }
console.rest_debug:
class: Drupal\Console\Command\Debug\RestCommand
arguments: ['@?plugin.manager.rest']
arguments: ['@entity_type.manager', '@?plugin.manager.rest']
tags:
- { name: drupal.command }
console.test_debug:
@@ -1,15 +1,15 @@
services:
console.rest_disable:
class: Drupal\Console\Command\Rest\DisableCommand
arguments: ['@config.factory', '@?plugin.manager.rest']
arguments: ['@entity_type.manager', '@?plugin.manager.rest']
tags:
- { name: drupal.command }
console.rest_enable:
class: Drupal\Console\Command\Rest\EnableCommand
arguments:
- '@entity_type.manager'
- '@?plugin.manager.rest'
- '@authentication_collector'
- '@config.factory'
- '@entity.manager'
tags:
- { name: drupal.command }
@@ -15,6 +15,7 @@
use Drupal\Console\Annotations\DrupalCommand;
use Drupal\Console\Command\Shared\RestTrait;
use Drupal\rest\Plugin\Type\ResourcePluginManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
/**
* @DrupalCommand(
@@ -26,6 +27,11 @@ class RestCommand extends Command
{
use RestTrait;
/**
* @var EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* @var ResourcePluginManager $pluginManagerRest
*/
@@ -34,10 +40,14 @@ class RestCommand extends Command
/**
* RestCommand constructor.
*
* @param ResourcePluginManager $pluginManagerRest
* @param EntityTypeManagerInterface $entityTypeManager
* @param ResourcePluginManager $pluginManagerRest
*/
public function __construct(ResourcePluginManager $pluginManagerRest)
{
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
ResourcePluginManager $pluginManagerRest
) {
$this->entityTypeManager = $entityTypeManager;
$this->pluginManagerRest = $pluginManagerRest;
parent::__construct();
}
@@ -19,6 +19,7 @@
use Drupal\Console\Extension\Manager;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Core\Utils\ChainQueue;
use Webmozart\PathUtil\Path;
/**
* Class PluginRestResourceCommand
@@ -149,14 +150,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
$prepared_plugin[$plugin_state] = $http_methods[$plugin_state];
}
$this->generator->generate([
'module_name' => $module,
'class_name' => $class_name,
'plugin_label' => $plugin_label,
'plugin_id' => $plugin_id,
'plugin_url' => $plugin_url,
'plugin_states' => $prepared_plugin,
]);
$this->generator->generate(
[
'module_name' => $module,
'class_name' => $class_name,
'plugin_label' => $plugin_label,
'plugin_id' => $plugin_id,
'plugin_url' => $plugin_url,
'plugin_states' => $prepared_plugin,
]
);
$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
@@ -205,8 +208,13 @@ function ($class) {
$plugin_url = $input->getOption('plugin-url');
if (!$plugin_url) {
$plugin_url = $this->getIo()->ask(
$this->trans('commands.generate.plugin.rest.resource.questions.plugin-url')
$this->trans('commands.generate.plugin.rest.resource.questions.plugin-url'),
null,
function ($plugin_url) {
return Path::isAbsolute($plugin_url) ? $plugin_url : '/'.$plugin_url;
}
);
$input->setOption('plugin-url', $plugin_url);
}
@@ -236,32 +244,39 @@ protected function getHttpMethods()
{
return [
'GET' => [
'http_code' => 200,
'response_class' => 'ResourceResponse',
'http_code' => 200,
'response_class' => 'ResourceResponse',
'uri_paths' => 'canonical',
],
'PUT' => [
'http_code' => 201,
'response_class' => 'ModifiedResourceResponse',
'http_code' => 201,
'response_class' => 'ModifiedResourceResponse',
'uri_paths' => 'canonical',
],
'POST' => [
'http_code' => 200,
'response_class' => 'ModifiedResourceResponse',
'http_code' => 200,
'response_class' => 'ModifiedResourceResponse',
'uri_paths' => 'create',
],
'PATCH' => [
'http_code' => 204,
'response_class' => 'ModifiedResourceResponse',
'http_code' => 204,
'response_class' => 'ModifiedResourceResponse',
'uri_paths' => 'canonical',
],
'DELETE' => [
'http_code' => 204,
'response_class' => 'ModifiedResourceResponse',
'http_code' => 204,
'response_class' => 'ModifiedResourceResponse',
'uri_type' => 'canonical',
],
'HEAD' => [
'http_code' => 200,
'response_class' => 'ResourceResponse',
'http_code' => 200,
'response_class' => 'ResourceResponse',
'uri_type' => 'canonical',
],
'OPTIONS' => [
'http_code' => 200,
'response_class' => 'ResourceResponse',
'http_code' => 200,
'response_class' => 'ResourceResponse',
'uri_type' => 'canonical',
],
];
}
@@ -13,8 +13,8 @@
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Annotations\DrupalCommand;
use Drupal\Console\Command\Shared\RestTrait;
use Drupal\Core\Config\ConfigFactory;
use Drupal\rest\Plugin\Type\ResourcePluginManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
/**
* @DrupalCommand(
@@ -27,9 +27,9 @@ class DisableCommand extends Command
use RestTrait;
/**
* @var ConfigFactory
* @var EntityTypeManagerInterface
*/
protected $configFactory;
protected $entityTypeManager;
/**
* @var ResourcePluginManager
@@ -39,14 +39,14 @@ class DisableCommand extends Command
/**
* DisableCommand constructor.
*
* @param ConfigFactory $configFactory
* @param ResourcePluginManager $pluginManagerRest
* @param EntityTypeManagerInterface $entityTypeManager
* @param ResourcePluginManager $pluginManagerRest
*/
public function __construct(
ConfigFactory $configFactory,
EntityTypeManagerInterface $entityTypeManager,
ResourcePluginManager $pluginManagerRest
) {
$this->configFactory = $configFactory;
$this->entityTypeManager = $entityTypeManager;
$this->pluginManagerRest = $pluginManagerRest;
parent::__construct();
}
@@ -68,10 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$resource_id = $input->getArgument('resource-id');
$rest_resources = $this->getRestResources();
$rest_resources_ids = array_merge(
array_keys($rest_resources['enabled']),
array_keys($rest_resources['disabled'])
);
$rest_resources_ids = array_keys($rest_resources['enabled']);
if (!$resource_id) {
$resource_id = $this->getIo()->choice(
@@ -16,8 +16,8 @@
use Drupal\Console\Command\Shared\RestTrait;
use Drupal\rest\Plugin\Type\ResourcePluginManager;
use Drupal\Core\Authentication\AuthenticationCollector;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
/**
* @DrupalCommand(
@@ -29,6 +29,11 @@ class EnableCommand extends ContainerAwareCommand
{
use RestTrait;
/**
* @var EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* @var ResourcePluginManager $pluginManagerRest
*/
@@ -39,11 +44,6 @@ class EnableCommand extends ContainerAwareCommand
*/
protected $authenticationCollector;
/**
* @var ConfigFactory
*/
protected $configFactory;
/**
* The entity manager.
*
@@ -54,21 +54,21 @@ class EnableCommand extends ContainerAwareCommand
/**
* EnableCommand constructor.
*
* @param ResourcePluginManager $pluginManagerRest
* @param AuthenticationCollector $authenticationCollector
* @param ConfigFactory $configFactory
* @param EntityManager $entity_manager
* @param EntityTypeManagerInterface $entityTypeManager
* @param ResourcePluginManager $pluginManagerRest
* @param AuthenticationCollector $authenticationCollector
* @param EntityManager $entity_manager
* The entity manager.
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
ResourcePluginManager $pluginManagerRest,
AuthenticationCollector $authenticationCollector,
ConfigFactory $configFactory,
EntityManager $entity_manager
) {
$this->entityTypeManager = $entityTypeManager;
$this->pluginManagerRest = $pluginManagerRest;
$this->authenticationCollector = $authenticationCollector;
$this->configFactory = $configFactory;
$this->entityManager = $entity_manager;
parent::__construct();
}
@@ -82,7 +82,7 @@ protected function configure()
->addArgument(
'resource-id',
InputArgument::OPTIONAL,
$this->trans('commands.rest.debug.arguments.resource-id')
$this->trans('commands.rest.enable.arguments.resource-id')
)
->setAliases(['ree']);
}
@@ -91,10 +91,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$resource_id = $input->getArgument('resource-id');
$rest_resources = $this->getRestResources();
$rest_resources_ids = array_merge(
array_keys($rest_resources['enabled']),
array_keys($rest_resources['disabled'])
);
$rest_resources_ids = array_keys($rest_resources['disabled']);
if (!$resource_id) {
$resource_id = $this->getIo()->choiceNoList(
$this->trans('commands.rest.enable.arguments.resource-id'),
@@ -114,20 +112,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
$methods = $plugin->availableMethods();
$method = $this->getIo()->choice(
$this->trans('commands.rest.enable.arguments.methods'),
$this->trans('commands.rest.enable.messages.methods'),
$methods
);
$this->getIo()->writeln(
$this->trans('commands.rest.enable.messages.selected-method') . ' ' . $method
);
$format = $this->getIo()->choice(
$this->trans('commands.rest.enable.arguments.formats'),
$this->trans('commands.rest.enable.messages.formats'),
$this->container->getParameter('serializer.formats')
);
$this->getIo()->writeln(
$this->trans('commands.rest.enable.messages.selected-format') . ' ' . $format
$this->trans('commands.rest.enable.messages.selected-formats') . ' ' . $format
);
// Get Authentication Provider and generate the question
@@ -49,8 +49,8 @@ public function getRestResources($rest_status = false)
public function getRestDrupalConfig()
{
if ($this->configFactory) {
return $this->configFactory->get('rest.settings')->get('resources') ?: [];
if ($this->entityTypeManager) {
return $this->entityTypeManager->getStorage('rest_resource_config')->loadMultiple() ?: [];
}
return null;

0 comments on commit 6f42420

Please sign in to comment.
You can’t perform that action at this time.