Skip to content

Commit

Permalink
Enable legacy routing by container configuration and throw exception …
Browse files Browse the repository at this point in the history
…if hooks require it
  • Loading branch information
aschempp committed May 8, 2020
1 parent 8b27f64 commit 2c0115f
Show file tree
Hide file tree
Showing 31 changed files with 252 additions and 356 deletions.
12 changes: 9 additions & 3 deletions core-bundle/src/DependencyInjection/ContaoCoreExtension.php
Expand Up @@ -91,7 +91,7 @@ public function load(array $configs, ContainerBuilder $container): void
$this->setImagineService($config, $container);
$this->overwriteImageTargetDir($config, $container);
$this->handleTokenCheckerConfig($config, $container);
$this->handleLegacyRouting($config, $container);
$this->handleLegacyRouting($config, $container, $loader);

$container
->registerForAutoconfiguration(PickerProviderInterface::class)
Expand Down Expand Up @@ -284,7 +284,7 @@ private function handleTokenCheckerConfig(array $config, ContainerBuilder $conta
}
}

private function handleLegacyRouting(array $config, ContainerBuilder $container): void
private function handleLegacyRouting(array $config, ContainerBuilder $container, YamlFileLoader $loader): void
{
$count = 0;

Expand All @@ -298,8 +298,14 @@ private function handleLegacyRouting(array $config, ContainerBuilder $container)
$config['url_suffix'] = '.html';
}

$legacyRouting = 2 !== $count;

$container->setParameter('contao.prepend_locale', $config['prepend_locale']);
$container->setParameter('contao.url_suffix', $config['url_suffix']);
$container->setParameter('contao.legacy_routing', 2 !== $count);
$container->setParameter('contao.legacy_routing', $legacyRouting);

if ($legacyRouting) {
$loader->load('legacy_routing.yml');
}
}
}
Expand Up @@ -13,19 +13,13 @@
namespace Contao\CoreBundle\EventListener\DataContainer;

use Contao\CoreBundle\DataContainer\PaletteManipulator;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\ServiceAnnotation\Callback;
use Contao\DataContainer;
use Symfony\Contracts\Translation\TranslatorInterface;
use Terminal42\ServiceAnnotationBundle\ServiceAnnotationInterface;

class LegacyRoutingListener implements ServiceAnnotationInterface
{
/**
* @var ContaoFramework
*/
private $framework;

/**
* @var TranslatorInterface
*/
Expand All @@ -41,9 +35,8 @@ class LegacyRoutingListener implements ServiceAnnotationInterface
*/
private $urlSuffix;

public function __construct(ContaoFramework $framework, TranslatorInterface $translator, bool $prependLocale = false, string $urlSuffix = '.html')
public function __construct(TranslatorInterface $translator, bool $prependLocale = false, string $urlSuffix = '.html')
{
$this->framework = $framework;
$this->translator = $translator;
$this->prependLocale = $prependLocale;
$this->urlSuffix = $urlSuffix;
Expand All @@ -54,10 +47,6 @@ public function __construct(ContaoFramework $framework, TranslatorInterface $tra
*/
public function disableRoutingFields(): void
{
if (!$this->framework->isLegacyRouting()) {
return;
}

$translator = $this->translator;

$GLOBALS['TL_DCA']['tl_page']['fields']['languagePrefix']['eval']['disabled'] = true;
Expand All @@ -84,10 +73,6 @@ public function disableRoutingFields(): void
*/
public function overrideLanguagePrefix($value, DataContainer $dc)
{
if (!$this->framework->isLegacyRouting()) {
return $value;
}

return $this->prependLocale ? $dc->activeRecord->language : '';
}

Expand All @@ -96,10 +81,6 @@ public function overrideLanguagePrefix($value, DataContainer $dc)
*/
public function overrideUrlSuffix($value)
{
if (!$this->framework->isLegacyRouting()) {
return $value;
}

return $this->urlSuffix;
}
}
17 changes: 17 additions & 0 deletions core-bundle/src/Exception/LegacyRoutingException.php
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\Exception;

class LegacyRoutingException extends \LogicException
{
}
26 changes: 17 additions & 9 deletions core-bundle/src/Framework/ContaoFramework.php
Expand Up @@ -14,6 +14,7 @@

use Contao\ClassLoader;
use Contao\Config;
use Contao\CoreBundle\Exception\LegacyRoutingException;
use Contao\CoreBundle\Exception\RedirectResponseException;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
Expand Down Expand Up @@ -146,6 +147,10 @@ public function initialize(bool $isFrontend = false): void

$this->setConstants();
$this->initializeFramework();

if (!$this->legacyRouting) {
$this->throwOnLegacyRoutingHooks();
}
}

public function setHookListeners(array $hookListeners): void
Expand Down Expand Up @@ -173,15 +178,6 @@ public function getAdapter($class): Adapter
return $this->adapterCache[$class];
}

public function isLegacyRouting()
{
$this->initialize();

return $this->legacyRouting
|| !empty($GLOBALS['TL_HOOKS']['getPageIdFromUrl'])
|| !empty($GLOBALS['TL_HOOKS']['getRootPageFromUrl']);
}

/**
* @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0
*/
Expand Down Expand Up @@ -445,4 +441,16 @@ private function registerHookListeners(): void
$GLOBALS['TL_HOOKS'][$hookName] = array_merge(...$priorities);
}
}

