From 84ba43d1ca3021a39d464ab656fbbb4d4c3c4930 Mon Sep 17 00:00:00 2001 From: Danil Pyatnitsev Date: Tue, 4 Feb 2020 00:10:14 +0300 Subject: [PATCH 1/3] [FrameworkBundle] added new parameter router.request_context.url The router.request_context.url can be used instead of router.request_context / router.request_context.host etc to define url in console commands --- .../Compiler/RequestContextPass.php | 48 +++++++++++++++++++ .../DependencyInjection/Configuration.php | 1 + .../FrameworkBundle/FrameworkBundle.php | 2 + .../Resources/config/routing.xml | 1 + .../Compiler/RequestContextPassTest.php | 46 ++++++++++++++++++ .../DependencyInjection/ConfigurationTest.php | 1 + 6 files changed, 99 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RequestContextPassTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php new file mode 100644 index 0000000000000..84fc64529802a --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** + * Add support of url property for routing generator + * + * @author Danil Pyatnitsev + */ +class RequestContextPass implements CompilerPassInterface +{ + + public function process(ContainerBuilder $container) + { + if (false === $container->hasParameter('router.request_context.url')) { + return; + } + + $url = $container->getParameter('router.request_context.url'); + $urlComponents = parse_url($url); + + if (isset($urlComponents['scheme'])) { + $container->setParameter('router.request_context.scheme', $urlComponents['scheme']); + } + if (isset($urlComponents['host'])) { + $container->setParameter('router.request_context.host', $urlComponents['host']); + } + if (isset($urlComponents['port'])) { + $name = (isset($urlComponents['scheme']) && 'https' === $urlComponents['scheme']) ? 'https' : 'http'; + $container->setParameter("request_listener.{$name}_port", $urlComponents['port']); + } + if (isset($urlComponents['path'])) { + $container->setParameter("router.request_context.base_url", $urlComponents['path']); + } + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 8ee78c3e5b72e..b9011c69507f9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -489,6 +489,7 @@ private function addRouterSection(ArrayNodeDefinition $rootNode) ->scalarNode('host')->defaultValue('%router.request_context.host%')->end() ->scalarNode('scheme')->defaultValue('%router.request_context.scheme%')->end() ->scalarNode('base_url')->defaultValue('%router.request_context.base_url%')->end() + ->scalarNode('url')->defaultValue('%router.request_context.url%')->end() ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php index 6cafc4399e31a..c036e8e3ecf40 100644 --- a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php +++ b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php @@ -18,6 +18,7 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass; +use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RequestContextPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass; @@ -130,6 +131,7 @@ public function build(ContainerBuilder $container) $this->addCompilerPassIfExists($container, AddAutoMappingConfigurationPass::class); $container->addCompilerPass(new RegisterReverseContainerPass(true)); $container->addCompilerPass(new RegisterReverseContainerPass(false), PassConfig::TYPE_AFTER_REMOVING); + $container->addCompilerPass(new RequestContextPass()); if ($container->getParameter('kernel.debug')) { $container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 2); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml index 96ac2c72b4b23..43913caa84e40 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml @@ -8,6 +8,7 @@ localhost http + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RequestContextPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RequestContextPassTest.php new file mode 100644 index 0000000000000..0974749ed6b0b --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RequestContextPassTest.php @@ -0,0 +1,46 @@ +setParameter('router.request_context.url', 'https://foo.example.com:8080/bar'); + $container->addCompilerPass(new RequestContextPass()); + + $container->register('router', '\stdClass')->setPublic(true); + $container->compile(); + + $this->assertEquals('foo.example.com', $container->getParameter('router.request_context.host')); + $this->assertEquals('https', $container->getParameter('router.request_context.scheme')); + $this->assertEquals('/bar', $container->getParameter('router.request_context.base_url')); + $this->assertEquals('8080', $container->getParameter('request_listener.https_port')); + } + + public function testRouterRequestContextUrlParseWithoutBaseUrlTest() + { + $container = new ContainerBuilder(); + $container->setParameter('router.request_context.url', 'https://foo.example.com:8080'); + $container->addCompilerPass(new RequestContextPass()); + + $container->register('router', '\stdClass')->setPublic(true); + $container->compile(); + + $this->assertEquals('foo.example.com', $container->getParameter('router.request_context.host')); + $this->assertEquals('https', $container->getParameter('router.request_context.scheme')); + $this->assertEquals(false, $container->hasParameter('router.request_context.base_url')); + $this->assertEquals('8080', $container->getParameter('request_listener.https_port')); + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index 7e3d096180e34..c746d2c9f6c17 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -417,6 +417,7 @@ protected static function getBundleDefaultConfig() 'host' => '%router.request_context.host%', 'scheme' => '%router.request_context.scheme%', 'base_url' => '%router.request_context.base_url%', + 'url' => '%router.request_context.url%' ], ], 'session' => [ From ed1bf4784b7e2d5f3e087d5c14f76271de9ded1d Mon Sep 17 00:00:00 2001 From: Danil Pyatnitsev Date: Tue, 4 Feb 2020 00:28:08 +0300 Subject: [PATCH 2/3] [FrameworkBundle] fixed code standards --- .../Compiler/RequestContextPass.php | 3 +-- .../Compiler/RequestContextPassTest.php | 17 ++++++++++------- .../DependencyInjection/ConfigurationTest.php | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php index 84fc64529802a..aaa00e469fbb4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php @@ -15,13 +15,12 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; /** - * Add support of url property for routing generator + * Add support of url property for routing generator. * * @author Danil Pyatnitsev */ class RequestContextPass implements CompilerPassInterface { - public function process(ContainerBuilder $container) { if (false === $container->hasParameter('router.request_context.url')) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RequestContextPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RequestContextPassTest.php index 0974749ed6b0b..8d9dead825a55 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RequestContextPassTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/RequestContextPassTest.php @@ -1,9 +1,12 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. */ namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; @@ -40,7 +43,7 @@ public function testRouterRequestContextUrlParseWithoutBaseUrlTest() $this->assertEquals('foo.example.com', $container->getParameter('router.request_context.host')); $this->assertEquals('https', $container->getParameter('router.request_context.scheme')); - $this->assertEquals(false, $container->hasParameter('router.request_context.base_url')); + $this->assertFalse(false, $container->hasParameter('router.request_context.base_url')); $this->assertEquals('8080', $container->getParameter('request_listener.https_port')); } -} \ No newline at end of file +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index c746d2c9f6c17..c3bf54919fe92 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -417,7 +417,7 @@ protected static function getBundleDefaultConfig() 'host' => '%router.request_context.host%', 'scheme' => '%router.request_context.scheme%', 'base_url' => '%router.request_context.base_url%', - 'url' => '%router.request_context.url%' + 'url' => '%router.request_context.url%', ], ], 'session' => [ From 7465340c4dc74fabf145bacb27422f3c2f1187c1 Mon Sep 17 00:00:00 2001 From: Danil Pyatnitsev Date: Tue, 4 Feb 2020 00:31:08 +0300 Subject: [PATCH 3/3] [FrameworkBundle] fixed code standards --- .../DependencyInjection/Compiler/RequestContextPass.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php index aaa00e469fbb4..de799fcce2aa5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RequestContextPass.php @@ -41,7 +41,7 @@ public function process(ContainerBuilder $container) $container->setParameter("request_listener.{$name}_port", $urlComponents['port']); } if (isset($urlComponents['path'])) { - $container->setParameter("router.request_context.base_url", $urlComponents['path']); + $container->setParameter('router.request_context.base_url', $urlComponents['path']); } } -} \ No newline at end of file +}