Skip to content

Commit

Permalink
Allow to configure routeName and routePattern dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jul 10, 2022
1 parent d23a797 commit d529b4f
Showing 1 changed file with 60 additions and 64 deletions.
124 changes: 60 additions & 64 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,43 +539,14 @@ final public function getBaseRoutePattern(): string
}

if ($this->isChild()) { // the admin class is a child, prefix it with the parent route pattern
$baseRoutePattern = $this->baseRoutePattern;
if (null === $baseRoutePattern) {
preg_match(self::CLASS_REGEX, $this->getModelClass(), $matches);

if (!$matches) {
throw new \LogicException(sprintf(
'Please define a default `baseRoutePattern` value for the admin class `%s`',
static::class
));
}
$baseRoutePattern = $this->urlize($matches[5], '-');
}

$this->cachedBaseRoutePattern = sprintf(
'%s/%s/%s',
$this->getParent()->getBaseRoutePattern(),
$this->getParent()->getRouterIdParameter(),
$baseRoutePattern
$this->generateBaseRoutePattern(true)
);
} elseif (null !== $this->baseRoutePattern) {
$this->cachedBaseRoutePattern = $this->baseRoutePattern;
} else {
preg_match(self::CLASS_REGEX, $this->getModelClass(), $matches);

if (!$matches) {
throw new \LogicException(sprintf(
'Please define a default `baseRoutePattern` value for the admin class `%s`',
static::class
));
}

$this->cachedBaseRoutePattern = sprintf(
'/%s%s/%s',
'' === $matches[1] ? '' : $this->urlize($matches[1], '-').'/',
$this->urlize($matches[3], '-'),
$this->urlize($matches[5], '-')
);
$this->cachedBaseRoutePattern = $this->generateBaseRoutePattern();
}

return $this->cachedBaseRoutePattern;
Expand All @@ -593,44 +564,13 @@ final public function getBaseRouteName(): string
}

if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
$baseRouteName = $this->baseRouteName;
if (null === $baseRouteName) {
preg_match(self::CLASS_REGEX, $this->getModelClass(), $matches);

if (!$matches) {
throw new \LogicException(sprintf(
'Cannot automatically determine base route name,'
.' please define a default `baseRouteName` value for the admin class `%s`',
static::class
));
}
$baseRouteName = $this->urlize($matches[5]);
}

$this->cachedBaseRouteName = sprintf(
'%s_%s',
$this->getParent()->getBaseRouteName(),
$baseRouteName
$this->generateBaseRouteName(true)
);
} elseif (null !== $this->baseRouteName) {
$this->cachedBaseRouteName = $this->baseRouteName;
} else {
preg_match(self::CLASS_REGEX, $this->getModelClass(), $matches);

if (!$matches) {
throw new \LogicException(sprintf(
'Cannot automatically determine base route name,'
.' please define a default `baseRouteName` value for the admin class `%s`',
static::class
));
}

$this->cachedBaseRouteName = sprintf(
'admin_%s%s_%s',
'' === $matches[1] ? '' : $this->urlize($matches[1]).'_',
$this->urlize($matches[3]),
$this->urlize($matches[5])
);
$this->cachedBaseRouteName = $this->generateBaseRouteName();
}

return $this->cachedBaseRouteName;
Expand Down Expand Up @@ -1856,6 +1796,62 @@ protected function configure(): void
{
}

protected function generateBaseRoutePattern(bool $isChildAdmin = false): string
{
if (null !== $this->baseRoutePattern) {
return $this->baseRoutePattern;
}

preg_match(self::CLASS_REGEX, $this->getModelClass(), $matches);

if (!$matches) {
throw new \LogicException(sprintf(
'Please define a default `baseRoutePattern` value for the admin class `%s`',
static::class
));
}

if ($isChildAdmin) {
return $this->urlize($matches[5]);
}

return $this->cachedBaseRoutePattern = sprintf(
'/%s%s/%s',
'' === $matches[1] ? '' : $this->urlize($matches[1], '-').'/',
$this->urlize($matches[3], '-'),
$this->urlize($matches[5], '-')
);
}


protected function generateBaseRouteName(bool $isChildAdmin = false): string
{
if (null !== $this->baseRouteName) {
return $this->baseRouteName;
}

preg_match(self::CLASS_REGEX, $this->getModelClass(), $matches);

if (!$matches) {
throw new \LogicException(sprintf(
'Cannot automatically determine base route name,'
.' please define a default `baseRouteName` value for the admin class `%s`',
static::class
));
}

if ($isChildAdmin) {
return $this->urlize($matches[5]);
}

return sprintf(
'admin_%s%s_%s',
'' === $matches[1] ? '' : $this->urlize($matches[1]).'_',
$this->urlize($matches[3]),
$this->urlize($matches[5])
);
}

/**
* @phpstan-return T
*/
Expand Down

0 comments on commit d529b4f

Please sign in to comment.