Revert "feature #30501 [FrameworkBundle][Routing] added Configurators to handle template and redirect controllers (HeahDude)"

This reverts commit 477ee19778, reversing
changes made to 9bfa25869a.
This commit is contained in:
Nicolas Grekas 2020-04-24 11:45:27 +02:00
parent 69452b22c2
commit e4e8945aef
16 changed files with 13 additions and 587 deletions

View File

@ -6,7 +6,6 @@ CHANGELOG
* Added link to source for controllers registered as named services
* Added link to source on controller on `router:match`/`debug:router` (when `framework.ide` is configured)
* Added `Routing\Loader` and `Routing\Loader\Configurator` namespaces to ease defining routes with default controllers
* Added the `framework.router.context` configuration node to configure the `RequestContext`
* Made `MicroKernelTrait::configureContainer()` compatible with `ContainerConfigurator`
* Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails.

View File

@ -11,14 +11,14 @@
namespace Symfony\Bundle\FrameworkBundle\Kernel;
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Bundle\FrameworkBundle\Routing\Loader\PhpFileLoader as RoutingPhpFileLoader;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\AbstractConfigurator;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader as ContainerPhpFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Routing\Loader\PhpFileLoader as RoutingPhpFileLoader;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouteCollectionBuilder;

View File

@ -25,7 +25,7 @@
<argument type="service" id="file_locator" />
</service>
<service id="routing.loader.php" class="Symfony\Bundle\FrameworkBundle\Routing\Loader\PhpFileLoader">
<service id="routing.loader.php" class="Symfony\Component\Routing\Loader\PhpFileLoader">
<tag name="routing.loader" />
<argument type="service" id="file_locator" />
</service>

View File

@ -1,33 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits\AddTrait;
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class GoneRouteConfigurator extends RouteConfigurator
{
use AddTrait;
/**
* @param bool $permanent Whether the route is gone permanently
*
* @return $this
*/
final public function permanent(bool $permanent = true)
{
return $this->defaults(['permanent' => $permanent]);
}
}

View File

@ -1,63 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits\AddTrait;
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
/**
* @author Jules Pietri <jules@heahprod.com>
*/
class RedirectRouteConfigurator extends RouteConfigurator
{
use AddTrait;
/**
* @param bool $permanent Whether the redirection is permanent
*
* @return $this
*/
final public function permanent(bool $permanent = true)
{
return $this->defaults(['permanent' => $permanent]);
}
/**
* @param bool|array $ignoreAttributes Whether to ignore attributes or an array of attributes to ignore
*
* @return $this
*/
final public function ignoreAttributes($ignoreAttributes = true)
{
return $this->defaults(['ignoreAttributes' => $ignoreAttributes]);
}
/**
* @param bool $keepRequestMethod Whether redirect action should keep HTTP request method
*
* @return $this
*/
final public function keepRequestMethod(bool $keepRequestMethod = true)
{
return $this->defaults(['keepRequestMethod' => $keepRequestMethod]);
}
/**
* @param bool $keepQueryParams Whether redirect action should keep query parameters
*
* @return $this
*/
final public function keepQueryParams(bool $keepQueryParams = true)
{
return $this->defaults(['keepQueryParams' => $keepQueryParams]);
}
}

View File

