Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 250fa7e

Browse files
[FrameworkBundle] Allow configuring the default base URI with a DSN
1 parent ed1b6ea commit 250fa7e

File tree

8 files changed

+36
-27
lines changed

8 files changed

+36
-27
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CHANGELOG
66

77
* Added link to source for controllers registered as named services
88
* Added link to source on controller on `router:match`/`debug:router` (when `framework.ide` is configured)
9-
* Added the `framework.router.context` configuration node to configure the `RequestContext`
9+
* Added the `framework.router.default_uri` configuration option to configure the default `RequestContext`
1010
* Made `MicroKernelTrait::configureContainer()` compatible with `ContainerConfigurator`
1111
* Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails.
1212
* Added flex-compatible default implementations for `MicroKernelTrait::registerBundles()` and `getProjectDir()`

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ private function addRouterSection(ArrayNodeDefinition $rootNode)
470470
->children()
471471
->scalarNode('resource')->isRequired()->end()
472472
->scalarNode('type')->end()
473+
->scalarNode('default_uri')
474+
->info('The default URI used to generate URLs in a non-HTTP context')
475+
->defaultNull()
476+
->end()
473477
->scalarNode('http_port')->defaultValue(80)->end()
474478
->scalarNode('https_port')->defaultValue(443)->end()
475479
->scalarNode('strict_requirements')
@@ -482,15 +486,6 @@ private function addRouterSection(ArrayNodeDefinition $rootNode)
482486
->defaultTrue()
483487
->end()
484488
->booleanNode('utf8')->defaultNull()->end()
485-
->arrayNode('context')
486-
->info('The request context used to generate URLs in a non-HTTP context')
487-
->addDefaultsIfNotSet()
488-
->children()
489-
->scalarNode('host')->defaultValue('%router.request_context.host%')->end()
490-
->scalarNode('scheme')->defaultValue('%router.request_context.scheme%')->end()
491-
->scalarNode('base_url')->defaultValue('%router.request_context.base_url%')->end()
492-
->end()
493-
->end()
494489
->end()
495490
->end()
496491
->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,10 +899,10 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
899899
$container->setParameter('request_listener.http_port', $config['http_port']);
900900
$container->setParameter('request_listener.https_port', $config['https_port']);
901901

902-
$requestContext = $container->getDefinition('router.request_context');
903-
$requestContext->replaceArgument(0, $config['context']['base_url']);
904-
$requestContext->replaceArgument(2, $config['context']['host']);
905-
$requestContext->replaceArgument(3, $config['context']['scheme']);
902+
if (null !== $config['default_uri']) {
903+
$container->getDefinition('router.request_context')
904+
->replaceArgument(0, $config['default_uri']);
905+
}
906906

907907
if ($this->annotationsConfigEnabled) {
908908
$container->register('routing.loader.annotation', AnnotatedRouteControllerLoader::class)

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@
8080
<service id="Symfony\Component\Routing\RequestContextAwareInterface" alias="router" />
8181

8282
<service id="router.request_context" class="Symfony\Component\Routing\RequestContext">
83-
<argument></argument> <!-- base_url -->
84-
<argument>GET</argument>
85-
<argument></argument> <!-- host -->
86-
<argument></argument> <!-- scheme -->
83+
<factory class="Symfony\Component\Routing\RequestContext" method="fromUri" />
84+
<argument>%router.request_context.base_url%</argument>
85+
<argument>%router.request_context.host%</argument>
86+
<argument>%router.request_context.scheme%</argument>
8787
<argument>%request_listener.http_port%</argument>
8888
<argument>%request_listener.https_port%</argument>
8989
<call method="setParameter">
@@ -117,8 +117,8 @@
117117

118118
<service id="Symfony\Bundle\FrameworkBundle\Controller\RedirectController" public="true">
119119
<argument type="service" id="router" />
120-
<argument>%request_listener.http_port%</argument>
121-
<argument>%request_listener.https_port%</argument>
120+
<argument type="service"><service class="int"><factory service="router.request_context" method="getHttpPort" /></service></argument>
121+
<argument type="service"><service class="int"><factory service="router.request_context" method="getHttpsPort" /></service></argument>
122122
</service>
123123

124124
<service id="Symfony\Bundle\FrameworkBundle\Controller\TemplateController" public="true">

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,11 @@ protected static function getBundleDefaultConfig()
410410
],
411411
'router' => [
412412
'enabled' => false,
413+
'default_uri' => null,
413414
'http_port' => 80,
414415
'https_port' => 443,
415416
'strict_requirements' => true,
416417
'utf8' => null,
417-
'context' => [
418-
'host' => '%router.request_context.host%',
419-
'scheme' => '%router.request_context.scheme%',
420-
'base_url' => '%router.request_context.base_url%',
421-
],
422418
],
423419
'session' => [
424420
'enabled' => false,

src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
</service>
2121

2222
<service id="security.authentication.retry_entry_point" class="Symfony\Component\Security\Http\EntryPoint\RetryAuthenticationEntryPoint">
23-
<argument>%request_listener.http_port%</argument>
24-
<argument>%request_listener.https_port%</argument>
23+
<argument type="service"><service class="int"><factory service="router.request_context" method="getHttpPort" /></service></argument>
24+
<argument type="service"><service class="int"><factory service="router.request_context" method="getHttpsPort" /></service></argument>
2525
</service>
2626

2727
<service id="security.authentication.basic_entry_point" class="Symfony\Component\Security\Http\EntryPoint\BasicAuthenticationEntryPoint" />

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* added `ExpressionLanguageProvider` to expose extra functions to route conditions
1313
* added support for a `stateless` keyword for configuring route stateless in PHP, YAML and XML configurations.
1414
* added the "hosts" option to be able to configure the host per locale.
15+
* added `RequestContext::fromUri()` to ease building the default context
1516

1617
5.0.0
1718
-----

src/Symfony/Component/Routing/RequestContext.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ public function __construct(string $baseUrl = '', string $method = 'GET', string
4545
$this->setQueryString($queryString);
4646
}
4747

48+
public static function fromUri(string $uri, string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443): self
49+
{
50+
$uri = parse_url($uri);
51+
$scheme = $uri['scheme'] ?? $scheme;
52+
$host = $uri['host'] ?? $host;
53+
54+
if (isset($uri['port'])) {
55+
if ('http' === $scheme) {
56+
$httpPort = $uri['port'];
57+
} elseif ('https' === $scheme) {
58+
$httpsPort = $uri['port'];
59+
}
60+
}
61+
62+
return new self($uri['path'] ?? '', 'GET', $host, $scheme, $httpPort, $httpsPort);
63+
}
64+
4865
/**
4966
* Updates the RequestContext information based on a HttpFoundation Request.
5067
*

0 commit comments

Comments
 (0)