Skip to content

Commit

Permalink
Re-add the reference type
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jan 11, 2024
1 parent 71c1abd commit 53f4cda
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 49 deletions.
34 changes: 32 additions & 2 deletions core-bundle/contao/models/PageModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Contao\Model\Registry;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* Reads and writes pages
Expand Down Expand Up @@ -1087,7 +1088,36 @@ public function getFrontendUrl($strParams=null)
{
trigger_deprecation('contao/core-bundle', '5.3', __METHOD__ . ' is deprecated, use the content URL generator instead.');

return UrlUtil::makeAbsolutePath($this->getAbsoluteUrl($strParams), Environment::get('base'));
$this->loadDetails();

$objRouter = System::getContainer()->get('contao.routing.content_url_generator');

if (\is_array($strParams))
{
$parameters = $strParams;
}
else
{
$parameters = array('parameters' => $strParams);
}

try
{
$strUrl = $objRouter->generate($this, $parameters);
}
catch (RouteNotFoundException $e)
{
$pageRegistry = System::getContainer()->get('contao.routing.page_registry');

if (!$pageRegistry->isRoutable($this))
{
throw new ResourceNotFoundException(sprintf('Page ID %s is not routable', $this->id), 0, $e);
}

throw $e;
}

return $strUrl;
}

/**
Expand Down Expand Up @@ -1119,7 +1149,7 @@ public function getAbsoluteUrl($strParams=null)

try
{
$strUrl = $objRouter->generate($this, $parameters);
$strUrl = $objRouter->generate($this, $parameters, UrlGeneratorInterface::ABSOLUTE_URL);
}
catch (RouteNotFoundException $e)
{
Expand Down
3 changes: 2 additions & 1 deletion core-bundle/contao/modules/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Contao\CoreBundle\Util\UrlUtil;
use Contao\Model\Collection;
use Symfony\Component\Routing\Exception\ExceptionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* Parent class for front end modules.
Expand Down Expand Up @@ -361,7 +362,7 @@ protected function renderNavigation($pid, $level=1, $host=null, $language=null)
}
else
{
$href = StringUtil::ampersand(UrlUtil::makeAbsolutePath($href, Environment::get('base')));
$href = StringUtil::ampersand($href);
}

$items[] = $this->compileNavigationRow($objPage, $objSubpage, $subitems, $href);
Expand Down
7 changes: 2 additions & 5 deletions core-bundle/src/InsertTag/Resolver/LegacyInsertTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Routing\Exception\ExceptionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

#[AsInsertTag('lang')]
#[AsInsertTag('br')]
Expand Down Expand Up @@ -304,11 +305,7 @@ public function __invoke(ResolvedInsertTag $insertTag): InsertTagResult

try {
$blnAbsolute = \in_array('absolute', \array_slice($insertTag->getParameters()->all(), 1), true);
$strUrl = $this->container->get('contao.routing.content_url_generator')->generate($objArticle);

if (!$blnAbsolute) {
$strUrl = UrlUtil::makeAbsolutePath($strUrl, Environment::get('base'));
}
$strUrl = $this->container->get('contao.routing.content_url_generator')->generate($objArticle, [], $blnAbsolute ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH);
} catch (ExceptionInterface) {
// Ignore routing exception
}
Expand Down
5 changes: 2 additions & 3 deletions core-bundle/src/InsertTag/Resolver/LinkInsertTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Contao\PageModel;
use Contao\StringUtil;
use Symfony\Component\Routing\Exception\ExceptionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class LinkInsertTag
{
Expand Down Expand Up @@ -87,12 +88,10 @@ public function replaceInsertTag(ResolvedInsertTag $insertTag): InsertTagResult
if (\in_array($insertTag->getName(), ['link', 'link_open', 'link_url'], true)) {
try {
$blnAbsolute = \in_array('absolute', \array_slice($insertTag->getParameters()->all(), 1), true);
$strUrl = $this->urlGenerator->generate($objNextPage);
$strUrl = $this->urlGenerator->generate($objNextPage, [], $blnAbsolute ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH);

if (0 === strncasecmp($strUrl, 'mailto:', 7)) {
$strUrl = StringUtil::encodeEmail($strUrl);
} elseif (!$blnAbsolute) {
$strUrl = UrlUtil::makeAbsolutePath($strUrl, Environment::get('base'));
}
} catch (ExceptionInterface) {
// Use empty URL defined above
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Routing/ContentUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(
/**
* @throws ExceptionInterface
*/
public function generate(object $content, array $parameters = []): string
public function generate(object $content, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
$cacheKey = sha1(serialize($content)."\0".serialize($parameters));

Expand Down Expand Up @@ -103,7 +103,7 @@ public function generate(object $content, array $parameters = []): string
return $this->urlCache[$cacheKey] = $this->urlGenerator->generate(
PageRoute::PAGE_BASED_ROUTE_NAME,
[...$optionalParameters, ...$parameters, RouteObjectInterface::ROUTE_OBJECT => $route],
UrlGeneratorInterface::ABSOLUTE_URL,
$referenceType,
);
} catch (ExceptionInterface $exception) {
$this->urlCache[$cacheKey] = $exception;
Expand Down
21 changes: 0 additions & 21 deletions core-bundle/src/Util/UrlUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,4 @@ public static function makeAbsolute(string $relativeUrl, string $baseUrl): strin

return (string) $base->withPath($path)->withQuery($query)->withFragment($relative->getFragment());
}

/**
* Makes an absolute URL relative to the given path. This will never make a protocol-relative URL, but
* will remove the host if it matches the base URL.
*/
public static function makeAbsolutePath(string $absoluteUrl, string $baseUrl): string
{
$absolute = new Uri($absoluteUrl);

if ('' === $absolute->getAuthority()) {
return (string) $absolute->withPath($absolute->getPath() ?: '/');
}

$base = new Uri($baseUrl);

if ($base->getScheme() === $absolute->getScheme() && $base->getHost() === $absolute->getHost()) {
$absolute = $absolute->withScheme('')->withHost('');
}

return (string) $absolute->withPath($absolute->getPath() ?: '/');
}
}
15 changes: 0 additions & 15 deletions core-bundle/tests/Util/UrlUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,4 @@ public function testMakeAbsoluteFailsForRelativeBasePath(): void

UrlUtil::makeAbsolute('foo', 'path/foo');
}

/**
* @dataProvider getMakeAbsolutePath
*/
public function testMakeRelative(string $url, string $base, string $expected): void
{
$this->assertSame($expected, UrlUtil::makeAbsolutePath($url, $base));
}

public function getMakeAbsolutePath(): \Generator
{
yield ['/page', 'https://foobar.com', '/page'];
yield ['https://foobar.com/page', 'https://foobar.com', '/page'];
yield ['https://foobar.com/page', 'https://example.com', 'https://foobar.com/page'];
}
}

0 comments on commit 53f4cda

Please sign in to comment.