@ -1,73 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator as BaseRouteConfigurator;
/**
* @author Jules Pietri <jules@heahprod.com>
*/
class RouteConfigurator extends BaseRouteConfigurator
{
/**
* @param string $template The template name
* @param array $context The template variables
*/
final public function template(string $template, array $context = []): TemplateRouteConfigurator
{
return (new TemplateRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
->defaults([
'_controller' => TemplateController::class,
'template' => $template,
'context' => $context,
])
;
}
/**
* @param string $route The route name to redirect to
*/
final public function redirectToRoute(string $route): RedirectRouteConfigurator
{
return (new RedirectRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
->defaults([
'_controller' => RedirectController::class.'::redirectAction',
'route' => $route,
])
;
}
/**
* @param string $url The relative path or URL to redirect to
*/
final public function redirectToUrl(string $url): UrlRedirectRouteConfigurator
{
return (new UrlRedirectRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
->defaults([
'_controller' => RedirectController::class.'::urlRedirectAction',
'path' => $url,
])
;
}
final public function gone(): GoneRouteConfigurator
{
return (new GoneRouteConfigurator($this->collection, $this->route, $this->name, $this->parentConfigurator, $this->prefixes))
->defaults([
'_controller' => RedirectController::class.'::redirectAction',
'route' => '',
])
;
}
}

View File

@ -1,20 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits\AddTrait;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator as BaseRoutingConfigurator;
class RoutingConfigurator extends BaseRoutingConfigurator
{
use AddTrait;
}

View File

@ -1,53 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits\AddTrait;
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
/**
* @author Jules Pietri <jules@heahprod.com>
*/
class TemplateRouteConfigurator extends RouteConfigurator
{
use AddTrait;
/**
* @param int|null $maxAge Max age for client caching
*
* @return $this
*/
final public function maxAge(?int $maxAge)
{
return $this->defaults(['maxAge' => $maxAge]);
}
/**
* @param int|null $sharedMaxAge Max age for shared (proxy) caching
*
* @return $this
*/
final public function sharedMaxAge(?int $sharedMaxAge)
{
return $this->defaults(['sharedAge' => $sharedMaxAge]);
}
/**
* @param bool|null $private Whether or not caching should apply for client caches only
*
* @return $this
*/
final public function private(?bool $private = true)
{
return $this->defaults(['private' => $private]);
}
}

View File

@ -1,46 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits;
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\RouteConfigurator;
use Symfony\Component\Routing\Loader\Configurator\CollectionConfigurator;
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator as BaseRouteConfigurator;
trait AddTrait
{
/**
* Adds a route.
*
* @param string|array $path the path, or the localized paths of the route
*
* @return RouteConfigurator
*/
public function add(string $name, $path): BaseRouteConfigurator
{
$parentConfigurator = $this instanceof CollectionConfigurator ? $this : ($this instanceof RouteConfigurator ? $this->parentConfigurator : null);
$route = $this->createLocalizedRoute($this->collection, $name, $path, $this->name, $this->prefixes);
return new RouteConfigurator($this->collection, $route, $this->name, $parentConfigurator, $this->prefixes);
}
/**
* Adds a route.
*
* @param string|array $path the path, or the localized paths of the route
*
* @return RouteConfigurator
*/
final public function __invoke(string $name, $path): BaseRouteConfigurator
{
return $this->add($name, $path);
}
}

View File

@ -1,62 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\Traits\AddTrait;
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
/**
* @author Jules Pietri <jules@heahprod.com>
*/
class UrlRedirectRouteConfigurator extends RouteConfigurator
{
use AddTrait;
/**
* @param bool $permanent Whether the redirection is permanent
*
* @return $this
*/
final public function permanent(bool $permanent = true)
{
return $this->defaults(['permanent' => $permanent]);
}
/**
* @param string|null $scheme The URL scheme (null to keep the current one)
* @param int|null $port The HTTP or HTTPS port (null to keep the current one for the same scheme or the default configured port)
*
* @return $this
*/
final public function scheme(?string $scheme, int $port = null)
{
$this->defaults(['scheme' => $scheme]);
if ('http' === $scheme) {
$this->defaults(['httpPort' => $port]);
} elseif ('https' === $scheme) {
$this->defaults(['httpsPort' => $port]);
}
return $this;
}
/**
* @param bool $keepRequestMethod Whether redirect action should keep HTTP request method
*
* @return $this
*/
final public function keepRequestMethod(bool $keepRequestMethod = true)
{
return $this->defaults(['keepRequestMethod' => $keepRequestMethod]);
}
}

View File

@ -1,31 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader;
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Routing\Loader\PhpFileLoader as BasePhpFileLoader;
use Symfony\Component\Routing\RouteCollection;
/**
* @author Jules Pietri <jules@heahprod.com>
*/
class PhpFileLoader extends BasePhpFileLoader
{
protected function callConfigurator(callable $result, string $path, string $file): RouteCollection
{
$collection = new RouteCollection();
$result(new RoutingConfigurator($collection, $this, $path, $file));
return $collection;
}
}

View File

@ -1,45 +0,0 @@
<?php
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator;
return function (RoutingConfigurator $routes) {
$routes->add('classic_route', '/classic');
$routes->add('template_route', '/static')
->template('static.html.twig', ['foo' => 'bar'])
->maxAge(300)
->sharedMaxAge(100)
->private()
->methods(['GET'])
->utf8()
->condition('abc')
;
$routes->add('redirect_route', '/redirect')
->redirectToRoute('target_route')
->permanent()
->ignoreAttributes(['attr', 'ibutes'])
->keepRequestMethod()
->keepQueryParams()
->schemes(['http'])
->host('legacy')
->utf8()
;
$routes->add('url_redirect_route', '/redirect-url')
->redirectToUrl('/url-target')
->permanent()
->scheme('http', 1)
->keepRequestMethod()
->host('legacy')
->utf8()
;
$routes->add('not_a_route', '/not-a-path')
->gone()
->host('legacy')
->utf8()
;
$routes->add('gone_route', '/gone-path')
->gone()
->permanent()
->utf8()
;
};

View File

@ -1,116 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Routing\Loader;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
abstract class AbstractLoaderTest extends TestCase
{
/** @var LoaderInterface */
protected $loader;
abstract protected function getLoader(): LoaderInterface;
abstract protected function getType(): string;
protected function setUp(): void
{
$this->loader = $this->getLoader();
}
protected function tearDown(): void
{
$this->loader = null;
}
public function getLocator(): FileLocatorInterface
{
return new FileLocator([__DIR__.'/../../Fixtures/Resources/config/routing']);
}
public function testRoutesAreLoaded()
{
$routeCollection = $this->loader->load('routes.'.$this->getType());
$expectedCollection = new RouteCollection();
$expectedCollection->add('classic_route', (new Route('/classic')));
$expectedCollection->add('template_route', (new Route('/static'))
->setDefaults([
'_controller' => TemplateController::class,
'context' => ['foo' => 'bar'],
'template' => 'static.html.twig',
'maxAge' => 300,
'sharedAge' => 100,
'private' => true,
])
->setMethods(['GET'])
->setOptions(['utf8' => true])
->setCondition('abc')
);
$expectedCollection->add('redirect_route', (new Route('/redirect'))
->setDefaults([
'_controller' => RedirectController::class.'::redirectAction',
'route' => 'target_route',
'permanent' => true,
'ignoreAttributes' => ['attr', 'ibutes'],
'keepRequestMethod' => true,
'keepQueryParams' => true,
])
->setSchemes(['http'])
->setHost('legacy')
->setOptions(['utf8' => true])
);
$expectedCollection->add('url_redirect_route', (new Route('/redirect-url'))
->setDefaults([
'_controller' => RedirectController::class.'::urlRedirectAction',
'path' => '/url-target',
'permanent' => true,
'scheme' => 'http',
'httpPort' => 1,
'keepRequestMethod' => true,
])
->setHost('legacy')
->setOptions(['utf8' => true])
);
$expectedCollection->add('not_a_route', (new Route('/not-a-path'))
->setDefaults([
'_controller' => RedirectController::class.'::redirectAction',
'route' => '',
])
->setHost('legacy')
->setOptions(['utf8' => true])
);
$expectedCollection->add('gone_route', (new Route('/gone-path'))
->setDefaults([
'_controller' => RedirectController::class.'::redirectAction',
'route' => '',
'permanent' => true,
])
->setOptions(['utf8' => true])
);
$expectedCollection->addResource(new FileResource(realpath(
__DIR__.'/../../Fixtures/Resources/config/routing/routes.'.$this->getType()
)));
$this->assertEquals($expectedCollection, $routeCollection);
}
}

View File

@ -1,28 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Routing\Loader;
use Symfony\Bundle\FrameworkBundle\Routing\Loader\PhpFileLoader;
use Symfony\Component\Config\Loader\LoaderInterface;
class PhpFileLoaderTest extends AbstractLoaderTest
{
protected function getLoader(): LoaderInterface
{
return new PhpFileLoader($this->getLocator());
}
protected function getType(): string
{
return 'php';
}
}

View File

@ -83,7 +83,6 @@
"symfony/messenger": "<4.4",
"symfony/mime": "<4.4",
"symfony/property-info": "<4.4",
"symfony/routing": "<5.1",
"symfony/serializer": "<4.4",
"symfony/stopwatch": "<4.4",
"symfony/translation": "<5.0",

View File

@ -104,33 +104,31 @@ class XmlFileLoader extends FileLoader
/**
* Parses a route and adds it to the RouteCollection.
*
* @param \DOMElement $node Element to parse that represents a Route
* @param string $filepath Full path of the XML file being processed
* @param \DOMElement $node Element to parse that represents a Route
* @param string $path Full path of the XML file being processed
*
* @throws \InvalidArgumentException When the XML is invalid
*/
protected function parseRoute(RouteCollection $collection, \DOMElement $node, string $filepath)
protected function parseRoute(RouteCollection $collection, \DOMElement $node, string $path)
{
if ('' === $id = $node->getAttribute('id')) {
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have an "id" attribute.', $filepath));
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have an "id" attribute.', $path));
}
$schemes = preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY);
$methods = preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY);
list($defaults, $requirements, $options, $condition, $paths, /* $prefixes */, $hosts) = $this->parseConfigs($node, $filepath);
list($defaults, $requirements, $options, $condition, $paths, /* $prefixes */, $hosts) = $this->parseConfigs($node, $path);
$path = $node->getAttribute('path');
if (!$paths && '' === $path) {
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have a "path" attribute or <path> child nodes.', $filepath));
if (!$paths && '' === $node->getAttribute('path')) {
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have a "path" attribute or <path> child nodes.', $path));
}
if ($paths && '' !== $path) {
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must not have both a "path" attribute and <path> child nodes.', $filepath));
if ($paths && '' !== $node->getAttribute('path')) {
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must not have both a "path" attribute and <path> child nodes.', $path));
}
$routes = $this->createLocalizedRoute($collection, $id, $paths ?: $path);
$routes = $this->createLocalizedRoute($collection, $id, $paths ?: $node->getAttribute('path'));
$routes->addDefaults($defaults);
$routes->addRequirements($requirements);
$routes->addOptions($options);