diff --git a/config/services/debug.yml b/config/services/debug.yml index 5b546f80e..47e7fd590 100644 --- a/config/services/debug.yml +++ b/config/services/debug.yml @@ -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: diff --git a/config/services/rest.yml b/config/services/rest.yml index 985c195ed..bf88e4ed1 100644 --- a/config/services/rest.yml +++ b/config/services/rest.yml @@ -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 } diff --git a/src/Command/Debug/RestCommand.php b/src/Command/Debug/RestCommand.php index 1aaa6a7a3..527051f90 100644 --- a/src/Command/Debug/RestCommand.php +++ b/src/Command/Debug/RestCommand.php @@ -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(); } diff --git a/src/Command/Generate/PluginRestResourceCommand.php b/src/Command/Generate/PluginRestResourceCommand.php index 6127ceb78..bbcf2d575 100644 --- a/src/Command/Generate/PluginRestResourceCommand.php +++ b/src/Command/Generate/PluginRestResourceCommand.php @@ -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', ], ]; } diff --git a/src/Command/Rest/DisableCommand.php b/src/Command/Rest/DisableCommand.php index 8044367df..5fabbd6b3 100644 --- a/src/Command/Rest/DisableCommand.php +++ b/src/Command/Rest/DisableCommand.php @@ -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( diff --git a/src/Command/Rest/EnableCommand.php b/src/Command/Rest/EnableCommand.php index 93af9a64b..b99c8399b 100644 --- a/src/Command/Rest/EnableCommand.php +++ b/src/Command/Rest/EnableCommand.php @@ -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,7 +112,7 @@ 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( @@ -122,12 +120,12 @@ protected function execute(InputInterface $input, OutputInterface $output) ); $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 diff --git a/src/Command/Shared/RestTrait.php b/src/Command/Shared/RestTrait.php index a174d028c..4470c25b5 100644 --- a/src/Command/Shared/RestTrait.php +++ b/src/Command/Shared/RestTrait.php @@ -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; diff --git a/templates/module/src/Plugin/Rest/Resource/rest.php.twig b/templates/module/src/Plugin/Rest/Resource/rest.php.twig index e2a620d95..726dcf453 100644 --- a/templates/module/src/Plugin/Rest/Resource/rest.php.twig +++ b/templates/module/src/Plugin/Rest/Resource/rest.php.twig @@ -26,93 +26,94 @@ use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; * id = "{{ plugin_id }}", * label = @Translation("{{ plugin_label }}"), * uri_paths = { - * "canonical" = "/{{ plugin_url }}" +{% for state_settings in plugin_states %} + * "{{ state_settings.uri_paths }}" = "{{ plugin_url }}" +{% endfor %} * } * ) */ class {{ class_name }} extends ResourceBase {% endblock %} {% block class_variables %} - /** - * A current user instance. - * - * @var \Drupal\Core\Session\AccountProxyInterface - */ - protected $currentUser; + /** + * A current user instance. + * + * @var \Drupal\Core\Session\AccountProxyInterface + */ + protected $currentUser; {% endblock %} {% block class_construct %} - /** - * Constructs a new {{ class_name }} object. - * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param array $serializer_formats - * The available serialization formats. - * @param \Psr\Log\LoggerInterface $logger - * A logger instance. - * @param \Drupal\Core\Session\AccountProxyInterface $current_user - * A current user instance. - */ - public function __construct( - array $configuration, - $plugin_id, - $plugin_definition, - array $serializer_formats, - LoggerInterface $logger, - AccountProxyInterface $current_user) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger); + /** + * Constructs a new {{ class_name }} object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param array $serializer_formats + * The available serialization formats. + * @param \Psr\Log\LoggerInterface $logger + * A logger instance. + * @param \Drupal\Core\Session\AccountProxyInterface $current_user + * A current user instance. + */ + public function __construct( + array $configuration, + $plugin_id, + $plugin_definition, + array $serializer_formats, + LoggerInterface $logger, + AccountProxyInterface $current_user) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger); - $this->currentUser = $current_user; - } + $this->currentUser = $current_user; + } {% endblock %} {% block class_create %} - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->getParameter('serializer.formats'), - $container->get('logger.factory')->get('{{module_name}}'), - $container->get('current_user') - ); - } + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->getParameter('serializer.formats'), + $container->get('logger.factory')->get('{{module_name}}'), + $container->get('current_user') + ); + } {% endblock %} {% block class_methods %} {% for state, state_settings in plugin_states %} - /** - * Responds to {{ state }} requests. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity object. - * - * @return \Drupal\rest\{{ state_settings.response_class }} - * The HTTP response object. - * - * @throws \Symfony\Component\HttpKernel\Exception\HttpException - * Throws exception expected. - */ - public function {{ state|lower }}(EntityInterface $entity) { + /** + * Responds to {{ state }} requests. + * + * @param string $payload + * + * @return \Drupal\rest\{{ state_settings.response_class }} + * The HTTP response object. + * + * @throws \Symfony\Component\HttpKernel\Exception\HttpException + * Throws exception expected. + */ + public function {{ state|lower }}($payload) { - // You must to implement the logic of your REST Resource here. - // Use current user after pass authentication to validate access. - if (!$this->currentUser->hasPermission('access content')) { - throw new AccessDeniedHttpException(); - } + // You must to implement the logic of your REST Resource here. + // Use current user after pass authentication to validate access. + if (!$this->currentUser->hasPermission('access content')) { + throw new AccessDeniedHttpException(); + } - return new {{ state_settings.response_class }}({{ (state == 'DELETE') ? 'NULL' : '$entity' }}, {{ state_settings.http_code }}); - } + return new {{ state_settings.response_class }}({{ (state == 'DELETE') ? 'NULL' : '$payload' }}, {{ state_settings.http_code }}); + } {% endfor %} {% endblock %}