Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Content resolvers convert content to a route object
- Loading branch information
Showing
8 changed files
with
282 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
services: | ||
_defaults: | ||
autoconfigure: true | ||
|
||
Contao\CoreBundle\Routing\Content\ArticleResolver: | ||
tags: | ||
- contao.content_resolver | ||
|
||
Contao\CoreBundle\Routing\Content\PageResolver: | ||
tags: | ||
- contao.content_resolver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Routing\Content; | ||
|
||
use Contao\ArticleModel; | ||
use Contao\PageModel; | ||
use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
use Symfony\Component\Routing\Route; | ||
|
||
class ArticleResolver implements ContentResolverInterface | ||
{ | ||
/** | ||
* @param ArticleModel $article | ||
*/ | ||
public function resolveContent($article): Route | ||
{ | ||
/** @var PageModel $page */ | ||
$page = $article->getRelated('pid'); | ||
|
||
if (!$page instanceof PageModel) { | ||
throw new RouteNotFoundException(sprintf('Page ID %s for article ID %s not found', $article->id, $article->pid)); | ||
} | ||
|
||
return new PageRoute($page, '/articles/'.($article->alias ?: $article->id), $article); | ||
} | ||
|
||
public function supportsContent($content): bool | ||
{ | ||
return $content instanceof ArticleModel; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
core-bundle/src/Routing/Content/ContentResolverInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Contao\CoreBundle\Routing\Content; | ||
|
||
use Symfony\Component\Routing\Route; | ||
|
||
interface ContentResolverInterface | ||
{ | ||
public const ATTRIBUTE = '_content_resolver'; | ||
|
||
/** | ||
* Returns a Symfony\Component\Routing\Route or another $content to resolve. | ||
* | ||
* @param mixed $content The route "content" which may also be an object or anything | ||
* | ||
* @return Route|mixed | ||
*/ | ||
public function resolveContent($content); | ||
|
||
/** | ||
* Whether this provider supports the supplied $content. | ||
* | ||
* This check does not need to look if the specific instance can be | ||
* resolved to a route, only whether the router can generate routes from | ||
* objects of this class. | ||
* | ||
* @param mixed $content The route "content" which may also be an object or anything | ||
*/ | ||
public function supportsContent($content): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Routing\Content; | ||
|
||
use Contao\PageModel; | ||
use Symfony\Component\Routing\Route; | ||
|
||
class PageResolver implements ContentResolverInterface | ||
{ | ||
/** | ||
* @param PageModel $page | ||
*/ | ||
public function resolveContent($page): Route | ||
{ | ||
return new PageRoute($page); | ||
} | ||
|
||
public function supportsContent($content): bool | ||
{ | ||
return $content instanceof PageModel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Routing; | ||
|
||
use Contao\CoreBundle\Routing\Content\ContentResolverInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Cmf\Component\Routing\RouteObjectInterface; | ||
use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface; | ||
use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
use Symfony\Component\Routing\Generator\UrlGenerator as SymfonyUrlGenerator; | ||
use Symfony\Component\Routing\RequestContext; | ||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
class DelegatingUrlGenerator extends SymfonyUrlGenerator implements VersatileGeneratorInterface | ||
{ | ||
/** | ||
* @var array<ContentResolverInterface> | ||
*/ | ||
private $resolvers; | ||
|
||
public function __construct($resolvers, LoggerInterface $logger = null) | ||
{ | ||
parent::__construct(new RouteCollection(), new RequestContext(), $logger); | ||
|
||
$this->resolvers = $resolvers; | ||
} | ||
|
||
/** | ||
* @param array $parameters | ||
* @param int $referenceType | ||
*/ | ||
public function generate($content, $parameters = [], $referenceType = self::ABSOLUTE_PATH): string | ||
{ | ||
$route = $this->resolveContent($content); | ||
|
||
// the Route has a cache of its own and is not recompiled as long as it does not get modified | ||
$compiledRoute = $route->compile(); | ||
$hostTokens = $compiledRoute->getHostTokens(); | ||
|
||
$debug_message = $this->getRouteDebugMessage($content); | ||
|
||
return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $debug_message, $referenceType, $hostTokens); | ||
} | ||
|
||
public function supports($content): bool | ||
{ | ||
if ($content instanceof Route) { | ||
return true; | ||
} | ||
|
||
foreach ($this->resolvers as $provider) { | ||
if ($provider->supportsContent($content)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function getRouteDebugMessage($name, array $parameters = []): string | ||
{ | ||
if (is_scalar($name)) { | ||
return $name; | ||
} | ||
|
||
if (\is_array($name)) { | ||
return serialize($name); | ||
} | ||
|
||
if ($name instanceof RouteObjectInterface) { | ||
return 'key '.$name->getRouteKey(); | ||
} | ||
|
||
if ($name instanceof Route) { | ||
return 'path '.$name->getPath(); | ||
} | ||
|
||
if (\is_object($name)) { | ||
return \get_class($name); | ||
} | ||
|
||
return 'null route'; | ||
} | ||
|
||
private function resolveContent($content): Route | ||
{ | ||
if ($content instanceof Route) { | ||
return $content; | ||
} | ||
|
||
$route = null; | ||
|
||
foreach ($this->resolvers as $resolver) { | ||
if ($resolver->supportsContent($content)) { | ||
$route = $resolver->resolveContent($content); | ||
|
||
if ($route instanceof Route) { | ||
$route->setDefault(ContentResolverInterface::ATTRIBUTE, $resolver); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
if ($route instanceof Route) { | ||
return $route; | ||
} | ||
|
||
if (null !== $route && $route !== $content) { | ||
return $this->resolveContent($route); | ||
} | ||
|
||
throw new RouteNotFoundException('No route found for '.$this->getRouteDebugMessage($content)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters