class RenameAdminPathsEventSubscriber in Rename Admin Paths 8.2
Hierarchy
- class \Drupal\rename_admin_paths\EventSubscriber\RenameAdminPathsEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of RenameAdminPathsEventSubscriber
3 files declare their use of RenameAdminPathsEventSubscriber
- RenameAdminPathEventSubscriberTest.php in tests/
src/ Unit/ EventSubscriber/ RenameAdminPathEventSubscriberTest.php - RenameAdminPathsSettingsForm.php in src/
Form/ RenameAdminPathsSettingsForm.php - RenameAdminPathsValidator.php in src/
Form/ RenameAdminPathsValidator.php
File
- src/
EventSubscriber/ RenameAdminPathsEventSubscriber.php, line 12
Namespace
Drupal\rename_admin_paths\EventSubscriberView source
class RenameAdminPathsEventSubscriber implements EventSubscriberInterface {
/**
* list of admin paths.
*
* @var array
*/
const ADMIN_PATHS = [
'admin',
'user',
];
/**
* @var Config
*/
private $config;
/**
* @param Config $config
*/
public function __construct(Config $config) {
$this->config = $config;
}
/**
* Use a very low priority so we are sure all routes are correctly marked as
* admin route which is mostly done in other event subscribers like the
* AdminRouteSubscriber
*
* @return array
*/
public static function getSubscribedEvents() {
return [
RoutingEvents::ALTER => [
[
'onRoutesAlter',
-2048,
],
],
];
}
/**
* @param RouteBuildEvent $event
*/
public function onRoutesAlter(RouteBuildEvent $event) {
foreach (self::ADMIN_PATHS as $path) {
if ($this->config
->isPathEnabled($path)) {
$this
->alterRouteCollection($event
->getRouteCollection(), $path, $this->config
->getPathValue($path));
}
}
}
/**
* @param RouteCollection $routeCollection
* @param string $from
* @param string $to
*/
private function alterRouteCollection(RouteCollection $routeCollection, string $from, string $to) : void {
foreach ($routeCollection as $route) {
$this
->replaceRoutePath($route, $from, $to);
}
}
/**
* @param Route $route
* @param string $from
* @param string $to
*/
private function replaceRoutePath(Route $route, string $from, string $to) : void {
if ($this
->matchRouteByPath($route, $from)) {
$route
->setPath(preg_replace(sprintf('~^/%s~', $from), sprintf('/%s', $to), $route
->getPath(), 1));
}
}
/**
* @param Route $route
* @param string $path
*
* @return boolean
*
* match /path, /path/ and /path/* but not /path*
*/
private function matchRouteByPath(Route $route, string $path) : bool {
return (bool) preg_match(sprintf('~^/%s(?:/(.*))?$~', $path), $route
->getPath());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RenameAdminPathsEventSubscriber:: |
private | property | ||
RenameAdminPathsEventSubscriber:: |
constant | list of admin paths. | ||
RenameAdminPathsEventSubscriber:: |
private | function | ||
RenameAdminPathsEventSubscriber:: |
public static | function | Use a very low priority so we are sure all routes are correctly marked as admin route which is mostly done in other event subscribers like the AdminRouteSubscriber | |
RenameAdminPathsEventSubscriber:: |
private | function | ||
RenameAdminPathsEventSubscriber:: |
public | function | ||
RenameAdminPathsEventSubscriber:: |
private | function | ||
RenameAdminPathsEventSubscriber:: |
public | function |