View source
<?php
namespace Drupal\jsonrpc_core\Plugin\jsonrpc\Method;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\jsonrpc\Exception\JsonRpcException;
use Drupal\jsonrpc\HandlerInterface;
use Drupal\jsonrpc\Object\Error;
use Drupal\jsonrpc\Object\ParameterBag;
use Drupal\jsonrpc\Plugin\JsonRpcMethodBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
class Plugins extends JsonRpcMethodBase {
protected $pluginManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, PluginManagerInterface $plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->pluginManager = $plugin_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$request = $configuration[HandlerInterface::JSONRPC_REQUEST_KEY];
try {
$plugin_manager = $container
->get($request
->getParameter('service'));
} catch (ServiceNotFoundException $e) {
throw JsonRpcException::fromError(Error::invalidParams($e
->getMessage()));
}
return new static($configuration, $plugin_id, $plugin_definition, $plugin_manager);
}
public function execute(ParameterBag $params) {
$pager = $params
->get('page');
$definitions = $this->pluginManager
->getDefinitions();
foreach ($definitions as $definition) {
if (!is_array($definition)) {
throw JsonRpcException::fromError(Error::invalidParams('Object-based plugin definitions are not yet supported.'));
}
}
if (!$pager) {
return $definitions;
}
return array_slice($definitions, $pager['offset'], $pager['limit']);
}
public static function outputSchema() {
return [
'type' => 'object',
'patternProperties' => [
'.{1,}' => [
'class' => [
'type' => 'string',
],
'uri' => [
'type' => 'string',
],
'description' => [
'type' => 'string',
],
'provider' => [
'type' => 'string',
],
'id' => [
'type' => 'string',
],
],
],
];
}
}