commit 76cbb9a225361bc286f90b7c5af2122638f4e1f3 Author: Tim Plunkett Date: Wed Jan 29 12:20:05 2014 -0800 Do not use exceptions for getters diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index 303a3e8..598a531 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -109,9 +109,19 @@ public static function createFromPath($path) { $url->setExternal(); return $url; } - // Look up the route name and parameters used for the given path. - $result = \Drupal::service('router')->match('/' . $path); - return new static($result['_route'], $result['_raw_variables']->all()); + + // Special case the front page route. + if ($path == '') { + $route_name = $path; + $route_parameters = array(); + } + else { + // Look up the route name and parameters used for the given path. + $result = \Drupal::service('router')->match('/' . $path); + $route_name = $result[RouteObjectInterface::ROUTE_NAME]; + $route_parameters = $result['_raw_variables']->all(); + } + return new static($route_name, $route_parameters); } /** @@ -128,12 +138,16 @@ public static function createFromRequest(Request $request) { $route_parameters = $request->attributes->get('_raw_variables')->all(); return new static($route_name, $route_parameters); } - elseif (($path = $request->attributes->get('_system_path')) && UrlHelper::isExternal($path)) { - $url = new static($path); - $url->setExternal(); - return $url; + + // Use the system path if it is set, otherwise use the requested path. + if ($request->attributes->has('_system_path')) { + $path = $request->attributes->get('_system_path'); } - throw new \Exception('asdf'); + else { + $path = $request->getPathInfo(); + } + + return static::createFromPath($path); } /** @@ -145,7 +159,8 @@ protected function setExternal() { $this->external = TRUE; // What was passed in as the route name is actually the path. $this->path = $this->routeName; - $this->routeName = NULL; + $this->routeName = ''; + $this->routeParameters = array(); return $this; } @@ -164,9 +179,6 @@ public function isExternal() { * @return string */ public function getRouteName() { - if ($this->isExternal()) { - throw new \Exception('External URLs do not have route names.'); - } return $this->routeName; } @@ -176,9 +188,6 @@ public function getRouteName() { * @return array */ public function getRouteParameters() { - if ($this->isExternal()) { - throw new \Exception('External URLs do not have route parameters.'); - } return $this->routeParameters; } @@ -284,9 +293,6 @@ public function toString() { * An associative array containing all the properties of the route. */ public function toArray() { - if ($this->isExternal()) { - throw new \Exception('External URLs do not have route metadata.'); - } return array( 'route_name' => $this->getRouteName(), 'route_parameters' => $this->getRouteParameters(), diff --git a/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php b/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php index d5d358b..d88002b 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php @@ -11,7 +11,6 @@ use Drupal\Core\Entity\EntityStorageControllerInterface; use Drupal\Core\Url; use Drupal\menu_link\MenuLinkInterface; -use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Route; /** @@ -463,10 +462,6 @@ public function preSave(EntityStorageControllerInterface $storage_controller) { // This is the easiest way to handle the unique internal path '', // since a path marked as external does not need to match a route. $this->external = (url_is_external($this->link_path) || $this->link_path == '') ? 1 : 0; - if ($this->link_path == '') { - $this->route_name = ''; - $this->route_parameters = array(); - } // Try to find a parent link. If found, assign it and derive its menu. $parent = $this->findParent($storage_controller); @@ -511,20 +506,9 @@ public function preSave(EntityStorageControllerInterface $storage_controller) { // Find the route_name. if (!isset($this->route_name)) { - $route_name = ''; - $route_parameters = array(); - if (!$this->external) { - try { - $url = Url::createFromPath($this->link_path); - $route_name = $url->getRouteName(); - $route_parameters = $url->getRouteParameters(); - } - catch (ResourceNotFoundException $e) { - } - } - - $this->route_name = $route_name; - $this->route_parameters = $route_parameters; + $url = Url::createFromPath($this->link_path); + $this->route_name = $url->getRouteName(); + $this->route_parameters = $url->getRouteParameters(); } elseif (empty($this->link_path)) { $this->link_path = \Drupal::urlGenerator()->getPathFromRoute($this->route_name, $this->route_parameters); diff --git a/core/tests/Drupal/Tests/Core/ExternalUrlTest.php b/core/tests/Drupal/Tests/Core/ExternalUrlTest.php index ebbb5bd..e0baaa7 100644 --- a/core/tests/Drupal/Tests/Core/ExternalUrlTest.php +++ b/core/tests/Drupal/Tests/Core/ExternalUrlTest.php @@ -131,11 +131,14 @@ public function testToString(Url $url) { * @depends testCreateFromPath * * @covers ::toArray() - * - * @expectedException \Exception */ public function testToArray(Url $url) { - $this->assertNull($url->toArray()); + $expected = array( + 'route_name' => '', + 'route_parameters' => array(), + 'options' => array(), + ); + $this->assertSame($expected, $url->toArray()); } /** @@ -144,11 +147,9 @@ public function testToArray(Url $url) { * @depends testCreateFromPath * * @covers ::getRouteName() - * - * @expectedException \Exception */ public function testGetRouteName(Url $url) { - $this->assertNull($url->getRouteName()); + $this->assertSame('', $url->getRouteName()); } /** @@ -157,11 +158,9 @@ public function testGetRouteName(Url $url) { * @depends testCreateFromPath * * @covers ::getRouteParameters() - * - * @expectedException \Exception */ public function testGetRouteParameters(Url $url) { - $this->assertNull($url->getRouteParameters()); + $this->assertSame(array(), $url->getRouteParameters()); } /** diff --git a/core/tests/Drupal/Tests/Core/UrlTest.php b/core/tests/Drupal/Tests/Core/UrlTest.php index 8719fd1..c3faf86 100644 --- a/core/tests/Drupal/Tests/Core/UrlTest.php +++ b/core/tests/Drupal/Tests/Core/UrlTest.php @@ -115,6 +115,16 @@ public function testCreateFromPath() { } /** + * Tests the createFromPath method with the special path. + * + * @covers ::createFromPath() + */ + public function testCreateFromPathFront() { + $url = Url::createFromPath(''); + $this->assertSame('', $url->getRouteName()); + } + + /** * Tests that an invalid path will thrown an exception. * * @covers ::createFromPath()