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:
class: Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher
arguments: ['@service_container']
controller_resolver:
class: Drupal\Core\ControllerResolver
class: Drupal\Core\Controller\ControllerResolver
arguments: ['@service_container']
http_kernel:
class: Drupal\Core\HttpKernel
......@@ -264,13 +264,8 @@ services:
tags:
- { name: event_subscriber }
arguments: ['@settings']
route_enhancer.modal:
class: Drupal\Core\Routing\Enhancer\ModalEnhancer
arguments: ['@content_negotiation']
tags:
- { name: route_enhancer, priority: 40 }
route_enhancer.dialog:
class: Drupal\Core\Routing\Enhancer\DialogEnhancer
route_enhancer.content_controller:
class: Drupal\Core\Routing\Enhancer\ContentControllerEnhancer
arguments: ['@content_negotiation']
tags:
- { name: route_enhancer, priority: 30 }
......@@ -290,11 +285,12 @@ services:
arguments: ['@content_negotiation']
tags:
- { name: route_enhancer, priority: 10 }
route_enhancer.page:
class: Drupal\Core\Routing\Enhancer\PageEnhancer
arguments: ['@content_negotiation']
tags:
- { name: route_enhancer, priority: 0 }
controller.page:
class: Drupal\Core\Controller\HtmlPageController
arguments: ['@http_kernel']
controller.dialog:
class: Drupal\Core\Controller\DialogController
arguments: ['@http_kernel']
router_listener:
class: Symfony\Component\HttpKernel\EventListener\RouterListener
tags:
......@@ -366,7 +362,7 @@ services:
- { name: event_subscriber }
arguments: ['@language_manager']
exception_controller:
class: Drupal\Core\ExceptionController
class: Drupal\Core\Controller\ExceptionController
arguments: ['@content_negotiation']
calls:
- [setContainer, ['@service_container']]
......
......@@ -2,18 +2,16 @@
/**
* @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\InsertCommand;
use Drupal\Core\Ajax\PrependCommand;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* 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 @@
/**
* @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\Log\LoggerInterface;
......@@ -79,7 +79,8 @@ protected function createController($controller) {
if (!class_exists($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);
}
else {
......
......@@ -2,22 +2,36 @@
/**
* @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\OpenModalDialogCommand;
use Drupal\Core\Ajax\OpenDialogCommand;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* 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.
......@@ -45,7 +59,7 @@ protected function forward(Request $request, $content) {
// controller.
$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 @@
/**
* @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\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Drupal\Core\ContentNegotiation;
/**
* This controller handles HTTP errors generated by the routing system.
*/
......@@ -107,7 +109,7 @@ public function on403Html(FlattenException $exception, Request $request) {
drupal_static_reset('menu_set_active_trail');
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');
}
else {
......@@ -180,7 +182,7 @@ public function on404Html(FlattenException $exception, Request $request) {
drupal_static_reset('menu_set_active_trail');
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');
}
else {
......
......@@ -2,10 +2,10 @@
/**
* @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\Response;
......
......@@ -2,36 +2,34 @@
/**
* @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\Response;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* 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
* The service container this object should use.
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $kernel
*/
public function setContainer(ContainerInterface $container = NULL) {
$this->container = $container;
public function __construct(HttpKernelInterface $kernel) {
$this->httpKernel = $kernel;
}
/**
......@@ -60,7 +58,7 @@ public function content(Request $request, $_content) {
$attributes->remove('system_path');
$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.
if ($response->isOk()) {
......
......@@ -2,28 +2,17 @@
/**
* @file
* Contains \Drupal\Core\ControllerInterface.
* Contains \Drupal\Core\ControllerInterface;
*/
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.
*
* @deprecated Use \Drupal\Core\Controller\ControllerInterface instead.
*/
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);
interface ControllerInterface extends BaseInterface {
}
......@@ -7,7 +7,7 @@
namespace Drupal\Core\Entity;
use Drupal\Core\HtmlFormController;
use Drupal\Core\Controller\HtmlFormController;
use Symfony\Component\HttpFoundation\Request;
/**
......@@ -16,7 +16,7 @@
class HtmlEntityFormController extends HtmlFormController {
/**
* Overrides \Drupal\Core\HtmlFormController::content().
* {@inheritdoc}
*
* Due to reflection, the argument must be named $_entity_form. The parent
* method has $request and $_form, but the parameter must match the route.
......@@ -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
* representing the entity and operation being performed.
......
......@@ -39,7 +39,7 @@ public function __construct(ContentNegotiation $negotiation) {
public function enhance(array $defaults, Request $request) {
if (empty($defaults['_content']) && $this->negotiation->getContentType($request) == 'drupal_ajax') {
$defaults['_content'] = $defaults['_controller'];
$defaults['_controller'] = '\Drupal\Core\AjaxController::content';
$defaults['_controller'] = '\Drupal\Core\Controller\AjaxController::content';
}
return $defaults;
}
......
......@@ -2,7 +2,7 @@
/**
* @file
* Contains \Drupal\Core\Routing\Enhancer\DialogEnhancer.
* Contains \Drupal\Core\Routing\Enhancer\ContentControllerEnhancer.
*/
namespace Drupal\Core\Routing\Enhancer;
......@@ -12,33 +12,30 @@
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.
*
* @var \Drupal\CoreContentNegotiation
* @var \Drupal\Core\ContentNegotiation
*/
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.
*
* @var string
*/
protected $controller = '\Drupal\Core\Ajax\DialogController::dialog';
/**
* Constructs a new \Drupal\Core\Routing\Enhancer\AjaxEnhancer object.
* Constructs a new ContentControllerEnhancer object.
*
* @param \Drupal\Core\ContentNegotiation $negotiation
* The Content Negotiation service.
......@@ -51,8 +48,11 @@ public function __construct(ContentNegotiation $negotiation) {
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request) {
if (empty($defaults['_controller']) && !empty($defaults['_content']) && $this->negotiation->getContentType($request) == $this->targetContentType) {
$defaults['_controller'] = $this->controller;
if (empty($defaults['_controller']) && !empty($defaults['_content'])) {
$type = $this->negotiation->getContentType($request);
if (isset($this->types[$type])) {
$defaults['_controller'] = $this->types[$type];
}
}
return $defaults;
}
......
......@@ -38,7 +38,7 @@ public function __construct(ContentNegotiation $negotiation) {
*/
public function enhance(array $defaults, Request $request) {
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;
}
......
<?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 @@
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\ControllerResolver;
use Drupal\Core\Controller\ControllerResolver;
use Drupal\simpletest\UnitTestBase;
/**
......
......@@ -7,7 +7,7 @@
namespace Drupal\system\Tests\System;
use \Drupal\Core\ContentNegotiation;
use \Drupal\Core\ExceptionController;
use \Drupal\Core\Controller\ExceptionController;
use \Drupal\simpletest\UnitTestBase;
use \Symfony\Component\HttpFoundation\Request;
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