From 6f83e11b922acb5695097cb6578e4cb2af58d3ea Mon Sep 17 00:00:00 2001 From: k911 Date: Mon, 27 Aug 2018 22:40:19 +0200 Subject: [PATCH] perf(swoole): Improve Dependency Injection configuration Allow to enable or disable drivers basing on configuration settings By either decorating or not, HttpServerDriverInterface --- ...verDriver.php => EntityManagerHandler.php} | 2 +- DependencyInjection/Configuration.php | 25 ++++++++-- DependencyInjection/SwooleExtension.php | 49 +++++++++++++++++-- Resources/config/services.yaml | 20 +------- 4 files changed, 68 insertions(+), 28 deletions(-) rename Bridge/Doctrine/ORM/{EntityManagerHttpServerDriver.php => EntityManagerHandler.php} (93%) diff --git a/Bridge/Doctrine/ORM/EntityManagerHttpServerDriver.php b/Bridge/Doctrine/ORM/EntityManagerHandler.php similarity index 93% rename from Bridge/Doctrine/ORM/EntityManagerHttpServerDriver.php rename to Bridge/Doctrine/ORM/EntityManagerHandler.php index b1cedfc8..e68897b6 100644 --- a/Bridge/Doctrine/ORM/EntityManagerHttpServerDriver.php +++ b/Bridge/Doctrine/ORM/EntityManagerHandler.php @@ -9,7 +9,7 @@ use Swoole\Http\Request; use Swoole\Http\Response; -final class EntityManagerHttpServerDriver implements HttpServerDriverInterface +final class EntityManagerHandler implements HttpServerDriverInterface { private $decorated; private $connection; diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 83a6b481..8c8ddb7e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -28,7 +28,7 @@ public function getConfigTreeBuilder(): TreeBuilder $rootNode ->children() - ->arrayNode('server') + ->arrayNode('http_server') ->children() ->scalarNode('host') ->defaultValue('127.0.0.1') @@ -38,10 +38,25 @@ public function getConfigTreeBuilder(): TreeBuilder ->max(65535) ->defaultValue(9501) ->end() - ->booleanNode('use_advanced_static_handler') - ->defaultFalse() - ->treatNullLike(false) - ->end() + ->arrayNode('drivers') + ->children() + ->booleanNode('debug') + ->defaultFalse() + ->treatNullLike(false) + ->end() + ->booleanNode('trust_all_proxies') + ->defaultFalse() + ->treatNullLike(false) + ->end() + ->booleanNode('entity_manager_handler') + ->defaultNull() + ->end() + ->booleanNode('advanced_static_handler') + ->defaultFalse() + ->treatNullLike(false) + ->end() + ->end() + ->end() // drivers ->arrayNode('settings') ->children() ->booleanNode('serve_static_files') diff --git a/DependencyInjection/SwooleExtension.php b/DependencyInjection/SwooleExtension.php index ff77e199..745b0a8c 100644 --- a/DependencyInjection/SwooleExtension.php +++ b/DependencyInjection/SwooleExtension.php @@ -4,9 +4,14 @@ namespace App\Bundle\SwooleBundle\DependencyInjection; +use App\Bundle\SwooleBundle\Bridge\Doctrine\ORM\EntityManagerHandler; +use App\Bundle\SwooleBundle\Bridge\Symfony\HttpFoundation\TrustAllProxiesHttpServerDriver; +use App\Bundle\SwooleBundle\Bridge\Symfony\HttpKernel\DebugHttpKernelHttpServerDriver; use App\Bundle\SwooleBundle\Server\AdvancedStaticFilesHandler; use App\Bundle\SwooleBundle\Server\HttpServerConfiguration; use App\Bundle\SwooleBundle\Server\HttpServerDriverInterface; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; @@ -36,7 +41,7 @@ public function load(array $configs, ContainerBuilder $container): void $configuration = Configuration::fromTreeBuilder(); $config = $this->processConfiguration($configuration, $configs); - $this->registerServer($config['server'], $container); + $this->registerHttpServer($config['http_server'], $container); } /** @@ -45,7 +50,7 @@ public function load(array $configs, ContainerBuilder $container): void * * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException */ - private function registerServer(array $config, ContainerBuilder $container): void + private function registerHttpServer(array $config, ContainerBuilder $container): void { $container->getDefinition('app.swoole.server.http_server.server_instance') ->addArgument($config['host']) @@ -58,7 +63,45 @@ private function registerServer(array $config, ContainerBuilder $container): voi ->addArgument($config['port']) ->addArgument($config['settings'] ?? []); - if (true === $config['use_advanced_static_handler']) { + if (!empty($config['drivers'])) { + $this->registerHttpServerDrivers($config['drivers'], $container); + } + } + + /** + * @param array $config + * @param ContainerBuilder $container + */ + private function registerHttpServerDrivers(array $config, ContainerBuilder $container): void + { + if ($config['trust_all_proxies']) { + $container->register(TrustAllProxiesHttpServerDriver::class) + ->addArgument(new Reference(TrustAllProxiesHttpServerDriver::class.'.inner')) + ->setAutowired(true) + ->setPublic(false) + ->setDecoratedService(HttpServerDriverInterface::class, null, -10); + } + + if ( + $config['entity_manager_handler'] || + (null === $config['entity_manager_handler'] && \class_exists(EntityManager::class) && $container->has(EntityManagerInterface::class)) + ) { + $container->register(EntityManagerHandler::class) + ->addArgument(new Reference(EntityManagerHandler::class.'.inner')) + ->setAutowired(true) + ->setPublic(false) + ->setDecoratedService(HttpServerDriverInterface::class, null, -20); + } + + if ($config['debug']) { + $container->register(DebugHttpKernelHttpServerDriver::class) + ->addArgument(new Reference(DebugHttpKernelHttpServerDriver::class.'.inner')) + ->setAutowired(true) + ->setPublic(false) + ->setDecoratedService(HttpServerDriverInterface::class, null, -50); + } + + if ($config['advanced_static_handler']) { $container->register(AdvancedStaticFilesHandler::class) ->addArgument(new Reference(AdvancedStaticFilesHandler::class.'.inner')) ->setAutowired(true) diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 0c38cfb8..02a2bd93 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -22,24 +22,6 @@ services: 'App\Bundle\SwooleBundle\Server\HttpServerDriverInterface': class: App\Bundle\SwooleBundle\Bridge\Symfony\HttpKernel\HttpKernelHttpServerDriver - 'App\Bundle\SwooleBundle\Bridge\Symfony\HttpFoundation\TrustAllProxiesHttpServerDriver': - decorates: App\Bundle\SwooleBundle\Server\HttpServerDriverInterface - decoration_priority: -10 - arguments: ['@App\Bundle\SwooleBundle\Bridge\Symfony\HttpFoundation\TrustAllProxiesHttpServerDriver.inner'] - autoconfigure: false - - 'App\Bundle\SwooleBundle\Bridge\Doctrine\ORM\EntityManagerHttpServerDriver': - decorates: App\Bundle\SwooleBundle\Server\HttpServerDriverInterface - decoration_priority: -20 - arguments: ['@App\Bundle\SwooleBundle\Bridge\Doctrine\ORM\EntityManagerHttpServerDriver.inner'] - autoconfigure: false - - 'App\Bundle\SwooleBundle\Bridge\Symfony\HttpKernel\DebugHttpKernelHttpServerDriver': - decorates: App\Bundle\SwooleBundle\Server\HttpServerDriverInterface - decoration_priority: -50 - arguments: ['@App\Bundle\SwooleBundle\Bridge\Symfony\HttpKernel\DebugHttpKernelHttpServerDriver.inner'] - autoconfigure: false - 'App\Bundle\SwooleBundle\Server\LimitedHttpServerDriver': 'App\Bundle\SwooleBundle\Server\HttpServerConfiguration': @@ -57,4 +39,4 @@ services: 'App\Bundle\SwooleBundle\Command\ServerProfileCommand': tags: ['console.command'] arguments: - $driver: '@App\Bundle\SwooleBundle\Server\LimitedHttpServerDriver' + $driver: '@App\Bundle\SwooleBundle\Server\LimitedHttpServerDriver'