diff --git a/core/core.services.yml b/core/core.services.yml index 47bb2e2..557d757 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -274,9 +274,6 @@ services: arguments: ['@url_generator', '@module_handler', '@language_manager'] calls: - [setRequest, ['@?request']] - url_factory: - class: Drupal\Core\UrlFactory - arguments: ['@url_generator'] router.dynamic: class: Symfony\Cmf\Component\Routing\DynamicRouter arguments: ['@router.request_context', '@router.matcher', '@url_generator'] diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index c7916c3..b9c7493 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -9,6 +9,7 @@ use Drupal\Core\DependencyInjection\DependencySerialization; use Drupal\Core\Routing\UrlGeneratorInterface; +use Symfony\Component\HttpFoundation\Request; /** * Defines an object that holds information about an internal route. @@ -16,6 +17,13 @@ class Url extends DependencySerialization { /** + * The URL generator. + * + * @var \Drupal\Core\Routing\UrlGeneratorInterface + */ + protected $urlGenerator; + + /** * The route name. * * @var string @@ -37,15 +45,10 @@ class Url extends DependencySerialization { protected $routeOptions; /** - * The URL generator. - * - * @var \Drupal\Core\Routing\UrlGeneratorInterface - */ - protected $urlGenerator; - - /** * Constructs a new Url object. * + * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator + * The URL generator. * @param string $route_name * The name of the route * @param array $route_parameters @@ -68,13 +71,33 @@ class Url extends DependencySerialization { * respectively. if mixed mode sessions are permitted, TRUE enforces HTTPS * and FALSE enforces HTTP. */ - public function __construct($route_name, $route_parameters = array(), $route_options = array()) { + public function __construct(UrlGeneratorInterface $url_generator, $route_name, $route_parameters = array(), $route_options = array()) { + $this->urlGenerator = $url_generator; $this->routeName = $route_name; $this->routeParameters = $route_parameters; $this->routeOptions = $route_options; } /** + * Returns the Url object matching a system path. + * + * @param string $path + * A system path (e.g. 'node/1'). + * + * @return static + * An Url object. + */ + public static function createFromPath($path) { + // Look up the route name and parameters used for the given path. + $request = Request::create('/' . $path); + $request->attributes->set('_system_path', $path); + + if ($result = \Drupal::service('router')->matchRequest($request)) { + return new static(\Drupal::urlGenerator(), $result['_route'], $result['_raw_variables']->all()); + } + } + + /** * Returns the route name. * * @return string @@ -105,8 +128,7 @@ public function getRouteOptions() { * Generates the path for this Url object. */ public function toString() { - // @todo Refactor UrlGenerator to accept $this instead of three arguments? - $this->urlGenerator()->generateFromRoute($this->routeName, $this->routeParameters, $this->routeOptions); + $this->urlGenerator->generateFromRoute($this->routeName, $this->routeParameters, $this->routeOptions); } /** @@ -123,27 +145,4 @@ public function toArray() { ); } - /** - * Gets the URL generator. - * - * @return \Drupal\Core\Routing\UrlGeneratorInterface - * The URL generator. - */ - protected function urlGenerator() { - if (!$this->urlGenerator) { - $this->urlGenerator = \Drupal::urlGenerator(); - } - return $this->urlGenerator; - } - - /** - * Sets the URL generator. - * - * @param \Drupal\Core\Routing\UrlGeneratorInterface - * The URL generator. - */ - public function setUrlGenerator(UrlGeneratorInterface $url_generator) { - $this->urlGenerator = $url_generator; - } - } diff --git a/core/lib/Drupal/Core/UrlFactoryInterface.php b/core/lib/Drupal/Core/UrlFactoryInterface.php deleted file mode 100644 index ef48705..0000000 --- a/core/lib/Drupal/Core/UrlFactoryInterface.php +++ /dev/null @@ -1,37 +0,0 @@ -urlGenerator = $url_generator; - } - - /** - * {@inheritdoc} - */ - public function create($route_name, $route_parameters = array(), $route_options = array()) { - return new Url($route_name, $route_parameters, $route_options); - } - - /** - * {@inheritdoc} - */ - public function createFromPath($path) { - // Look up the route name and parameters used for the given path. - $request = Request::create('/' . $path); - $request->attributes->set('_system_path', $path); - try { - $result = \Drupal::service('router')->matchRequest($request); - - // @todo Inject the url generator here instead of letting Url retrieve - // it from the container? - return new Url($result['_route'], $result['_raw_variables']->all()); - } - catch (\Exception $e) { - // @todo Throw a helpful exception or something. - } - } - -} 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 346e8e4..45e7616 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 @@ -8,6 +8,7 @@ namespace Drupal\menu_link\Entity; use Drupal\Core\Language\Language; +use Drupal\Core\Url; use Drupal\menu_link\MenuLinkInterface; use Symfony\Component\Routing\Route; use Symfony\Component\HttpFoundation\Request; @@ -547,7 +548,7 @@ public function preSave(EntityStorageControllerInterface $storage_controller) { } // Find the route_name. if (!isset($this->route_name)) { - if ($url = \Drupal::service('url_factory')->createFromPath($this->link_path)) { + if ($url = Url::createFromPath($this->link_path)) { $this->route_name = $url->getRouteName(); $this->route_parameters = $url->getRouteParameters(); }