diff --git a/pathauto.services.yml b/pathauto.services.yml index 748de19..3d3a760 100644 --- a/pathauto.services.yml +++ b/pathauto.services.yml @@ -10,7 +10,7 @@ services: arguments: ['@config.factory', '@path.alias_storage', '@database','@pathauto.verbose_messenger', '@string_translation'] pathauto.alias_uniquifier: class: Drupal\pathauto\AliasUniquifier - arguments: ['@config.factory', '@pathauto.alias_storage_helper','@module_handler', '@router.no_access_checks', '@path.alias_manager'] + arguments: ['@config.factory', '@pathauto.alias_storage_helper','@module_handler', '@router.route_provider', '@path.alias_manager'] pathauto.verbose_messenger: class: Drupal\pathauto\VerboseMessenger arguments: ['@config.factory', '@current_user'] diff --git a/src/AliasUniquifier.php b/src/AliasUniquifier.php index 902a03e..9527e9a 100644 --- a/src/AliasUniquifier.php +++ b/src/AliasUniquifier.php @@ -12,9 +12,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Path\AliasManagerInterface; -use Drupal\Core\Path\AliasStorageInterface; -use Symfony\Component\Routing\Exception\ResourceNotFoundException; -use Symfony\Component\Routing\Matcher\UrlMatcherInterface; +use Drupal\Core\Routing\RouteProviderInterface; /** * Provides a utility for creating a unique path alias. @@ -43,11 +41,11 @@ class AliasUniquifier implements AliasUniquifierInterface { protected $moduleHandler; /** - * The url matcher service. + * The route provider service. * - * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface + * @var \Drupal\Core\Routing\RouteProviderInterface. */ - protected $urlMatcher; + protected $routeProvider; /** * The alias manager. @@ -57,15 +55,6 @@ class AliasUniquifier implements AliasUniquifierInterface { protected $aliasManager; /** - * Stores the last matching route name. - * - * Used to prevent a loop if the same route matches a given pattern. - * - * @var - */ - protected $lastRouteName; - - /** * Creates a new AliasUniquifier. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory @@ -74,14 +63,14 @@ class AliasUniquifier implements AliasUniquifierInterface { * The alias storage helper. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. - * @param \Symfony\Component\Routing\Matcher\UrlMatcherInterface $url_matcher + * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider * The url matcher service. */ - public function __construct(ConfigFactoryInterface $config_factory, AliasStorageHelperInterface $alias_storage_helper, ModuleHandlerInterface $module_handler, UrlMatcherInterface $url_matcher, AliasManagerInterface $alias_manager) { + public function __construct(ConfigFactoryInterface $config_factory, AliasStorageHelperInterface $alias_storage_helper, ModuleHandlerInterface $module_handler, RouteProviderInterface $route_provider, AliasManagerInterface $alias_manager) { $this->configFactory = $config_factory; $this->aliasStorageHelper = $alias_storage_helper; $this->moduleHandler = $module_handler; - $this->urlMatcher = $url_matcher; + $this->routeProvider = $route_provider; $this->aliasManager = $alias_manager; } @@ -118,6 +107,7 @@ class AliasUniquifier implements AliasUniquifierInterface { if ($existing_source != $alias) { // If it is an alias for the provided source, it is allowed to keep using // it. If not, then it is reserved. + $this->lastRouteName = NULL; return $existing_source != $source; } @@ -167,20 +157,17 @@ class AliasUniquifier implements AliasUniquifierInterface { return TRUE; } - try { - $route = $this->urlMatcher->match($path); + $routes = $this->routeProvider->getRoutesByPattern($path); - if ($route['_route'] == $this->lastRouteName) { - throw new \InvalidArgumentException('The path "' . $path . '" collides with the route with identifier ' . $this->lastRouteName . ', whose path is ' . $route['_route_object']->getPath()); + // Only return true for an exact match, ignore placeholders. + foreach ($routes as $route) { + if ($route->getPath() == $path) { + return TRUE; } - - $this->lastRouteName = $route['_route']; - return TRUE; - } - catch (ResourceNotFoundException $e) { - $this->lastRouteName = NULL; - return FALSE; } + + return FALSE; + } }