private function throwOnLegacyRoutingHooks(): void
{
if (
empty($GLOBALS['TL_HOOKS']['getPageIdFromUrl'])
&& empty($GLOBALS['TL_HOOKS']['getRootPageFromUrl'])
) {
return;
}

throw new LegacyRoutingException('Legacy routing is required to support the getPageIdFromUrl and getRootPageFromUrl hooks. Check the Symfony inspector for more information.');
}
}
28 changes: 28 additions & 0 deletions core-bundle/src/Resources/config/legacy_routing.yml
@@ -0,0 +1,28 @@
services:
_defaults:
autoconfigure: true
autowire: false
public: false

contao.routing.frontend_loader:
class: Contao\CoreBundle\Routing\FrontendLoader
arguments:
- '%contao.prepend_locale%'
- '%contao.url_suffix%'
tags:
- routing.loader

contao.routing.legacy_matcher:
class: Contao\CoreBundle\Routing\Matcher\LegacyMatcher
decorates: contao.routing.nested_matcher
arguments:
- '@contao.framework'
- '@contao.routing.legacy_matcher.inner'
- '%contao.url_suffix%'
- '%contao.prepend_locale%'

Contao\CoreBundle\EventListener\DataContainer\LegacyRoutingListener:
arguments:
- '@translator'
- '%contao.prepend_locale%'
- '%contao.url_suffix%'
9 changes: 0 additions & 9 deletions core-bundle/src/Resources/config/listener.yml
Expand Up @@ -20,15 +20,6 @@ services:
tags:
- { name: terminal42_service_annotation }

Contao\CoreBundle\EventListener\DataContainer\LegacyRoutingListener:
arguments:
- '@contao.framework'
- '@translator'
- '%contao.prepend_locale%'
- '%contao.url_suffix%'
tags:
- { name: terminal42_service_annotation }

Contao\CoreBundle\EventListener\DataContainer\PageTypeOptionsListener:
arguments:
- !tagged_locator { tag: contao.page_provider, index_by: type, default_index_method: getPageType }
Expand Down
24 changes: 4 additions & 20 deletions core-bundle/src/Resources/config/services.yml
Expand Up @@ -460,9 +460,9 @@ services:
contao.routing.candidates:
class: Contao\CoreBundle\Routing\Candidates\Candidates
arguments:
- '@contao.framework'
- '@database_connection'
- !tagged_locator { tag: contao.page_provider, index_by: type, default_index_method: getPageType }
- '%contao.legacy_routing%'
- '%contao.url_suffix%'
- '%contao.prepend_locale%'

Expand All @@ -472,15 +472,6 @@ services:
contao.routing.final_matcher:
class: Contao\CoreBundle\Routing\Matcher\UrlMatcher

