From e641cbdd462a077e2ede42a77060e152c89d2f19 Mon Sep 17 00:00:00 2001 From: Valentin Udaltsov Date: Sun, 10 Nov 2019 19:23:41 +0300 Subject: [PATCH] [Routing] Deprecate RouteCollectionBuilder --- UPGRADE-5.1.md | 13 ++++ UPGRADE-6.0.md | 13 ++++ .../Bundle/FrameworkBundle/CHANGELOG.md | 6 ++ .../Kernel/MicroKernelTrait.php | 33 ++++++++- .../Tests/Kernel/ConcreteMicroKernel.php | 8 +- .../Tests/Kernel/MicroKernelTraitTest.php | 12 +++ .../Kernel/MicroKernelWithConfigureRoutes.php | 74 +++++++++++++++++++ .../Bundle/FrameworkBundle/composer.json | 2 +- src/Symfony/Component/Routing/CHANGELOG.md | 6 ++ .../Configurator/RoutingConfigurator.php | 40 ++++++++-- .../Routing/RouteCollectionBuilder.php | 2 + .../Tests/RouteCollectionBuilderTest.php | 3 + 12 files changed, 200 insertions(+), 12 deletions(-) create mode 100644 UPGRADE-5.1.md create mode 100644 UPGRADE-6.0.md create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelWithConfigureRoutes.php diff --git a/UPGRADE-5.1.md b/UPGRADE-5.1.md new file mode 100644 index 000000000000..619aefa79cd5 --- /dev/null +++ b/UPGRADE-5.1.md @@ -0,0 +1,13 @@ +UPGRADE FROM 5.0 to 5.1 +======================= + +FrameworkBundle +--------------- + + * Marked `MicroKernelTrait::configureRoutes()` as `@internal` and `@final`. + * Deprecated not overriding `MicroKernelTrait::configureRouting()`. + +Routing +------- + + * Deprecated `RouteCollectionBuilder` in favor of `RoutingConfigurator`. diff --git a/UPGRADE-6.0.md b/UPGRADE-6.0.md new file mode 100644 index 000000000000..3a3cd85984d0 --- /dev/null +++ b/UPGRADE-6.0.md @@ -0,0 +1,13 @@ +UPGRADE FROM 5.x to 6.0 +======================= + +FrameworkBundle +--------------- + + * Removed `MicroKernelTrait::configureRoutes()`. + * Made `MicroKernelTrait::configureRouting()` abstract. + +Routing +------- + + * Removed `RouteCollectionBuilder`. diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 73b4b875f289..9d140b3ce8d1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +5.1.0 +----- + + * Marked `MicroKernelTrait::configureRoutes()` as `@internal` and `@final`. + * Deprecated not overriding `MicroKernelTrait::configureRouting()`. + 5.0.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php b/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php index 181ea8276a6d..df9e801bbc70 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php +++ b/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php @@ -14,6 +14,7 @@ use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; use Symfony\Component\Routing\RouteCollectionBuilder; /** @@ -29,8 +30,28 @@ trait MicroKernelTrait * * $routes->import('config/routing.yml'); * $routes->add('/admin', 'App\Controller\AdminController::dashboard', 'admin_dashboard'); + * + * @final since Symfony 5.1, override configureRouting() instead + * + * @internal since Symfony 5.1, use configureRouting() instead + */ + protected function configureRoutes(RouteCollectionBuilder $routes) + { + } + + /** + * Adds or imports routes into your application. + * + * $routes->import($this->getProjectDir().'/config/*.{yaml,php}'); + * $routes + * ->add('admin_dashboard', '/admin') + * ->controller('App\Controller\AdminController::dashboard') + * ; */ - abstract protected function configureRoutes(RouteCollectionBuilder $routes); + protected function configureRouting(RoutingConfigurator $routes): void + { + @trigger_error(sprintf('Not overriding the "%s()" method is deprecated since Symfony 5.1 and will trigger a fatal error in 6.0.', __METHOD__), E_USER_DEPRECATED); + } /** * Configures the container. @@ -91,7 +112,15 @@ public function loadRoutes(LoaderInterface $loader) { $routes = new RouteCollectionBuilder($loader); $this->configureRoutes($routes); + $collection = $routes->build(); + + if (0 !== \count($collection)) { + @trigger_error(sprintf('Adding routes via the "%s:configureRoutes()" method is deprecated since Symfony 5.1 and will have no effect in 6.0; use "configureRouting()" instead.', self::class), E_USER_DEPRECATED); + } + + $file = (new \ReflectionObject($this))->getFileName(); + $this->configureRouting(new RoutingConfigurator($collection, $loader, null, $file)); - return $routes->build(); + return $collection; } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/ConcreteMicroKernel.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/ConcreteMicroKernel.php index 5792f2995803..099292bef826 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/ConcreteMicroKernel.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/ConcreteMicroKernel.php @@ -22,7 +22,7 @@ use Symfony\Component\HttpKernel\Event\ExceptionEvent; use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpKernel\KernelEvents; -use Symfony\Component\Routing\RouteCollectionBuilder; +use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; class ConcreteMicroKernel extends Kernel implements EventSubscriberInterface { @@ -80,10 +80,10 @@ public function __destruct() $fs->remove($this->cacheDir); } - protected function configureRoutes(RouteCollectionBuilder $routes) + protected function configureRouting(RoutingConfigurator $routes): void { - $routes->add('/', 'kernel::halloweenAction'); - $routes->add('/danger', 'kernel::dangerousAction'); + $routes->add('halloween', '/')->controller('kernel::halloweenAction'); + $routes->add('danger', '/danger')->controller('kernel::dangerousAction'); } protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php index dd909ea6fc8c..a66ebeffdcc3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php @@ -19,6 +19,18 @@ class MicroKernelTraitTest extends TestCase { + /** + * @group legacy + * @expectedDeprecation Adding routes via the "Symfony\Bundle\FrameworkBundle\Tests\Kernel\MicroKernelWithConfigureRoutes:configureRoutes()" method is deprecated since Symfony 5.1 and will have no effect in 6.0; use "configureRouting()" instead. + * @expectedDeprecation Not overriding the "Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait::configureRouting()" method is deprecated since Symfony 5.1 and will trigger a fatal error in 6.0. + */ + public function testConfigureRoutingDeprecated() + { + $kernel = new MicroKernelWithConfigureRoutes('test', false); + $kernel->boot(); + $kernel->handle(Request::create('/')); + } + public function test() { $kernel = new ConcreteMicroKernel('test', false); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelWithConfigureRoutes.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelWithConfigureRoutes.php new file mode 100644 index 000000000000..b57f301ee6c4 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelWithConfigureRoutes.php @@ -0,0 +1,74 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Kernel; + +use Psr\Log\NullLogger; +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; +use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; +use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\Routing\RouteCollectionBuilder; + +class MicroKernelWithConfigureRoutes extends Kernel +{ + use MicroKernelTrait; + + private $cacheDir; + + public function registerBundles(): iterable + { + return [ + new FrameworkBundle(), + ]; + } + + public function getCacheDir(): string + { + return $this->cacheDir = sys_get_temp_dir().'/sf_micro_kernel_with_configured_routes'; + } + + public function getLogDir(): string + { + return $this->cacheDir; + } + + public function __sleep(): array + { + throw new \BadMethodCallException('Cannot serialize '.__CLASS__); + } + + public function __wakeup() + { + throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); + } + + public function __destruct() + { + $fs = new Filesystem(); + $fs->remove($this->cacheDir); + } + + protected function configureRoutes(RouteCollectionBuilder $routes) + { + $routes->add('/', 'kernel::halloweenAction'); + } + + protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) + { + $c->register('logger', NullLogger::class); + $c->loadFromExtension('framework', [ + 'secret' => '$ecret', + ]); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index 2b4a2716b4e3..620f2d9769a9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -27,7 +27,7 @@ "symfony/polyfill-mbstring": "~1.0", "symfony/filesystem": "^4.4|^5.0", "symfony/finder": "^4.4|^5.0", - "symfony/routing": "^5.0" + "symfony/routing": "^5.1" }, "require-dev": { "doctrine/annotations": "~1.7", diff --git a/src/Symfony/Component/Routing/CHANGELOG.md b/src/Symfony/Component/Routing/CHANGELOG.md index f304a12a5958..bf52e1c35526 100644 --- a/src/Symfony/Component/Routing/CHANGELOG.md +++ b/src/Symfony/Component/Routing/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +5.1.0 +----- + + * Deprecated `RouteCollectionBuilder` in favor of `RoutingConfigurator`. + * Added support for a generic loader to `RoutingConfigurator`. + 5.0.0 ----- diff --git a/src/Symfony/Component/Routing/Loader/Configurator/RoutingConfigurator.php b/src/Symfony/Component/Routing/Loader/Configurator/RoutingConfigurator.php index 8ed06f307c64..737320bd2edd 100644 --- a/src/Symfony/Component/Routing/Loader/Configurator/RoutingConfigurator.php +++ b/src/Symfony/Component/Routing/Loader/Configurator/RoutingConfigurator.php @@ -11,7 +11,9 @@ namespace Symfony\Component\Routing\Loader\Configurator; -use Symfony\Component\Routing\Loader\PhpFileLoader; +use Symfony\Component\Config\Exception\LoaderLoadException; +use Symfony\Component\Config\Loader\FileLoader; +use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\Routing\RouteCollection; /** @@ -25,7 +27,7 @@ class RoutingConfigurator private $path; private $file; - public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file) + public function __construct(RouteCollection $collection, LoaderInterface $loader, ?string $path, string $file) { $this->collection = $collection; $this->loader = $loader; @@ -38,9 +40,7 @@ public function __construct(RouteCollection $collection, PhpFileLoader $loader, */ final public function import($resource, string $type = null, bool $ignoreErrors = false, $exclude = null): ImportConfigurator { - $this->loader->setCurrentDir(\dirname($this->path)); - - $imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file, $exclude) ?: []; + $imported = $this->load($resource, $type, $ignoreErrors, $exclude) ?: []; if (!\is_array($imported)) { return new ImportConfigurator($this->collection, $imported); } @@ -57,4 +57,34 @@ final public function collection(string $name = ''): CollectionConfigurator { return new CollectionConfigurator($this->collection, $name); } + + /** + * @param string|string[]|null $exclude + * + * @return RouteCollection|RouteCollection[]|null + */ + private function load($resource, ?string $type, bool $ignoreErrors, $exclude) + { + $loader = $this->loader; + + if (!$loader->supports($resource, $type)) { + if (null === $resolver = $loader->getResolver()) { + throw new LoaderLoadException($resource, $this->file, null, null, $type); + } + + if (false === $loader = $resolver->resolve($resource, $type)) { + throw new LoaderLoadException($resource, $this->file, null, null, $type); + } + } + + if (!$loader instanceof FileLoader) { + return $loader->load($resource, $type); + } + + if (null !== $this->path) { + $this->loader->setCurrentDir(\dirname($this->path)); + } + + return $this->loader->import($resource, $type, $ignoreErrors, $this->file, $exclude); + } } diff --git a/src/Symfony/Component/Routing/RouteCollectionBuilder.php b/src/Symfony/Component/Routing/RouteCollectionBuilder.php index 406e3c0acbe5..4bbcf795a1ab 100644 --- a/src/Symfony/Component/Routing/RouteCollectionBuilder.php +++ b/src/Symfony/Component/Routing/RouteCollectionBuilder.php @@ -19,6 +19,8 @@ * Helps add and import routes into a RouteCollection. * * @author Ryan Weaver + * + * @deprecated since Symfony 5.1, use RoutingConfigurator instead */ class RouteCollectionBuilder { diff --git a/src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php b/src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php index f5042749e2eb..d18ee37bd017 100644 --- a/src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php @@ -19,6 +19,9 @@ use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollectionBuilder; +/** + * @group legacy + */ class RouteCollectionBuilderTest extends TestCase { public function testImport() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy