diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index d7b605b..7003830 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -8,14 +8,15 @@ namespace Drupal\Core; use Drupal\Component\Utility\Url as UrlHelper; +use Drupal\Core\DependencyInjection\DependencySerialization; use Drupal\Core\Routing\UrlGeneratorInterface; use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; /** - * Defines an object that holds information about an internal route. + * Defines an object that holds information about a URL. */ -class Url { +class Url extends DependencySerialization { /** * The URL generator. @@ -39,7 +40,7 @@ class Url { protected $routeParameters = array(); /** - * The route options. + * The URL options. * * @var array */ @@ -93,15 +94,16 @@ public function __construct($route_name, $route_parameters = array(), $options = } /** - * Returns the Url object matching a system path. + * Returns the Url object matching a path. * * @param string $path - * A system path (e.g. 'node/1'). + * A path (e.g. 'node/1', 'http://drupal.org'). * * @return static * An Url object. * * @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException + * Thrown when the path cannot be matched. */ public static function createFromPath($path) { if (UrlHelper::isExternal($path)) { @@ -132,22 +134,15 @@ public static function createFromPath($path) { * * @return static * A Url object. + * + * @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException + * Thrown when the request cannot be matched. */ public static function createFromRequest(Request $request) { - if ($route_name = $request->attributes->get(RouteObjectInterface::ROUTE_NAME)) { - $route_parameters = $request->attributes->get('_raw_variables')->all(); - return new static($route_name, $route_parameters); - } - - // 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'); - } - else { - $path = $request->getPathInfo(); - } - - return static::createFromPath($path); + $result = \Drupal::service('router')->matchRequest($request); + $route_name = $result[RouteObjectInterface::ROUTE_NAME]; + $route_parameters = $result['_raw_variables']->all(); + return new static($route_name, $route_parameters); } /** @@ -157,10 +152,14 @@ public static function createFromRequest(Request $request) { */ protected function setExternal() { $this->external = TRUE; + // What was passed in as the route name is actually the path. $this->path = $this->routeName; + + // Set empty route name and parameters. $this->routeName = ''; $this->routeParameters = array(); + return $this; } @@ -316,14 +315,6 @@ public function getInternalPath() { } /** - * {@inheritdoc} - */ - public function __sleep() { - unset($this->urlGenerator); - return array_keys(get_object_vars($this)); - } - - /** * Gets the URL generator. * * @return \Drupal\Core\Routing\UrlGeneratorInterface diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php b/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php index 6784bd0..3a3e3ba 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Entity/Shortcut.php @@ -10,10 +10,8 @@ use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\EntityStorageControllerInterface; use Drupal\Core\Field\FieldDefinition; -use Drupal\Core\Routing\UrlMatcher; use Drupal\Core\Url; use Drupal\shortcut\ShortcutInterface; -use Symfony\Component\Routing\Exception\ResourceNotFoundException; /** * Defines the shortcut entity class. @@ -126,13 +124,9 @@ public static function preCreate(EntityStorageControllerInterface $storage_contr public function preSave(EntityStorageControllerInterface $storage_controller) { parent::preSave($storage_controller); - try { - $url = Url::createFromPath($this->path->value); - $this->setRouteName($url->getRouteName()); - $this->setRouteParams($url->getRouteParameters()); - } - catch (ResourceNotFoundException $e) { - } + $url = Url::createFromPath($this->path->value); + $this->setRouteName($url->getRouteName()); + $this->setRouteParams($url->getRouteParameters()); } /** diff --git a/core/tests/Drupal/Tests/Core/ExternalUrlTest.php b/core/tests/Drupal/Tests/Core/ExternalUrlTest.php index e0baaa7..cb21fb8 100644 --- a/core/tests/Drupal/Tests/Core/ExternalUrlTest.php +++ b/core/tests/Drupal/Tests/Core/ExternalUrlTest.php @@ -11,6 +11,7 @@ use Drupal\Core\Url; use Drupal\Tests\UnitTestCase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; /** * Tests the \Drupal\Core\Url class for external paths. @@ -30,6 +31,13 @@ class ExternalUrlTest extends UnitTestCase { protected $urlGenerator; /** + * The router. + * + * @var \Drupal\Tests\Core\Routing\TestRouterInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $router; + + /** * An external URL to test. * * @var string @@ -60,7 +68,9 @@ protected function setUp() { return $path; })); + $this->router = $this->getMock('Drupal\Tests\Core\Routing\TestRouterInterface'); $container = new ContainerBuilder(); + $container->set('router', $this->router); $container->set('url_generator', $this->urlGenerator); \Drupal::setContainer($container); } @@ -82,23 +92,17 @@ public function testCreateFromPath() { * Tests the createFromRequest method. * * @covers ::createFromRequest() + * + * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException */ public function testCreateFromRequest() { - $request = new Request(array(), array(), array('_system_path' => $this->path)); - $url = Url::createFromRequest($request); - $this->assertInstanceOf('Drupal\Core\Url', $url); - $this->assertTrue($url->isExternal()); - } - - /** - * Tests the createFromRequest method. - * - * @covers ::createFromRequest() - * - * @expectedException \Exception - */ - public function testCreateFromRequestWithoutSystemPath() { $request = new Request(); + + $this->router->expects($this->once()) + ->method('matchRequest') + ->with($request) + ->will($this->throwException(new ResourceNotFoundException())); + $url = Url::createFromRequest($request); $this->assertNull($url); } diff --git a/core/tests/Drupal/Tests/Core/Routing/TestRouterInterface.php b/core/tests/Drupal/Tests/Core/Routing/TestRouterInterface.php new file mode 100644 index 0000000..6c2c21a --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Routing/TestRouterInterface.php @@ -0,0 +1,17 @@ +method('generateFromRoute') ->will($this->returnValueMap($this->map)); - $this->router = $this->getMock('Symfony\Component\Routing\RouterInterface'); + $this->router = $this->getMock('Drupal\Tests\Core\Routing\TestRouterInterface'); $container = new ContainerBuilder(); $container->set('router', $this->router); $container->set('url_generator', $this->urlGenerator); @@ -146,12 +148,18 @@ public function testCreateFromPathInvalid() { * @covers ::createFromRequest() */ public function testCreateFromRequest() { - $request = new Request(array(), array(), array( + $attributes = array( '_raw_variables' => new ParameterBag(array( 'color' => 'chartreuse', )), RouteObjectInterface::ROUTE_NAME => 'the_route_name', - )); + ); + $request = new Request(array(), array(), $attributes); + + $this->router->expects($this->once()) + ->method('matchRequest') + ->with($request) + ->will($this->returnValue($attributes)); $url = Url::createFromRequest($request); $expected = new Url('the_route_name', array('color' => 'chartreuse'));