contao.routing.frontend_loader:
class: Contao\CoreBundle\Routing\FrontendLoader
arguments:
- '@contao.framework'
- '%contao.prepend_locale%'
- '%contao.url_suffix%'
tags:
- routing.loader

contao.routing.frontend_matcher:
class: Symfony\Component\HttpFoundation\RequestMatcher
calls:
Expand All @@ -505,19 +496,10 @@ services:
contao.routing.language_filter:
class: Contao\CoreBundle\Routing\Matcher\LanguageFilter

contao.routing.legacy_matcher:
class: Contao\CoreBundle\Routing\Matcher\LegacyMatcher
decorates: contao.routing.nested_matcher
arguments:
- '@contao.framework'
- '@contao.routing.legacy_matcher.inner'
- '%contao.url_suffix%'
- '%contao.prepend_locale%'

contao.routing.legacy_route_provider:
class: Contao\CoreBundle\Routing\LegacyRouteProvider
arguments:
- '@contao.routing.frontend_loader'
- '@?contao.routing.frontend_loader'
- '@contao.routing.route_provider'
tags:
- contao.content_resolver
Expand Down Expand Up @@ -585,6 +567,7 @@ services:
- '@database_connection'
- '@contao.routing.candidates'
- !tagged_locator { tag: contao.page_provider, index_by: type, default_index_method: getPageType }
- '%contao.legacy_routing%'
- '%contao.prepend_locale%'

contao.routing.route_404_provider:
Expand All @@ -604,6 +587,7 @@ services:
arguments:
- '@router'
- '@contao.framework'
- '%contao.legacy_routing%'
- '%contao.prepend_locale%'
- '%contao.url_suffix%'
public: true
Expand Down
41 changes: 18 additions & 23 deletions core-bundle/src/Resources/contao/classes/Frontend.php
Expand Up @@ -10,6 +10,7 @@

namespace Contao;

