diff --git a/Command/SwooleServerHandle.php b/Command/ServerProfileCommand.php similarity index 81% rename from Command/SwooleServerHandle.php rename to Command/ServerProfileCommand.php index 98c71594..adc7e4d0 100644 --- a/Command/SwooleServerHandle.php +++ b/Command/ServerProfileCommand.php @@ -5,7 +5,7 @@ namespace App\Bundle\SwooleBundle\Command; use App\Bundle\SwooleBundle\Driver\ConsoleDebugDriver; -use App\Bundle\SwooleBundle\Server\Counter; +use App\Bundle\SwooleBundle\Server\AtomicCounter; use App\Bundle\SwooleBundle\Server\ServerUtils; use Swoole\Http\Request; use Swoole\Http\Response; @@ -18,24 +18,26 @@ use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\HttpKernel\KernelInterface; -class SwooleServerHandle extends Command +class ServerProfileCommand extends Command { private $requestCounter; - private $kernel; + private $server; /** * @param KernelInterface $kernel - * @param Counter $counter + * @param Server $server + * @param AtomicCounter $counter * * @throws \Symfony\Component\Console\Exception\LogicException */ - public function __construct(KernelInterface $kernel, Counter $counter) + public function __construct(KernelInterface $kernel, Server $server, AtomicCounter $counter) { parent::__construct(); $this->requestCounter = $counter; $this->kernel = $kernel; + $this->server = $server; } /** @@ -45,12 +47,11 @@ public function __construct(KernelInterface $kernel, Counter $counter) */ protected function configure(): void { - $this->setName('swoole:server:handle') + $this->setName('swoole:server:profile') ->setDescription('Handles specified amount of requests to a local swoole server. Useful for debug or benchmarking.') ->addArgument('requests', InputArgument::REQUIRED, 'Number of requests to handle by the server') ->addOption('host', null, InputOption::VALUE_REQUIRED, 'Host of the server', '127.0.0.1') ->addOption('port', null, InputOption::VALUE_REQUIRED, 'Port of the server', 9501) - ->addOption('enable-profiling', null, InputOption::VALUE_NONE, 'Enables server profiling') ->addOption('enable-static', null, InputOption::VALUE_NONE, 'Enables static files serving'); } @@ -67,22 +68,24 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): void { $io = new SymfonyStyle($input, $output); - $host = $input->getOption('host'); - $port = (int) $input->getOption('port'); - $server = new Server($host, $port, SWOOLE_BASE, SWOOLE_SOCK_TCP); - $profilingEnabled = (bool) $input->getOption('enable-profiling'); + + $host = (string) ($input->getOption('host') ?? $this->server->host); + $port = (int) ($input->getOption('port') ?? $this->server->port); + $this->server->host = $host; + $this->server->port = $port; + $staticFilesServingEnabled = (bool) $input->getOption('enable-static'); - $driver = new ConsoleDebugDriver($this->kernel, $output, $profilingEnabled); + $driver = new ConsoleDebugDriver($this->kernel, $output, true); $requestLimit = (int) $input->getArgument('requests'); if ($staticFilesServingEnabled) { - $server->set([ + $this->server->set([ 'enable_static_handler' => true, 'document_root' => $this->kernel->getRootDir().'/public', ]); } - $server->on('request', function (Request $request, Response $response) use ($driver, $requestLimit, $server, $output) { + $this->server->on('request', function (Request $request, Response $response) use ($driver, $requestLimit, $output) { $driver->handle($request, $response); $this->requestCounter->increment(); @@ -94,7 +97,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void if ($requestLimit === $current) { $output->writeln('Request limit has been hit'); - $server->stop(); + $this->server->stop(); } }); @@ -106,7 +109,6 @@ protected function execute(InputInterface $input, OutputInterface $output): void $rows = [ ['env', $this->kernel->getEnvironment()], ['debug', \var_export($this->kernel->isDebug(), true)], - ['profiling', \var_export($profilingEnabled, true)], ['memory_limit', ServerUtils::formatBytes(ServerUtils::getMaxMemory())], ['trusted_hosts', \implode(', ', $trustedHosts)], ['trusted_proxies', \implode(', ', $trustedProxies)], @@ -119,6 +121,6 @@ protected function execute(InputInterface $input, OutputInterface $output): void $io->newLine(); $io->table(['Configuration', 'Values'], $rows); - $server->start(); + $this->server->start(); } } diff --git a/Command/SwooleServerRun.php b/Command/ServerRunCommand.php similarity index 76% rename from Command/SwooleServerRun.php rename to Command/ServerRunCommand.php index e705f659..bf4d24ec 100644 --- a/Command/SwooleServerRun.php +++ b/Command/ServerRunCommand.php @@ -16,20 +16,23 @@ use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\HttpKernel\KernelInterface; -class SwooleServerRun extends Command +class ServerRunCommand extends Command { private $kernel; + private $server; /** * @param KernelInterface $kernel + * @param Server $server * * @throws \Symfony\Component\Console\Exception\LogicException */ - public function __construct(KernelInterface $kernel) + public function __construct(KernelInterface $kernel, Server $server) { parent::__construct(); $this->kernel = $kernel; + $this->server = $server; } /** @@ -41,9 +44,8 @@ protected function configure(): void { $this->setName('swoole:server:run') ->setDescription('Runs a local swoole server') - ->addOption('host', null, InputOption::VALUE_REQUIRED, 'Host of the server', '127.0.0.1') - ->addOption('port', null, InputOption::VALUE_REQUIRED, 'Port of the server', 9501) - ->addOption('enable-profiling', null, InputOption::VALUE_NONE, 'Enables server profiling') + ->addOption('host', null, InputOption::VALUE_OPTIONAL, 'Host of the server') + ->addOption('port', null, InputOption::VALUE_OPTIONAL, 'Port of the server') ->addOption('enable-static', null, InputOption::VALUE_NONE, 'Enables static files serving'); } @@ -59,21 +61,24 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): void { $io = new SymfonyStyle($input, $output); - $host = $input->getOption('host'); - $port = (int) $input->getOption('port'); - $server = new Server($host, $port, SWOOLE_BASE, SWOOLE_SOCK_TCP); - $profilingEnabled = (bool) $input->getOption('enable-profiling'); + + $host = (string) ($input->getOption('host') ?? $this->server->host); + $port = (int) ($input->getOption('port') ?? $this->server->port); + $this->server->host = $host; + $this->server->port = $port; + +// $profilingEnabled = (bool) $input->getOption('enable-profiling'); $staticFilesServingEnabled = (bool) $input->getOption('enable-static'); - $driver = new ConsoleDebugDriver($this->kernel, $output, $profilingEnabled); + $driver = new ConsoleDebugDriver($this->kernel, $output, false); if ($staticFilesServingEnabled) { - $server->set([ + $this->server->set([ 'enable_static_handler' => true, 'document_root' => $this->kernel->getRootDir().'/public', ]); } - $server->on('request', function (Request $request, Response $response) use ($driver) { + $this->server->on('request', function (Request $request, Response $response) use ($driver) { $driver->handle($request, $response); }); @@ -84,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void $rows = [ ['env', $this->kernel->getEnvironment()], ['debug', \var_export($this->kernel->isDebug(), true)], - ['profiling', \var_export($profilingEnabled, true)], +// ['profiling', \var_export($profilingEnabled, true)], ['memory_limit', ServerUtils::formatBytes(ServerUtils::getMaxMemory())], ['trusted_hosts', \implode(', ', $trustedHosts)], ['trusted_proxies', \implode(', ', $trustedProxies)], @@ -98,6 +103,6 @@ protected function execute(InputInterface $input, OutputInterface $output): void $io->newLine(); $io->table(['Configuration', 'Values'], $rows); - $server->start(); + $this->server->start(); } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 528d180c..b8957486 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -7,7 +7,7 @@ use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; -class Configuration implements ConfigurationInterface +final class Configuration implements ConfigurationInterface { private $builder; diff --git a/DependencyInjection/SwooleExtension.php b/DependencyInjection/SwooleExtension.php index 6625c973..1202131b 100644 --- a/DependencyInjection/SwooleExtension.php +++ b/DependencyInjection/SwooleExtension.php @@ -10,7 +10,7 @@ use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; -class SwooleExtension extends Extension implements PrependExtensionInterface +final class SwooleExtension extends Extension implements PrependExtensionInterface { /** * {@inheritdoc} @@ -31,6 +31,23 @@ public function load(array $configs, ContainerBuilder $container): void $configuration = Configuration::fromTreeBuilder(); $config = $this->processConfiguration($configuration, $configs); + + $this->registerServer($config['server'], $container); + } + + /** + * @param array $config + * @param ContainerBuilder $container + * + * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException + */ + private function registerServer(array $config, ContainerBuilder $container): void + { + $container->getDefinition('swoole.server.http') + ->addArgument($config['host']) + ->addArgument($config['port']) + ->addArgument(SWOOLE_BASE) + ->addArgument(SWOOLE_TCP); } /** diff --git a/Driver/ConsoleDebugDriver.php b/Driver/ConsoleDebugDriver.php index 6c81a0f4..fabee5d5 100644 --- a/Driver/ConsoleDebugDriver.php +++ b/Driver/ConsoleDebugDriver.php @@ -13,7 +13,7 @@ /** * Driver for running Symfony with Swoole as Symfony/Console command. */ -class ConsoleDebugDriver implements ProfilingDriverInterface +final class ConsoleDebugDriver implements ProfilingDriverInterface { private $kernel; private $output; diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index aa1db4a7..bd96a107 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -1,14 +1,25 @@ services: - - # default configuration for services in *this* file _defaults: - # automatically injects dependencies in your services autowire: true - # automatically registers your services as commands, event subscribers, etc. autoconfigure: true - # this means you cannot fetch services directly from the container via $container->get() - # if you need to do this, you can override this setting on individual services public: false - App\Bundle\SwooleBundle\Server\Counter: - factory: [App\Bundle\SwooleBundle\Server\Counter, fromZero] \ No newline at end of file + swoole.counter.atomic: + class: App\Bundle\SwooleBundle\Server\AtomicCounter + factory: [App\Bundle\SwooleBundle\Server\AtomicCounter, fromZero] + + swoole.server.http: + class: Swoole\Http\Server + + swoole.server.run_command: + class: App\Bundle\SwooleBundle\Command\ServerRunCommand + arguments: + $server: '@swoole.server.http' + tags: ['console.command'] + + swoole.server.profile_command: + class: App\Bundle\SwooleBundle\Command\ServerProfileCommand + arguments: + $counter: '@swoole.counter.atomic' + $server: '@swoole.server.http' + tags: ['console.command'] diff --git a/Server/Counter.php b/Server/AtomicCounter.php similarity index 95% rename from Server/Counter.php rename to Server/AtomicCounter.php index 70ce537e..13546e5d 100644 --- a/Server/Counter.php +++ b/Server/AtomicCounter.php @@ -6,7 +6,7 @@ use Swoole\Atomic; -final class Counter +final class AtomicCounter { private $counter; diff --git a/SwooleBundle.php b/SwooleBundle.php index 769a17e7..f6898679 100644 --- a/SwooleBundle.php +++ b/SwooleBundle.php @@ -4,29 +4,8 @@ namespace App\Bundle\SwooleBundle; -use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; -class SwooleBundle extends Bundle +final class SwooleBundle extends Bundle { - /** - * {@inheritdoc} - */ - public function boot(): void - { - } - - /** - * {@inheritdoc} - */ - public function shutdown(): void - { - } - - /** - * {@inheritdoc} - */ - public function build(ContainerBuilder $container): void - { - } }