feature #36651 [FrameworkBundle] Allow configuring the default base URI with a DSN (nicolas-grekas)

This PR was merged into the 5.1-dev branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fixes #35121, replaces #35580, partially reverts #35281
| License       | MIT
| Doc PR        | -

Instead of defining 3-4 parameters, this PR enables using a single DSN to configure the default URL context (for commands mainly):
```
framework:
	router:
		base_uri: 'https://my.host:8443/base-path/'
```

When using parameters directly, one can now set the same absolute URI in the `router.request_context.base_url` parameter, this will provide the same benefit.

Commits
-------

250fa7e979 [FrameworkBundle] Allow configuring the default base URI with a DSN
This commit is contained in:
Fabien Potencier 2020-05-04 09:42:27 +02:00
commit e9be7418a3
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 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`
* 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()`

View File

@ -470,6 +470,10 @@ class Configuration implements ConfigurationInterface
->children()
->scalarNode('resource')->isRequired()->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('https_port')->defaultValue(443)->end()
->scalarNode('strict_requirements')
@ -482,15 +486,6 @@ class Configuration implements ConfigurationInterface
->defaultTrue()
->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()

View File

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

View File

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

View File

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

View File

@ -20,8 +20,8 @@
</service>
<service id="security.authentication.retry_entry_point" class="Symfony\Component\Security\Http\EntryPoint\RetryAuthenticationEntryPoint">
<argument>%request_listener.http_port%</argument>
<argument>%request_listener.https_port%</argument>
<argument type="service"><service class="int"><factory service="router.request_context" method="getHttpPort" /></service></argument>
<argument type="service"><service class="int"><factory service="router.request_context" method="getHttpsPort" /></service></argument>
</service>
<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 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 `RequestContext::fromUri()` to ease building the default context
5.0.0
-----

View File

@ -45,6 +45,23 @@ class RequestContext
$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.
*