diff --git a/core/core.services.yml b/core/core.services.yml index 317af02..9badfe5 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -222,6 +222,10 @@ services: synchronized: true request_stack: class: Symfony\Component\HttpFoundation\RequestStack + request_info: + class: Drupal\Core\Routing\RequestInfo + calls: + - [setRequest, ['@?request=']] event_dispatcher: class: Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher arguments: ['@service_container'] @@ -430,7 +434,7 @@ services: arguments: ['@controller_resolver', '@service_container', '@entity.manager'] controller.dialog: class: Drupal\Core\Controller\DialogController - arguments: ['@controller_resolver', '@title_resolver'] + arguments: ['@controller_resolver', '@title_resolver', '@request_info'] ajax_response_renderer: class: Drupal\Core\Ajax\AjaxResponseRenderer router_listener: diff --git a/core/includes/menu.inc b/core/includes/menu.inc index a1c8a01..1dfc96c 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -554,7 +554,7 @@ function menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail = // Load the request corresponding to the current page. $request = \Drupal::request(); $system_path = NULL; - if ($route_name = $request->attributes->get(RouteObjectInterface::ROUTE_NAME)) { + if ($route_name = \Drupal::service('request_info')->getRouteName()) { // @todo https://drupal.org/node/2068471 is adding support so we can tell // if this is called on a 404/403 page. $system_path = $request->attributes->get('_system_path'); @@ -1182,7 +1182,7 @@ function menu_local_tasks($level = 0) { $data['tabs'] = array(); $data['actions'] = array(); - $route_name = \Drupal::request()->attributes->get(RouteObjectInterface::ROUTE_NAME); + $route_name = \Drupal::service('request_info')->getRouteName(); if (!empty($route_name)) { $manager = \Drupal::service('plugin.manager.menu.local_task'); $local_tasks = $manager->getTasksBuild($route_name); @@ -1237,7 +1237,7 @@ function menu_secondary_local_tasks() { */ function menu_get_local_actions() { $links = menu_local_tasks(); - $route_name = Drupal::request()->attributes->get(RouteObjectInterface::ROUTE_NAME); + $route_name = \Drupal::service('request_info')->getRouteName(); $manager = \Drupal::service('plugin.manager.menu.local_action'); return $manager->getActionsForRoute($route_name) + $links['actions']; } diff --git a/core/lib/Drupal/Core/Controller/DialogController.php b/core/lib/Drupal/Core/Controller/DialogController.php index 5fda730..02baf80 100644 --- a/core/lib/Drupal/Core/Controller/DialogController.php +++ b/core/lib/Drupal/Core/Controller/DialogController.php @@ -10,6 +10,7 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\OpenDialogCommand; use Drupal\Core\Page\HtmlPage; +use Drupal\Core\Routing\RequestInfo; use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -34,16 +35,26 @@ class DialogController { protected $titleResolver; /** + * The request info. + * + * @var \Drupal\Core\Routing\RequestInfo + */ + protected $requestInfo; + + /** * Constructs a new DialogController. * * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver * The controller resolver service. * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver * The title resolver. + * @param \Drupal\Core\Routing\RequestInfo $request_info + * The request info. */ - public function __construct(ControllerResolverInterface $controller_resolver, TitleResolverInterface $title_resolver) { + public function __construct(ControllerResolverInterface $controller_resolver, TitleResolverInterface $title_resolver, RequestInfo $request_info) { $this->controllerResolver = $controller_resolver; $this->titleResolver = $title_resolver; + $this->requestInfo = $request_info; } /** @@ -96,7 +107,7 @@ public function dialog(Request $request, $_content, $modal = FALSE) { // @todo Remove use of drupal_get_title() when // http://drupal.org/node/1871596 is in. - if (!$title = $this->titleResolver->getTitle($request, $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT))) { + if (!$title = $this->titleResolver->getTitle($request, $this->requestInfo->getRouteObject($request))) { // @todo Remove use of drupal_get_title() when // http://drupal.org/node/1871596 is in. $title = drupal_get_title(); @@ -125,7 +136,7 @@ public function dialog(Request $request, $_content, $modal = FALSE) { } else { // Generate a target based on the route id. - $route_name = $request->attributes->get(RouteObjectInterface::ROUTE_NAME); + $route_name = $this->requestInfo->getRouteName($request); $target = '#' . drupal_html_id("drupal-dialog-$route_name"); } } diff --git a/core/lib/Drupal/Core/Routing/RequestInfo.php b/core/lib/Drupal/Core/Routing/RequestInfo.php new file mode 100644 index 0000000..c4dfcb4 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/RequestInfo.php @@ -0,0 +1,104 @@ +request = $request; + return $this; + } + + /** + * Gets the current request. + * + * @return \Symfony\Component\HttpFoundation\Request + */ + public function getRequest() { + if (!$this->request) { + $this->setRequest(\Drupal::request()); + } + return $this->request; + } + + /** + * Get the route name. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * (optional) The request. + * + * @return string + */ + public function getRouteName(Request $request = NULL) { + $request = $request ?: $this->request; + return $request->attributes->get(RouteObjectInterface::ROUTE_NAME); + } + + /** + * Get the route object. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * (optional) The request. + * + * @return \Symfony\Component\Routing\Route + */ + public function getRouteObject(Request $request = NULL) { + $request = $request ?: $this->request; + return $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT); + } + + /** + * Gets the internal path. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * (optional) The request. + * + * @return string + * The internal requested path without path aliases. + */ + public function getSystemPath(Request $request = NULL) { + $request = $request ?: $this->request; + return $request->attributes->get('_system_path'); + } + + /** + * Gets the raw variables. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * (optional) The request. + * + * @return \Symfony\Component\HttpFoundation\ParameterBag + * The original variables prior to conversion by the ParamConverterManager. + */ + public function getRawVariables(Request $request = NULL) { + $request = $request ?: $this->request; + return $request->attributes->get('_raw_variables'); + } + +} diff --git a/core/modules/block/block.module b/core/modules/block/block.module index e97852c..16eb81d 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -119,7 +119,7 @@ function block_page_build(&$page) { // Fetch a list of regions for the current theme. $all_regions = system_region_list($theme); - if (\Drupal::request()->attributes->get(RouteObjectInterface::ROUTE_NAME) != 'block.admin_demo') { + if (\Drupal::service('request_info')->getRouteName() != 'block.admin_demo') { // Load all region content assigned via blocks. foreach (array_keys($all_regions) as $region) { // Assign blocks to region. diff --git a/core/tests/Drupal/Tests/Core/Routing/RequestInfoTest.php b/core/tests/Drupal/Tests/Core/Routing/RequestInfoTest.php new file mode 100644 index 0000000..f4e33dd --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Routing/RequestInfoTest.php @@ -0,0 +1,128 @@ + 'Request info', + 'description' => 'Tests the RequestInfo class.', + 'group' => 'Routing', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + $this->requestInfo = new RequestInfo(); + } + + /** + * Test the getRouteName(). + * + * @covers ::getRouteName + */ + public function testGetRouteName() { + $request_1 = new Request(array(), array(), array(RouteObjectInterface::ROUTE_NAME => 'route_1')); + $request_2 = new Request(array(), array(), array(RouteObjectInterface::ROUTE_NAME => 'route_2')); + + $this->assertSame('route_1', $this->requestInfo->getRouteName($request_1)); + + $this->requestInfo->setRequest($request_1); + $this->assertSame('route_1', $this->requestInfo->getRouteName()); + + $this->assertSame('route_2', $this->requestInfo->getRouteName($request_2)); + } + + /** + * Test the getRouteObject(). + * + * @covers ::getRouteObject + */ + public function testGetRouteObject() { + $route_1 = new Route(''); + $route_2 = new Route(''); + $request_1 = new Request(array(), array(), array(RouteObjectInterface::ROUTE_OBJECT => $route_1)); + $request_2 = new Request(array(), array(), array(RouteObjectInterface::ROUTE_OBJECT => $route_2)); + + $this->assertSame($route_1, $this->requestInfo->getRouteObject($request_1)); + + $this->requestInfo->setRequest($request_1); + $this->assertSame($route_1, $this->requestInfo->getRouteObject()); + + $this->assertSame($route_2, $this->requestInfo->getRouteObject($request_2)); + } + + /** + * Test the getSystemPath(). + * + * @covers ::getSystemPath + */ + public function testGetSystemPath() { + $request_1 = new Request(array(), array(), array('_system_path' => 'path_1')); + $request_2 = new Request(array(), array(), array('_system_path' => 'path_2')); + + $this->assertSame('path_1', $this->requestInfo->getSystemPath($request_1)); + + $this->requestInfo->setRequest($request_1); + $this->assertSame('path_1', $this->requestInfo->getSystemPath()); + + $this->assertSame('path_2', $this->requestInfo->getSystemPath($request_2)); + } + + /** + * Test the getRawVariables(). + * + * @covers ::getRawVariables + */ + public function testGetRawVariables() { + $raw_1 = new ParameterBag(); + $raw_1->set('foo', 'bar'); + + $raw_2 = new ParameterBag(); + $raw_2->set('baz', 'bob'); + + $request_1 = new Request(array(), array(), array('_raw_variables' => $raw_1)); + $request_2 = new Request(array(), array(), array('_raw_variables' => $raw_2)); + + $this->assertSame('bar', $this->requestInfo->getRawVariables($request_1)->get('foo')); + + $this->requestInfo->setRequest($request_1); + $this->assertSame('bar', $this->requestInfo->getRawVariables()->get('foo')); + + $this->assertSame('bob', $this->requestInfo->getRawVariables($request_2)->get('baz')); + } + +}