Commit 50310305 authored by alexpott's avatar alexpott
Browse files

Issue #2011122 by hussainweb, kgoel, chrisjlee: Replace drupal_container()...

Issue #2011122 by hussainweb, kgoel, chrisjlee: Replace drupal_container() with injected services in the rest module.
parent 033273b6
......@@ -8,13 +8,51 @@
namespace Drupal\rest\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Common base class for resource plugins.
*/
abstract class ResourceBase extends PluginBase implements ResourceInterface {
abstract class ResourceBase extends PluginBase implements ContainerFactoryPluginInterface, ResourceInterface {
/**
* The available serialization formats.
*
* @var array
*/
protected $serializerFormats = array();
/**
* Constructs a Drupal\rest\Plugin\ResourceBase 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 array $plugin_definition
* The plugin implementation definition.
* @param array $serializer_formats
* The available serialization formats.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, array $serializer_formats) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->serializerFormats = $serializer_formats;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->getParameter('serializer.formats')
);
}
/**
* Implements ResourceInterface::permissions().
......@@ -68,8 +106,7 @@ public function routes() {
case 'HEAD':
// Restrict GET and HEAD requests to the media type specified in the
// HTTP Accept headers.
$formats = drupal_container()->getParameter('serializer.formats');
foreach ($formats as $format_name) {
foreach ($this->serializerFormats as $format_name) {
// Expose one route per available format.
//$format_route = new Route($route->getPattern(), $route->getDefaults(), $route->getRequirements());
$format_route = clone $route;
......@@ -124,4 +161,5 @@ public function availableMethods() {
}
return $available;
}
}
......@@ -7,33 +7,35 @@
namespace Drupal\rest\Plugin\Type;
use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator;
use Drupal\Component\Plugin\Factory\ReflectionFactory;
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
/**
* Manages discovery and instantiation of resource plugins.
*/
class ResourcePluginManager extends PluginManagerBase {
class ResourcePluginManager extends DefaultPluginManager {
/**
* Overrides Drupal\Component\Plugin\PluginManagerBase::__construct().
* Constructs a new \Drupal\rest\Plugin\Type\ResourcePluginManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations,
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Language\LanguageManager $language_manager
* The language manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces) {
// Create resource plugin derivatives from declaratively defined resources.
$this->discovery = new AnnotatedClassDiscovery('Plugin/rest/resource', $namespaces);
$this->discovery = new DerivativeDiscoveryDecorator($this->discovery);
$this->discovery = new AlterDecorator($this->discovery, 'rest_resource');
$this->discovery = new CacheDecorator($this->discovery, 'rest');
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/rest/resource', $namespaces);
$this->factory = new ReflectionFactory($this->discovery);
$this->setCacheBackend($cache_backend, $language_manager, 'rest_plugins');
$this->alterInfo($module_handler, 'rest_resource');
}
/**
......
......@@ -9,8 +9,11 @@
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\ContentNegotiation;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\PathPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouteCollection;
......@@ -69,17 +72,60 @@ class RestExport extends PathPluginBase {
*/
protected $mimeType;
/**
* The content negotiation library.
*
* @var \Drupal\Core\ContentNegotiation
*/
protected $contentNegotiation;
/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Constructs a Drupal\rest\Plugin\ResourceBase 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 array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\ContentNegotiation $content_negotiation
* The content negotiation library.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ContentNegotiation $content_negotiation, Request $request) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->contentNegotiation = $content_negotiation;
$this->request = $request;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('content_negotiation'),
$container->get('request')
);
}
/**
* {@inheritdoc}
*/
public function initDisplay(ViewExecutable $view, array &$display, array &$options = NULL) {
parent::initDisplay($view, $display, $options);
$container = drupal_container();
$negotiation = $container->get('content_negotiation');
$request = $container->get('request');
$request_content_type = $negotiation->getContentType($request);
$request_content_type = $this->contentNegotiation->getContentType($this->request);
// Only use the requested content type if it's not 'html'. If it is then
// default to 'json' to aid debugging.
// @todo Remove the need for this when we have better content negotiation.
......@@ -87,7 +133,7 @@ public function initDisplay(ViewExecutable $view, array &$display, array &$optio
$this->setContentType($request_content_type);
}
$this->setMimeType($request->getMimeType($this->contentType));
$this->setMimeType($this->request->getMimeType($this->contentType));
}
/**
......
......@@ -33,7 +33,7 @@ public static function getInfo() {
* Tests several valid and invalid create requests on all entity types.
*/
public function testCreate() {
$serializer = drupal_container()->get('serializer');
$serializer = $this->container->get('serializer');
$entity_types = array('entity_test', 'node');
foreach ($entity_types as $entity_type) {
......
......@@ -209,7 +209,7 @@ protected function enableService($resource_type, $method = 'GET', $format = NULL
$config->save();
// Rebuild routing cache, so that the REST API paths are available.
drupal_container()->get('router.builder')->rebuild();
$this->container->get('router.builder')->rebuild();
// Reset the Simpletest permission cache, so that the new resource
// permissions get picked up.
drupal_static_reset('checkPermissions');
......
......@@ -33,7 +33,7 @@ public static function getInfo() {
* Tests several valid and invalid partial update requests on test entities.
*/
public function testPatchUpdate() {
$serializer = drupal_container()->get('serializer');
$serializer = $this->container->get('serializer');
// @todo once EntityNG is implemented for other entity types test all other
// entity types here as well.
$entity_type = 'entity_test';
......
......@@ -109,7 +109,7 @@ public function testSerializerResponses() {
$this->executeView($view);
// Get the serializer service.
$serializer = drupal_container()->get('serializer');
$serializer = $this->container->get('serializer');
$entities = array();
foreach ($view->result as $row) {
......
......@@ -10,14 +10,12 @@
*/
function rest_permission() {
$permissions = array();
if (drupal_container()->has('plugin.manager.rest')) {
$manager = drupal_container()->get('plugin.manager.rest');
$resources = config('rest.settings')->get('resources');
if ($resources && $enabled = array_intersect_key($manager->getDefinitions(), $resources)) {
foreach ($enabled as $key => $resource) {
$plugin = $manager->getInstance(array('id' => $key));
$permissions = array_merge($permissions, $plugin->permissions());
}
$manager = Drupal::service('plugin.manager.rest');
$resources = config('rest.settings')->get('resources');
if ($resources && $enabled = array_intersect_key($manager->getDefinitions(), $resources)) {
foreach ($enabled as $key => $resource) {
$plugin = $manager->getInstance(array('id' => $key));
$permissions = array_merge($permissions, $plugin->permissions());
}
}
return $permissions;
......
services:
plugin.manager.rest:
class: Drupal\rest\Plugin\Type\ResourcePluginManager
arguments: ['@container.namespaces']
arguments: ['@container.namespaces', '@cache.rest', '@language_manager', '@module_handler']
cache.rest:
class: Drupal\Core\Cache\CacheBackendInterface
tags:
- { name: cache.bin }
factory_method: get
factory_service: cache_factory
arguments: [rest]
rest.route_subscriber:
class: Drupal\rest\EventSubscriber\RouteSubscriber
tags:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment