Commit bd68a9bf authored by Dries's avatar Dries

Issue #1988666 by Crell, kim.pepper: Cleanup Controllers and Enhancers.

parent f6dd1e82
...@@ -160,7 +160,7 @@ services: ...@@ -160,7 +160,7 @@ services:
class: Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class: Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher
arguments: ['@service_container'] arguments: ['@service_container']
controller_resolver: controller_resolver:
class: Drupal\Core\ControllerResolver class: Drupal\Core\Controller\ControllerResolver
arguments: ['@service_container'] arguments: ['@service_container']
http_kernel: http_kernel:
class: Drupal\Core\HttpKernel class: Drupal\Core\HttpKernel
...@@ -264,13 +264,8 @@ services: ...@@ -264,13 +264,8 @@ services:
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
arguments: ['@settings'] arguments: ['@settings']
route_enhancer.modal: route_enhancer.content_controller:
class: Drupal\Core\Routing\Enhancer\ModalEnhancer class: Drupal\Core\Routing\Enhancer\ContentControllerEnhancer
arguments: ['@content_negotiation']
tags:
- { name: route_enhancer, priority: 40 }
route_enhancer.dialog:
class: Drupal\Core\Routing\Enhancer\DialogEnhancer
arguments: ['@content_negotiation'] arguments: ['@content_negotiation']
tags: tags:
- { name: route_enhancer, priority: 30 } - { name: route_enhancer, priority: 30 }
...@@ -290,11 +285,12 @@ services: ...@@ -290,11 +285,12 @@ services:
arguments: ['@content_negotiation'] arguments: ['@content_negotiation']
tags: tags:
- { name: route_enhancer, priority: 10 } - { name: route_enhancer, priority: 10 }
route_enhancer.page: controller.page:
class: Drupal\Core\Routing\Enhancer\PageEnhancer class: Drupal\Core\Controller\HtmlPageController
arguments: ['@content_negotiation'] arguments: ['@http_kernel']
tags: controller.dialog:
- { name: route_enhancer, priority: 0 } class: Drupal\Core\Controller\DialogController
arguments: ['@http_kernel']
router_listener: router_listener:
class: Symfony\Component\HttpKernel\EventListener\RouterListener class: Symfony\Component\HttpKernel\EventListener\RouterListener
tags: tags:
...@@ -366,7 +362,7 @@ services: ...@@ -366,7 +362,7 @@ services:
- { name: event_subscriber } - { name: event_subscriber }
arguments: ['@language_manager'] arguments: ['@language_manager']
exception_controller: exception_controller:
class: Drupal\Core\ExceptionController class: Drupal\Core\Controller\ExceptionController
arguments: ['@content_negotiation'] arguments: ['@content_negotiation']
calls: calls:
- [setContainer, ['@service_container']] - [setContainer, ['@service_container']]
......
...@@ -2,18 +2,16 @@ ...@@ -2,18 +2,16 @@
/** /**
* @file * @file
* Contains \Drupal\Core\AjaxController. * Contains \Drupal\Core\Controller\AjaxController.
*/ */
namespace Drupal\Core; namespace Drupal\Core\Controller;
use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InsertCommand; use Drupal\Core\Ajax\InsertCommand;
use Drupal\Core\Ajax\PrependCommand; use Drupal\Core\Ajax\PrependCommand;
use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Default controller for ajax requests. * Default controller for ajax requests.
......
<?php
/**
* @file
* Contains \Drupal\Core\Controller\ControllerInterface.
*/
namespace Drupal\Core\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a common interface for route controllers.
*/
interface ControllerInterface {
/**
* Instantiates a new instance of this controller.
*
* This is a factory method that returns a new instance of this object. The
* factory should pass any needed dependencies into the constructor of this
* object, but not the container itself. Every call to this method must return
* a new instance of this object; that is, it may not implement a singleton.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The service container this object should use.
*/
public static function create(ContainerInterface $container);
}
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
/** /**
* @file * @file
* Definition of Drupal\Core\ControllerResolver. * Contains \Drupal\Core\Controller\ControllerResolver.
*/ */
namespace Drupal\Core; namespace Drupal\Core\Controller;
use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver; use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver;
use Symfony\Component\HttpKernel\Log\LoggerInterface; use Symfony\Component\HttpKernel\Log\LoggerInterface;
...@@ -79,7 +79,8 @@ protected function createController($controller) { ...@@ -79,7 +79,8 @@ protected function createController($controller) {
if (!class_exists($class)) { if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
} }
if (in_array('Drupal\Core\ControllerInterface', class_implements($class))) { // @todo Remove the second in_array() once that interface has been removed.
if (in_array('Drupal\Core\Controller\ControllerInterface', class_implements($class)) || in_array('Drupal\Core\ControllerInterface', class_implements($class))) {
$controller = $class::create($this->container); $controller = $class::create($this->container);
} }
else { else {
......
...@@ -2,22 +2,36 @@ ...@@ -2,22 +2,36 @@
/** /**
* @file * @file
* Contains \Drupal\core\Ajax\DialogController * Contains \Drupal\core\Controller\DialogController.
*/ */
namespace Drupal\core\Ajax; namespace Drupal\core\Controller;
use Drupal\Core\ControllerInterface;
use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\OpenDialogCommand; use Drupal\Core\Ajax\OpenDialogCommand;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\HttpKernel\HttpKernelInterface;
/** /**
* Defines a default controller for dialog requests. * Defines a default controller for dialog requests.
*/ */
class DialogController extends ContainerAware { class DialogController {
/**
* The HttpKernel object to use for subrequests.
*
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/
protected $httpKernel;
/**
* Constructs a new HtmlPageController.
*
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $kernel
*/
public function __construct(HttpKernelInterface $kernel) {
$this->httpKernel = $kernel;
}
/** /**
* Forwards request to a subrequest. * Forwards request to a subrequest.
...@@ -45,7 +59,7 @@ protected function forward(Request $request, $content) { ...@@ -45,7 +59,7 @@ protected function forward(Request $request, $content) {
// controller. // controller.
$request->headers->remove('accept'); $request->headers->remove('accept');
return $this->container->get('http_kernel')->forward($content, $attributes->all(), $request->query->all()); return $this->httpKernel->forward($content, $attributes->all(), $request->query->all());
} }
/** /**
......
...@@ -2,18 +2,20 @@ ...@@ -2,18 +2,20 @@
/** /**
* @file * @file
* Definition of Drupal\Core\ExceptionController. * Contains \Drupal\Core\Controller\ExceptionController.
*/ */
namespace Drupal\Core; namespace Drupal\Core\Controller;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Exception\FlattenException; use Symfony\Component\HttpKernel\Exception\FlattenException;
use Drupal\Core\ContentNegotiation;
/** /**
* This controller handles HTTP errors generated by the routing system. * This controller handles HTTP errors generated by the routing system.
*/ */
...@@ -107,7 +109,7 @@ public function on403Html(FlattenException $exception, Request $request) { ...@@ -107,7 +109,7 @@ public function on403Html(FlattenException $exception, Request $request) {
drupal_static_reset('menu_set_active_trail'); drupal_static_reset('menu_set_active_trail');
menu_reset_static_cache(); menu_reset_static_cache();
$response = $this->container->get('http_kernel')->handle($subrequest, HttpKernel::SUB_REQUEST); $response = $this->container->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
$response->setStatusCode(403, 'Access denied'); $response->setStatusCode(403, 'Access denied');
} }
else { else {
...@@ -180,7 +182,7 @@ public function on404Html(FlattenException $exception, Request $request) { ...@@ -180,7 +182,7 @@ public function on404Html(FlattenException $exception, Request $request) {
drupal_static_reset('menu_set_active_trail'); drupal_static_reset('menu_set_active_trail');
menu_reset_static_cache(); menu_reset_static_cache();
$response = $this->container->get('http_kernel')->handle($subrequest, HttpKernel::SUB_REQUEST); $response = $this->container->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
$response->setStatusCode(404, 'Not Found'); $response->setStatusCode(404, 'Not Found');
} }
else { else {
......
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
/** /**
* @file * @file
* Contains \Drupal\Core\HtmlFormController. * Contains \Drupal\Core\Controller\HtmlFormController.
*/ */
namespace Drupal\Core; namespace Drupal\Core\Controller;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
......
...@@ -2,36 +2,34 @@ ...@@ -2,36 +2,34 @@
/** /**
* @file * @file
* Definition of Drupal\Core\HtmlPageController. * Contains \Drupal\Core\Controller\HtmlPageController.
*/ */
namespace Drupal\Core; namespace Drupal\Core\Controller;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Default controller for most HTML pages. * Default controller for most HTML pages.
*/ */
class HtmlPageController implements ContainerAwareInterface { class HtmlPageController {
/** /**
* The injection container for this object. * The HttpKernel object to use for subrequests.
* *
* @var \Symfony\Component\DependencyInjection\ContainerInterface * @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/ */
protected $container; protected $httpKernel;
/** /**
* Injects the service container used by this object. * Constructs a new HtmlPageController.
* *
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container * @param \Symfony\Component\HttpKernel\HttpKernelInterface $kernel
* The service container this object should use.
*/ */
public function setContainer(ContainerInterface $container = NULL) { public function __construct(HttpKernelInterface $kernel) {
$this->container = $container; $this->httpKernel = $kernel;
} }
/** /**
...@@ -60,7 +58,7 @@ public function content(Request $request, $_content) { ...@@ -60,7 +58,7 @@ public function content(Request $request, $_content) {
$attributes->remove('system_path'); $attributes->remove('system_path');
$attributes->remove('_content'); $attributes->remove('_content');
$response = $this->container->get('http_kernel')->forward($controller, $attributes->all(), $request->query->all()); $response = $this->httpKernel->forward($controller, $attributes->all(), $request->query->all());
// For successful (HTTP status 200) responses, decorate with blocks. // For successful (HTTP status 200) responses, decorate with blocks.
if ($response->isOk()) { if ($response->isOk()) {
......
...@@ -2,28 +2,17 @@ ...@@ -2,28 +2,17 @@
/** /**
* @file * @file
* Contains \Drupal\Core\ControllerInterface. * Contains \Drupal\Core\ControllerInterface;
*/ */
namespace Drupal\Core; namespace Drupal\Core;
use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Controller\ControllerInterface as BaseInterface;
/** /**
* Defines a common interface for route controllers. * BC shiv for controllers using the old interface name.
*/
interface ControllerInterface {
/**
* Instantiates a new instance of this controller.
*
* This is a factory method that returns a new instance of this object. The
* factory should pass any needed dependencies into the constructor of this
* object, but not the container itself. Every call to this method must return
* a new instance of this object; that is, it may not implement a singleton.
* *
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container * @deprecated Use \Drupal\Core\Controller\ControllerInterface instead.
* The service container this object should use.
*/ */
public static function create(ContainerInterface $container); interface ControllerInterface extends BaseInterface {
} }
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\Core\Entity; namespace Drupal\Core\Entity;
use Drupal\Core\HtmlFormController; use Drupal\Core\Controller\HtmlFormController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
class HtmlEntityFormController extends HtmlFormController { class HtmlEntityFormController extends HtmlFormController {
/** /**
* Overrides \Drupal\Core\HtmlFormController::content(). * {@inheritdoc}
* *
* Due to reflection, the argument must be named $_entity_form. The parent * Due to reflection, the argument must be named $_entity_form. The parent
* method has $request and $_form, but the parameter must match the route. * method has $request and $_form, but the parameter must match the route.
...@@ -26,7 +26,7 @@ public function content(Request $request, $_entity_form) { ...@@ -26,7 +26,7 @@ public function content(Request $request, $_entity_form) {
} }
/** /**
* Overrides \Drupal\Core\HtmlFormController::getFormObject(). * {@inheritdoc}
* *
* Instead of a class name or service ID, $form_arg will be a string * Instead of a class name or service ID, $form_arg will be a string
* representing the entity and operation being performed. * representing the entity and operation being performed.
......
...@@ -39,7 +39,7 @@ public function __construct(ContentNegotiation $negotiation) { ...@@ -39,7 +39,7 @@ public function __construct(ContentNegotiation $negotiation) {
public function enhance(array $defaults, Request $request) { public function enhance(array $defaults, Request $request) {
if (empty($defaults['_content']) && $this->negotiation->getContentType($request) == 'drupal_ajax') { if (empty($defaults['_content']) && $this->negotiation->getContentType($request) == 'drupal_ajax') {
$defaults['_content'] = $defaults['_controller']; $defaults['_content'] = $defaults['_controller'];
$defaults['_controller'] = '\Drupal\Core\AjaxController::content'; $defaults['_controller'] = '\Drupal\Core\Controller\AjaxController::content';
} }
return $defaults; return $defaults;
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
/** /**
* @file * @file
* Contains \Drupal\Core\Routing\Enhancer\DialogEnhancer. * Contains \Drupal\Core\Routing\Enhancer\ContentControllerEnhancer.
*/ */
namespace Drupal\Core\Routing\Enhancer; namespace Drupal\Core\Routing\Enhancer;
...@@ -12,33 +12,30 @@ ...@@ -12,33 +12,30 @@
use Drupal\Core\ContentNegotiation; use Drupal\Core\ContentNegotiation;
/** /**
* Enhances a route to use the DialogController for matching requests. * Enhances a route to select a controller based on the mime type of the request.
*/ */
class DialogEnhancer implements RouteEnhancerInterface { class ContentControllerEnhancer implements RouteEnhancerInterface {
/** /**
* Content negotiation library. * Content negotiation library.
* *
* @var \Drupal\CoreContentNegotiation * @var \Drupal\Core\ContentNegotiation
*/ */
protected $negotiation; protected $negotiation;
/** /**
* Content type this enhancer targets. * Associative array of supported mime types and their appropriate controller.
* *
* @var string * @var array
*/ */
protected $targetContentType = 'drupal_dialog'; protected $types = array(
'drupal_dialog' => 'controller.dialog:dialog',
'drupal_modal' => 'controller.dialog:modal',
'html' => 'controller.page:content',
);
/** /**
* Controller to route matching requests to. * Constructs a new ContentControllerEnhancer object.
*
* @var string
*/
protected $controller = '\Drupal\Core\Ajax\DialogController::dialog';
/**
* Constructs a new \Drupal\Core\Routing\Enhancer\AjaxEnhancer object.
* *
* @param \Drupal\Core\ContentNegotiation $negotiation * @param \Drupal\Core\ContentNegotiation $negotiation
* The Content Negotiation service. * The Content Negotiation service.
...@@ -51,8 +48,11 @@ public function __construct(ContentNegotiation $negotiation) { ...@@ -51,8 +48,11 @@ public function __construct(ContentNegotiation $negotiation) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function enhance(array $defaults, Request $request) { public function enhance(array $defaults, Request $request) {
if (empty($defaults['_controller']) && !empty($defaults['_content']) && $this->negotiation->getContentType($request) == $this->targetContentType) { if (empty($defaults['_controller']) && !empty($defaults['_content'])) {
$defaults['_controller'] = $this->controller; $type = $this->negotiation->getContentType($request);
if (isset($this->types[$type])) {
$defaults['_controller'] = $this->types[$type];
}
} }
return $defaults; return $defaults;
} }
......
...@@ -38,7 +38,7 @@ public function __construct(ContentNegotiation $negotiation) { ...@@ -38,7 +38,7 @@ public function __construct(ContentNegotiation $negotiation) {
*/ */
public function enhance(array $defaults, Request $request) { public function enhance(array $defaults, Request $request) {
if (empty($defaults['_controller']) && !empty($defaults['_form']) && $this->negotiation->getContentType($request) === 'html') { if (empty($defaults['_controller']) && !empty($defaults['_form']) && $this->negotiation->getContentType($request) === 'html') {
$defaults['_controller'] = '\Drupal\Core\HtmlFormController::content'; $defaults['_controller'] = '\Drupal\Core\Controller\HtmlFormController::content';
} }
return $defaults; return $defaults;
} }
......
<?php
/**
* @file
* Contains \Drupal\Core\Routing\Enhancer\ModalEnhancer.
*/
namespace Drupal\Core\Routing\Enhancer;
/**
* Enhances a route to use the DialogController for matching requests.
*/
class ModalEnhancer extends DialogEnhancer {
/**
* Content type this enhancer targets.
*
* @var string
*/
protected $targetContentType = 'drupal_modal';
/**
* Controller to route matching requests to.
*
* @var string
*/
protected $controller = '\Drupal\Core\Ajax\DialogController::modal';
}
<?php
/**
* @file
* Contains \Drupal\Core\Routing\Enhancer\PageEnhancer.
*/
namespace Drupal\Core\Routing\Enhancer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface;
use Drupal\Core\ContentNegotiation;
/**
* Enhances a page route with the appropriate controller.
*/
class PageEnhancer implements RouteEnhancerInterface {
/**
* Content negotiation library.
*
* @var \Drupal\CoreContentNegotiation
*/
protected $negotiation;
/**
* Constructs a new \Drupal\Core\Routing\Enhancer\PageEnhancer object.
*
* @param \Drupal\Core\ContentNegotiation $negotiation
* The Content Negotiation service.
*/
public function __construct(ContentNegotiation $negotiation) {
$this->negotiation = $negotiation;
}
/**
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request) {
if (empty($defaults['_controller']) && !empty($defaults['_content']) && $this->negotiation->getContentType($request) === 'html') {
$defaults['_controller'] = '\Drupal\Core\HtmlPageController::content';
}
return $defaults;
}
}
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\ControllerResolver; use Drupal\Core\Controller\ControllerResolver;
use Drupal\simpletest\UnitTestBase; use Drupal\simpletest\UnitTestBase;
/** /**
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\system\Tests\System; namespace Drupal\system\Tests\System;
use \Drupal\Core\ContentNegotiation; use \Drupal\Core\ContentNegotiation;
use \Drupal\Core\ExceptionController; use \Drupal\Core\Controller\ExceptionController;
use \Drupal\simpletest\UnitTestBase; use \Drupal\simpletest\UnitTestBase;
use \Symfony\Component\HttpFoundation\Request; use \Symfony\Component\HttpFoundation\Request;
use \Symfony\Component\HttpKernel\Exception\FlattenException; use \Symfony\Component\HttpKernel\Exception\FlattenException;
......
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