Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
perf(swoole): Improve Dependency Injection configuration
Browse files Browse the repository at this point in the history
Allow to enable or disable drivers basing on configuration settings
By either decorating or not, HttpServerDriverInterface
  • Loading branch information
k911 committed Sep 23, 2018
1 parent 17cde60 commit 6f83e11
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 28 deletions.
Expand Up @@ -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;
Expand Down
25 changes: 20 additions & 5 deletions DependencyInjection/Configuration.php
Expand Up @@ -28,7 +28,7 @@ public function getConfigTreeBuilder(): TreeBuilder

$rootNode
->children()
->arrayNode('server')
->arrayNode('http_server')
->children()
->scalarNode('host')
->defaultValue('127.0.0.1')
Expand All @@ -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')
Expand Down
49 changes: 46 additions & 3 deletions DependencyInjection/SwooleExtension.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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'])
Expand All @@ -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)
Expand Down
20 changes: 1 addition & 19 deletions Resources/config/services.yaml
Expand Up @@ -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':
Expand All @@ -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'

0 comments on commit 6f83e11

Please sign in to comment.