[FrameworkBundle] Allow configuring the default base URI with a DSN

This commit is contained in:
Nicolas Grekas 2020-05-01 09:59:31 +02:00
parent ed1b6eaf83
commit 250fa7e979
8 changed files with 36 additions and 27 deletions

View File

@ -6,7 +6,7 @@ CHANGELOG
* Added link to source for controllers registered as named services * 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 link to source on controller on `router:match`/`debug:router` (when `framework.ide` is configured)
* Added the `framework.router.context` configuration node to configure the `RequestContext` * Added the `framework.router.default_uri` configuration option to configure the default `RequestContext`
* Made `MicroKernelTrait::configureContainer()` compatible with `ContainerConfigurator` * 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. * Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails.
* Added flex-compatible default implementations for `MicroKernelTrait::registerBundles()` and `getProjectDir()` * Added flex-compatible default implementations for `MicroKernelTrait::registerBundles()` and `getProjectDir()`

View File

@ -470,6 +470,10 @@ class Configuration implements ConfigurationInterface
->children() ->children()
->scalarNode('resource')->isRequired()->end() ->scalarNode('resource')->isRequired()->end()
->scalarNode('type')->end() ->scalarNode('type')->end()
->scalarNode('default_uri')
->info('The default URI used to generate URLs in a non-HTTP context')
->defaultNull()
->end()
->scalarNode('http_port')->defaultValue(80)->end() ->scalarNode('http_port')->defaultValue(80)->end()
->scalarNode('https_port')->defaultValue(443)->end() ->scalarNode('https_port')->defaultValue(443)->end()
->scalarNode('strict_requirements') ->scalarNode('strict_requirements')
@ -482,15 +486,6 @@ class Configuration implements ConfigurationInterface
->defaultTrue() ->defaultTrue()
->end() ->end()
->booleanNode('utf8')->defaultNull()->end() ->booleanNode('utf8')->defaultNull()->end()
->arrayNode('context')
->info('The request context used to generate URLs in a non-HTTP context')
->addDefaultsIfNotSet()
->children()
->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()
->end()
->end()
->end() ->end()
->end() ->end()
->end() ->end()

View File

