Skip to content

Update rest resource command #3869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/services/debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions config/services/rest.yml
Original file line number Diff line number Diff line change
@@ -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 }
16 changes: 13 additions & 3 deletions src/Command/Debug/RestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -26,6 +27,11 @@ class RestCommand extends Command
{
use RestTrait;

/**
* @var EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* @var ResourcePluginManager $pluginManagerRest
*/
Expand All @@ -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();
}
Expand Down
61 changes: 38 additions & 23 deletions src/Command/Generate/PluginRestResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']);

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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',
],
];
}
Expand Down
19 changes: 8 additions & 11 deletions src/Command/Rest/DisableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -27,9 +27,9 @@ class DisableCommand extends Command
use RestTrait;

/**
* @var ConfigFactory
* @var EntityTypeManagerInterface
*/
protected $configFactory;
protected $entityTypeManager;

/**
* @var ResourcePluginManager
Expand All @@ -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();
}
Expand All @@ -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(
Expand Down
38 changes: 18 additions & 20 deletions src/Command/Rest/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -29,6 +29,11 @@ class EnableCommand extends ContainerAwareCommand
{
use RestTrait;

/**
* @var EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* @var ResourcePluginManager $pluginManagerRest
*/
Expand All @@ -39,11 +44,6 @@ class EnableCommand extends ContainerAwareCommand
*/
protected $authenticationCollector;

/**
* @var ConfigFactory
*/
protected $configFactory;

/**
* The entity manager.
*
Expand All @@ -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();
}
Expand All @@ -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']);
}
Expand All @@ -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'),
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Shared/RestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading