Skip to content

Commit

Permalink
Get URL prefix and suffix from the page registry
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 20, 2020
1 parent a4aba62 commit 31d34ec
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 37 deletions.
Expand Up @@ -88,6 +88,7 @@ protected function getRouteConfig(Reference $reference, Definition $definition,
RouteConfig::class,
[
$attributes['parameters'] ?? null,
$attributes['urlSuffix'] ?? null,
$attributes['requirements'] ?? [],
$attributes['options'] ?? [],
$defaults,
Expand Down
14 changes: 2 additions & 12 deletions core-bundle/src/Routing/Candidates/AbstractCandidates.php
Expand Up @@ -31,12 +31,12 @@ class AbstractCandidates implements CandidatesInterface
/**
* @var array
*/
private $urlPrefixes;
protected $urlPrefixes;

/**
* @var array
*/
private $urlSuffixes;
protected $urlSuffixes;

public function __construct(array $urlPrefixes, array $urlSuffixes)
{
Expand Down Expand Up @@ -126,16 +126,6 @@ public function getCandidates(Request $request): array
return array_values(array_unique($candidates));
}

protected function setUrlPrefixes(array $urlPrefixes): void
{
$this->urlPrefixes = $urlPrefixes;
}

protected function setUrlSuffixes(array $urlSuffixes): void
{
$this->urlSuffixes = $urlSuffixes;
}

private function addCandidatesFor(string $url, array &$candidates): void
{
if ('' === $url) {
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Routing/Candidates/LegacyCandidates.php
Expand Up @@ -42,7 +42,7 @@ public function getCandidates(Request $request): array
return [];
}

$this->setUrlPrefixes([$prefix]);
$this->urlPrefixes = [$prefix];

return parent::getCandidates($request);
}
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Routing/Candidates/LocaleCandidates.php
Expand Up @@ -52,6 +52,6 @@ private function initialize(): void

$this->initialized = true;

$this->setUrlSuffixes($this->pageRegistry->getUrlSuffixes());
$this->urlSuffixes = $this->pageRegistry->getUrlSuffixes();
}
}
9 changes: 2 additions & 7 deletions core-bundle/src/Routing/Candidates/PageCandidates.php
Expand Up @@ -75,12 +75,7 @@ private function initialize(): void

$this->initialized = true;

$urlPrefixes = $this->connection
->query("SELECT DISTINCT urlPrefix FROM tl_page WHERE type='root'")
->fetchAll(FetchMode::COLUMN)
;

$this->setUrlPrefixes($urlPrefixes);
$this->setUrlSuffixes($this->pageRegistry->getUrlSuffixes());
$this->urlPrefixes = $this->pageRegistry->getUrlPrefixes();
$this->urlSuffixes = $this->pageRegistry->getUrlSuffixes();
}
}
91 changes: 76 additions & 15 deletions core-bundle/src/Routing/Page/PageRegistry.php
Expand Up @@ -13,10 +13,16 @@
namespace Contao\CoreBundle\Routing\Page;

use Contao\PageModel;
use Doctrine\DBAL\Connection;
use Symfony\Component\Routing\Route;