@ -899,10 +899,10 @@ class FrameworkExtension extends Extension
$container->setParameter('request_listener.http_port', $config['http_port']); $container->setParameter('request_listener.http_port', $config['http_port']);
$container->setParameter('request_listener.https_port', $config['https_port']); $container->setParameter('request_listener.https_port', $config['https_port']);
$requestContext = $container->getDefinition('router.request_context'); if (null !== $config['default_uri']) {
$requestContext->replaceArgument(0, $config['context']['base_url']); $container->getDefinition('router.request_context')
$requestContext->replaceArgument(2, $config['context']['host']); ->replaceArgument(0, $config['default_uri']);
$requestContext->replaceArgument(3, $config['context']['scheme']); }
if ($this->annotationsConfigEnabled) { if ($this->annotationsConfigEnabled) {
$container->register('routing.loader.annotation', AnnotatedRouteControllerLoader::class) $container->register('routing.loader.annotation', AnnotatedRouteControllerLoader::class)

View File

@ -80,10 +80,10 @@
<service id="Symfony\Component\Routing\RequestContextAwareInterface" alias="router" /> <service id="Symfony\Component\Routing\RequestContextAwareInterface" alias="router" />
<service id="router.request_context" class="Symfony\Component\Routing\RequestContext"> <service id="router.request_context" class="Symfony\Component\Routing\RequestContext">
<argument></argument> <!-- base_url --> <factory class="Symfony\Component\Routing\RequestContext" method="fromUri" />
<argument>GET</argument> <argument>%router.request_context.base_url%</argument>
<argument></argument> <!-- host --> <argument>%router.request_context.host%</argument>
<argument></argument> <!-- scheme --> <argument>%router.request_context.scheme%</argument>
<argument>%request_listener.http_port%</argument> <argument>%request_listener.http_port%</argument>
<argument>%request_listener.https_port%</argument> <argument>%request_listener.https_port%</argument>
<call method="setParameter"> <call method="setParameter">
@ -117,8 +117,8 @@
<service id="Symfony\Bundle\FrameworkBundle\Controller\RedirectController" public="true"> <service id="Symfony\Bundle\FrameworkBundle\Controller\RedirectController" public="true">
<argument type="service" id="router" /> <argument type="service" id="router" />
<argument>%request_listener.http_port%</argument> <argument type="service"><service class="int"><factory service="router.request_context" method="getHttpPort" /></service></argument>
<argument>%request_listener.https_port%</argument> <argument type="service"><service class="int"><factory service="router.request_context" method="getHttpsPort" /></service></argument>
</service> </service>
<service id="Symfony\Bundle\FrameworkBundle\Controller\TemplateController" public="true"> <service id="Symfony\Bundle\FrameworkBundle\Controller\TemplateController" public="true">

View File

@ -410,15 +410,11 @@ class ConfigurationTest extends TestCase
], ],
'router' => [ 'router' => [
'enabled' => false, 'enabled' => false,
'default_uri' => null,
'http_port' => 80, 'http_port' => 80,
'https_port' => 443, 'https_port' => 443,
'strict_requirements' => true, 'strict_requirements' => true,
'utf8' => null, 'utf8' => null,
'context' => [
'host' => '%router.request_context.host%',
'scheme' => '%router.request_context.scheme%',
'base_url' => '%router.request_context.base_url%',
],
], ],
'session' => [ 'session' => [
'enabled' => false, 'enabled' => false,

View File

@ -20,8 +20,8 @@
</service> </service>
<service id="security.authentication.retry_entry_point" class="Symfony\Component\Security\Http\EntryPoint\RetryAuthenticationEntryPoint"> <service id="security.authentication.retry_entry_point" class="Symfony\Component\Security\Http\EntryPoint\RetryAuthenticationEntryPoint">
<argument>%request_listener.http_port%</argument> <argument type="service"><service class="int"><factory service="router.request_context" method="getHttpPort" /></service></argument>
<argument>%request_listener.https_port%</argument> <argument type="service"><service class="int"><factory service="router.request_context" method="getHttpsPort" /></service></argument>
</service> </service>
<service id="security.authentication.basic_entry_point" class="Symfony\Component\Security\Http\EntryPoint\BasicAuthenticationEntryPoint" /> <service id="security.authentication.basic_entry_point" class="Symfony\Component\Security\Http\EntryPoint\BasicAuthenticationEntryPoint" />

View File

@ -12,6 +12,7 @@ CHANGELOG
* added `ExpressionLanguageProvider` to expose extra functions to route conditions * added `ExpressionLanguageProvider` to expose extra functions to route conditions
* added support for a `stateless` keyword for configuring route stateless in PHP, YAML and XML configurations. * added support for a `stateless` keyword for configuring route stateless in PHP, YAML and XML configurations.
* added the "hosts" option to be able to configure the host per locale. * added the "hosts" option to be able to configure the host per locale.
* added `RequestContext::fromUri()` to ease building the default context
5.0.0 5.0.0
----- -----

View File

@ -45,6 +45,23 @@ class RequestContext
$this->setQueryString($queryString); $this->setQueryString($queryString);
} }
public static function fromUri(string $uri, string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443): self
{
$uri = parse_url($uri);
$scheme = $uri['scheme'] ?? $scheme;
$host = $uri['host'] ?? $host;
if (isset($uri['port'])) {
if ('http' === $scheme) {
$httpPort = $uri['port'];
} elseif ('https' === $scheme) {
$httpsPort = $uri['port'];
}
}
return new self($uri['path'] ?? '', 'GET', $host, $scheme, $httpPort, $httpsPort);
}
/** /**
* Updates the RequestContext information based on a HttpFoundation Request. * Updates the RequestContext information based on a HttpFoundation Request.
* *