Skip to content

Commit

Permalink
minor #37280 [Routing] Move configuration to PHP (phamuyentri)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 5.2-dev branch.

Discussion
----------

[Routing] Move configuration to PHP

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | [#37186](#37186)
| License       | MIT

Moving routing configurations in FrameworkBundle to PHP

Commits
-------

268edd8 [Routing] Move configuration to PHP
  • Loading branch information
Tobion committed Jun 15, 2020
2 parents dadc606 + 268edd8 commit 7f5d876
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerProfilerConfiguration($config['profiler'], $container, $loader, $phpLoader);
$this->registerWorkflowConfiguration($config['workflows'], $container, $loader);
$this->registerDebugConfiguration($config['php_errors'], $container, $phpLoader);
$this->registerRouterConfiguration($config['router'], $container, $loader, $config['translator']['enabled_locales'] ?? []);
$this->registerRouterConfiguration($config['router'], $container, $phpLoader, $config['translator']['enabled_locales'] ?? []);
$this->registerAnnotationsConfiguration($config['annotations'], $container, $loader);
$this->registerPropertyAccessConfiguration($config['property_access'], $container, $phpLoader);
$this->registerSecretsConfiguration($config['secrets'], $container, $phpLoader);
Expand Down Expand Up @@ -863,7 +863,7 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con
}
}

private function registerRouterConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader, array $enabledLocales = [])
private function registerRouterConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader, array $enabledLocales = [])
{
if (!$this->isConfigEnabled($container, $config)) {
$container->removeDefinition('console.command.router_debug');
Expand All @@ -872,7 +872,7 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
return;
}

$loader->load('routing.xml');
$loader->load('routing.php');

if (null === $config['utf8']) {
trigger_deprecation('symfony/framework-bundle', '5.1', 'Not setting the "framework.router.utf8" configuration option is deprecated, it will default to "true" in version 6.0.');
Expand Down
176 changes: 176 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Psr\Container\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
use Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader;
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableCompiledUrlMatcher;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\Routing\Generator\CompiledUrlGenerator;
use Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Loader\ContainerLoader;
use Symfony\Component\Routing\Loader\DirectoryLoader;
use Symfony\Component\Routing\Loader\GlobFileLoader;
use Symfony\Component\Routing\Loader\PhpFileLoader;
use Symfony\Component\Routing\Loader\XmlFileLoader;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
use Symfony\Component\Routing\Matcher\ExpressionLanguageProvider;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RequestContextAwareInterface;
use Symfony\Component\Routing\RouterInterface;

return static function (ContainerConfigurator $container) {
$container->parameters()
->set('router.request_context.host', 'localhost')
->set('router.request_context.scheme', 'http')
->set('router.request_context.base_url', '')
;

$container->services()
->set('routing.resolver', LoaderResolver::class)

->set('routing.loader.xml', XmlFileLoader::class)
->args([
service('file_locator'),
])
->tag('routing.loader')

->set('routing.loader.yml', YamlFileLoader::class)
->args([
service('file_locator'),
])
->tag('routing.loader')

->set('routing.loader.php', PhpFileLoader::class)
->args([
service('file_locator'),
])
->tag('routing.loader')

->set('routing.loader.glob', GlobFileLoader::class)
->args([
service('file_locator'),
])
->tag('routing.loader')

->set('routing.loader.directory', DirectoryLoader::class)
->args([
service('file_locator'),
])
->tag('routing.loader')

->set('routing.loader.container', ContainerLoader::class)
->args([
tagged_locator('routing.route_loader'),
])
->tag('routing.loader')

->set('routing.loader', DelegatingLoader::class)
->public()
->args([
service('routing.resolver'),
[], // Default options
[], // Default requirements
])

->set('router.default', Router::class)
->args([
service(ContainerInterface::class),
param('router.resource'),
[
'cache_dir' => param('kernel.cache_dir'),
'debug' => param('kernel.debug'),
'generator_class' => CompiledUrlGenerator::class,
'generator_dumper_class' => CompiledUrlGeneratorDumper::class,
'matcher_class' => RedirectableCompiledUrlMatcher::class,
'matcher_dumper_class' => CompiledUrlMatcherDumper::class,
],
service('router.request_context')->ignoreOnInvalid(),
service('parameter_bag')->ignoreOnInvalid(),
service('logger')->ignoreOnInvalid(),
param('kernel.default_locale'),
])
->call('setConfigCacheFactory', [
service('config_cache_factory'),
])
->tag('monolog.logger', ['channel' => 'router'])
->tag('container.service_subscriber', ['id' => 'routing.loader'])
->alias('router', 'router.default')
->public()
->alias(RouterInterface::class, 'router')
->alias(UrlGeneratorInterface::class, 'router')
->alias(UrlMatcherInterface::class, 'router')
->alias(RequestContextAwareInterface::class, 'router')

->set('router.request_context', RequestContext::class)
->factory([RequestContext::class, 'fromUri'])
->args([
param('router.request_context.base_url'),
param('router.request_context.host'),
param('router.request_context.scheme'),
param('request_listener.http_port'),
param('request_listener.https_port'),
])
->call('setParameter', [
'_functions',
service('router.expression_language_provider')->ignoreOnInvalid(),
])
->alias(RequestContext::class, 'router.request_context')

->set('router.expression_language_provider', ExpressionLanguageProvider::class)
->args([
tagged_locator('routing.expression_language_function', 'function'),
])
->tag('routing.expression_language_provider')

->set('router.cache_warmer', RouterCacheWarmer::class)
->args([service(ContainerInterface::class)])
->tag('container.service_subscriber', ['id' => 'router'])
->tag('kernel.cache_warmer')

->set('router_listener', RouterListener::class)
->args([
service('router'),
service('request_stack'),
service('router.request_context')->ignoreOnInvalid(),
service('logger')->ignoreOnInvalid(),
param('kernel.project_dir'),
param('kernel.debug'),
])
->tag('kernel.event_subscriber')
->tag('monolog.logger', ['channel' => 'request'])

->set(RedirectController::class)
->public()
->args([
service('router'),
inline_service('int')
->factory([service('router.request_context'), 'getHttpPort']),
inline_service('int')
->factory([service('router.request_context'), 'getHttpsPort']),
])

->set(TemplateController::class)
->args([
service('twig')->ignoreOnInvalid(),
])
->public()
;
};
128 changes: 0 additions & 128 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

This file was deleted.

0 comments on commit 7f5d876

Please sign in to comment.