use Contao\CoreBundle\Exception\LegacyRoutingException;
use Contao\CoreBundle\Exception\NoRootPageFoundException;
use Contao\CoreBundle\Monolog\ContaoContext;
use Contao\CoreBundle\Search\Document;
Expand Down Expand Up @@ -65,23 +66,13 @@ public function __construct()
*/
public static function getPageIdFromUrl()
{
$objRequest = System::getContainer()->get('request_stack')->getCurrentRequest();

if ($objRequest instanceof Request && $objRequest->attributes->get('pageModel') instanceof PageModel)
{
/** @var PageModel $objPage */
$objPage = $objRequest->attributes->get('pageModel');

return $objPage->alias ?: $objPage->id;
}
@trigger_error('Using Frontend::getPageIdFromUrl() has been deprecated and will no longer work in Contao 5.0. Use the Symfony routing instead.', E_USER_DEPRECATED);

if (!System::getContainer()->get('contao.framework')->isLegacyRouting())
if (!System::getContainer()->getParameter('contao.legacy_routing'))
{
throw new \RuntimeException('Frontend::getPageIdFromUrl() requires legacy routing. Configure "prepend_locale" or "url_suffix" in the Contao bundle.');
throw new LegacyRoutingException('Frontend::getPageIdFromUrl() requires legacy routing. Configure "prepend_locale" or "url_suffix" in the Contao bundle.');
}

@trigger_error('Using Frontend::getPageIdFromUrl() has been deprecated and will no longer work in Contao 5.0. Use the Symfony routing instead.', E_USER_DEPRECATED);

$strRequest = Environment::get('relativeRequest');

if ($strRequest == '')
Expand Down Expand Up @@ -309,19 +300,23 @@ public static function getRootIdFromUrl()
*/
public static function getRootPageFromUrl()
{
$objRequest = System::getContainer()->get('request_stack')->getCurrentRequest();

if ($objRequest instanceof Request && $objRequest->attributes->get('pageModel') instanceof PageModel)
if (!System::getContainer()->getParameter('contao.legacy_routing'))
{
/** @var PageModel $objPage */
$objPage = $objRequest->attributes->get('pageModel');
$objRequest = System::getContainer()->get('request_stack')->getCurrentRequest();

return PageModel::findByPk($objPage->rootId);
}
if ($objRequest instanceof Request)
{
$objPage = $objRequest->attributes->get('pageModel');

if (!System::getContainer()->get('contao.framework')->isLegacyRouting())
{
throw new \RuntimeException('Frontend::getRootPageFromUrl() requires legacy routing. Configure "prepend_locale" or "url_suffix" in the Contao bundle.');
if ($objPage instanceof PageModel)
{
$objPage->loadDetails();

return PageModel::findByPk($objPage->rootId);
}
}

throw new NoRootPageFoundException('No root page found');
}

$host = Environment::get('host');
Expand Down
Expand Up @@ -128,7 +128,7 @@ public function renderPage($pageModel)
}

// Use the first result (see #4872)
if (!System::getContainer()->get('contao.framework')->isLegacyRouting() || !Config::get('addLanguageToUrl'))
if (!System::getContainer()->getParameter('contao.legacy_routing') || !Config::get('addLanguageToUrl'))
{
$objNewPage = current($arrLangs);
}
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/contao/languages/en/tl_page.xlf
Expand Up @@ -426,7 +426,7 @@
<source>Edit the articles of page ID %s</source>
</trans-unit>
<trans-unit id="tl_page.legacyRouting">
<source><![CDATA[Some URL settings are disabled because Contao is in <strong>legacy routing mode</strong>. Legacy routing is active if <code>prepend_locale</code> or <code>url_suffix</code> is set in the configuration files, or an extension uses the <code>getPageIdFromUrl</code> or <code>getRootPageFromUrl</code> hooks.]]></source>
<source><![CDATA[Some URL settings are disabled because Contao is in <em>legacy routing mode</em>. <a href="https://docs.contao.org/manual/en/system/legacy-routing/" target="_blank" rel="noreferrer noopener">Learn more</a>]]></source>
</trans-unit>
<trans-unit id="CACHE.0">
<source>0 (do not cache)</source>
Expand Down
Expand Up @@ -1169,7 +1169,7 @@ public static function generateFrontendUrl(array $arrRow, $strParams=null, $strF
$page->language = $strForceLang;
$page->rootLanguage = $strForceLang;

if (System::getContainer()->get('contao.framework')->isLegacyRouting())
if (System::getContainer()->getParameter('contao.legacy_routing'))
{
$page->languagePrefix = System::getContainer()->getParameter('contao.prepend_locale') ? $strForceLang : '';
}
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Resources/contao/models/PageModel.php
Expand Up @@ -1004,7 +1004,7 @@ public function loadDetails()
$this->rootUseSSL = $objParentPage->useSSL;
$this->rootFallbackLanguage = $objParentPage->language;

if (System::getContainer()->get('contao.framework')->isLegacyRouting())
if (System::getContainer()->getParameter('contao.legacy_routing'))
{
$this->languagePrefix = System::getContainer()->getParameter('contao.prepend_locale') ? $objParentPage->language : '';
$this->urlSuffix = System::getContainer()->getParameter('contao.url_suffix');
Expand Down Expand Up @@ -1098,7 +1098,7 @@ public function getFrontendUrl($strParams=null, $strForceLang=null)
$page->language = $strForceLang;
$page->rootLanguage = $strForceLang;

if (System::getContainer()->get('contao.framework')->isLegacyRouting())
if (System::getContainer()->getParameter('contao.legacy_routing'))
{
$page->languagePrefix = System::getContainer()->getParameter('contao.prepend_locale') ? $strForceLang : '';
}
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/contao/pages/PageError404.php
Expand Up @@ -85,7 +85,7 @@ protected function prepare()
$objRootPage = $this->getRootPageFromUrl();

// Forward if the language should be but is not set (see #4028). CMF Routing will take care of this in non-legacy mode.
if (System::getContainer()->get('contao.framework')->isLegacyRouting() && Config::get('addLanguageToUrl'))
if (System::getContainer()->getParameter('contao.legacy_routing') && Config::get('addLanguageToUrl'))
{
// Get the request string without the script name
$strRequest = Environment::get('relativeRequest');
Expand Down

0 comments on commit 2c0115f

Please sign in to comment.