Skip to content
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

[rest:enable] Fix enable rest resource #2919

Merged
merged 3 commits into from
Nov 17, 2016
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/drupal-core/rest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ services:
- { name: drupal.command }
rest_enable:
class: Drupal\Console\Command\Rest\EnableCommand
arguments: ['@plugin.manager.rest', '@authentication_collector', '@config.factory']
arguments: ['@plugin.manager.rest', '@authentication_collector', '@config.factory', '%serializer.formats%', '@entity.manager']
tags:
- { name: drupal.command }
81 changes: 59 additions & 22 deletions src/Command/Rest/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Annotations\DrupalCommand;
use Drupal\rest\RestResourceConfigInterface;
use Drupal\Console\Style\DrupalStyle;
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;

/**
* @DrupalCommand(
Expand All @@ -41,28 +43,49 @@ class EnableCommand extends Command
protected $authenticationCollector;

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

/**
* The available serialization formats.
*
* @var array
*/
protected $formats;

/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;

/**
* EnableCommand constructor.
* @param ResourcePluginManager $pluginManagerRest
* @param AuthenticationCollector $authenticationCollector
* @param ConfigFactory $configFactory
* @param array $formats
* The available serialization formats.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(
ResourcePluginManager $pluginManagerRest,
AuthenticationCollector $authenticationCollector,
ConfigFactory $configFactory
ConfigFactory $configFactory,
array $formats,
EntityManager $entity_manager
) {
$this->pluginManagerRest = $pluginManagerRest;
$this->authenticationCollector = $authenticationCollector;
$this->configFactory = $configFactory;
$this->formats = $formats;
$this->entityManager = $entity_manager;
parent::__construct();
}


protected function configure()
{
$this
Expand All @@ -81,12 +104,10 @@ 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'])
);

if (!$resource_id) {
$resource_id = $io->choiceNoList(
$this->trans('commands.rest.enable.arguments.resource-id'),
Expand All @@ -101,17 +122,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
);
$input->setArgument('resource-id', $resource_id);

// Calculate states available by resource and generate the question
// Calculate states available by resource and generate the question.
$plugin = $this->pluginManagerRest->getInstance(['id' => $resource_id]);

$states = $plugin->availableMethods();
$methods = $plugin->availableMethods();
$method = $io->choice(
$this->trans('commands.rest.enable.arguments.methods'),
$methods
);
$io->writeln(
$this->trans('commands.rest.enable.messages.selected-method') . ' ' . $method
);

$state = $io->choice(
$this->trans('commands.rest.enable.arguments.states'),
$states
$format = $io->choice(
$this->trans('commands.rest.enable.arguments.formats'),
$this->formats
);
$io->writeln(
$this->trans('commands.rest.enable.messages.selected-state').' '.$state
$this->trans('commands.rest.enable.messages.selected-format') . ' ' . $format
);

// Get Authentication Provider and generate the question
Expand All @@ -125,21 +153,30 @@ protected function execute(InputInterface $input, OutputInterface $output)
);

$io->writeln(
$this->trans('commands.rest.enable.messages.selected-authentication-providers').' '.implode(
$this->trans('commands.rest.enable.messages.selected-authentication-providers') . ' ' . implode(
', ',
$authenticationProvidersSelected
)
);

$rest_settings = $this->getRestDrupalConfig();

$rest_settings[$resource_id][$state]['supported_formats'] = $formats;
$rest_settings[$resource_id][$state]['supported_auth'] = $authenticationProvidersSelected;

$config = $this->configFactory->getEditable('rest.settings');
$config->set('resources', $rest_settings);
$format_resource_id = str_replace(':', '.', $resource_id);
$config = $this->entityManager->getStorage('rest_resource_config')->load($format_resource_id);
if (!$config) {
$config = $this->entityManager->getStorage('rest_resource_config')->create([
'id' => $format_resource_id,
'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
'configuration' => []
]);
}
$configuration = $config->get('configuration') ?: [];
$configuration[$method] = [
'supported_formats' => [$format],
'supported_auth' => $authenticationProvidersSelected,
];
$config->set('configuration', $configuration);
$config->save();

return 0;
$message = sprintf($this->trans('commands.rest.enable.messages.success'), $resource_id);
$io->info($message);
return true;
}
}