class PageRegistry
{
/**
* @var Connection
*/
private $connection;

/**
* @var array
*/
Expand All @@ -32,6 +38,21 @@ class PageRegistry
*/
private $contentComposition = [];

/**
* @var array<string>
*/
private $urlPrefixes;

/**
* @var array<string>
*/
private $urlSuffixes;

public function __construct(Connection $connection)
{
$this->connection = $connection;
}

public function getRouteConfig(string $type): RouteConfig
{
return $this->routeConfigs[$type] ?? new RouteConfig();
Expand All @@ -51,21 +72,6 @@ public function enhancePageRoute(PageRoute $route): Route
return $enhancer->enhancePageRoute($route);
}

public function getUrlSuffixes(): array
{
$urlSuffixes = [];

foreach ($this->routeEnhancers as $enhancer) {
$urlSuffixes[] = $enhancer->getUrlSuffixes();
}

if (0 === \count($urlSuffixes)) {
return [];
}

return array_unique(array_merge(...$urlSuffixes));
}

public function supportsContentComposition(PageModel $pageModel): bool
{
if (!isset($this->contentComposition[$pageModel->type])) {
Expand All @@ -81,6 +87,26 @@ public function supportsContentComposition(PageModel $pageModel): bool
return (bool) $service;
}

/**
* @return array<string>
*/
public function getUrlPrefixes(): array
{
$this->initializePrefixAndSuffix();

return $this->urlPrefixes;
}

/**
* @return array<string>
*/
public function getUrlSuffixes(): array
{
$this->initializePrefixAndSuffix();

return $this->urlSuffixes;
}

/**
* @param ContentCompositionInterface|bool $contentComposition
*/
Expand Down Expand Up @@ -115,4 +141,39 @@ public function keys(): array
{
return array_keys($this->routeConfigs);
}

private function initializePrefixAndSuffix(): void
{
if (null !== $this->urlPrefixes || null !== $this->urlSuffixes) {
return;
}

$results = $this->connection
->query("SELECT urlPrefix, urlSuffix FROM tl_page WHERE type='root'")
->fetchAll()
;

$urlSuffixes = [
array_column($results, 'urlSuffix'),
array_filter(array_map(
function (RouteConfig $config) {
return $config->getUrlSuffix();
},
$this->routeConfigs
))
];

foreach ($this->routeConfigs as $config) {
if (null !== ($suffix = $config->getUrlSuffix())) {
$urlSuffixes[] = [$suffix];
}
}

foreach ($this->routeEnhancers as $enhancer) {
$urlSuffixes[] = $enhancer->getUrlSuffixes();
}

$this->urlSuffixes = array_unique(array_merge(...$urlSuffixes));
$this->urlPrefixes = array_unique(array_column($results, 'urlPrefix'));
}
}
13 changes: 12 additions & 1 deletion core-bundle/src/Routing/Page/RouteConfig.php
Expand Up @@ -19,6 +19,11 @@ final class RouteConfig
*/
private $pathParameters;

/**
* @var string|null
*/
private $urlSuffix;

/**
* @var array
*/
Expand All @@ -39,9 +44,10 @@ final class RouteConfig
*/
private $methods;

public function __construct(string $pathParameters = null, array $requirements = [], array $options = [], array $defaults = [], array $methods = [])
public function __construct(string $pathParameters = null, string $urlSuffix = null, array $requirements = [], array $options = [], array $defaults = [], array $methods = [])
{
$this->pathParameters = $pathParameters;
$this->urlSuffix = $urlSuffix;
$this->requirements = $requirements;
$this->options = $options;
$this->defaults = $defaults;
Expand All @@ -53,6 +59,11 @@ public function getPathParameters(): ?string
return $this->pathParameters;
}

public function getUrlSuffix(): ?string
{
return $this->urlSuffix;
}

public function getRequirements(): array
{
return $this->requirements;
Expand Down
4 changes: 4 additions & 0 deletions core-bundle/src/Routing/RouteFactory.php
Expand Up @@ -67,6 +67,10 @@ public function createRouteForPage(PageModel $pageModel, string $defaultParamete
$route = new PageRoute($pageModel, $pathParameters, $defaults, $requirements, $config->getOptions(), $config->getMethods());
$route->setContent($content);

if (null !== $config->getUrlSuffix()) {
$route->setUrlSuffix($config->getUrlSuffix());
}

return $this->pageRegistry->enhancePageRoute($route);
}

Expand Down
1 change: 1 addition & 0 deletions core-bundle/src/ServiceAnnotation/Page.php
Expand Up @@ -25,6 +25,7 @@
* @Attributes({
* @Attribute("value", type = "string"),
* @Attribute("parameters", type = "string"),
* @Attribute("urlSuffix", type = "string"),
* @Attribute("requirements", type = "array"),
* @Attribute("options", type = "array"),
* @Attribute("defaults", type = "array"),
Expand Down

0 comments on commit 31d34ec

Please sign in to comment.