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

Commit

Permalink
Use DI to wire swoole bundle commands
Browse files Browse the repository at this point in the history
  • Loading branch information
k911 committed May 20, 2018
1 parent b11a078 commit 42b0126
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 65 deletions.
36 changes: 19 additions & 17 deletions Command/SwooleServerHandle.php → Command/ServerProfileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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');
}

Expand All @@ -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();
Expand All @@ -94,7 +97,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void

if ($requestLimit === $current) {
$output->writeln('<comment>Request limit has been hit</comment>');
$server->stop();
$this->server->stop();
}
});

Expand All @@ -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)],
Expand All @@ -119,6 +121,6 @@ protected function execute(InputInterface $input, OutputInterface $output): void
$io->newLine();
$io->table(['Configuration', 'Values'], $rows);

$server->start();
$this->server->start();
}
}
33 changes: 19 additions & 14 deletions Command/SwooleServerRun.php → Command/ServerRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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');
}

Expand All @@ -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);
});

Expand All @@ -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)],
Expand All @@ -98,6 +103,6 @@ protected function execute(InputInterface $input, OutputInterface $output): void
$io->newLine();
$io->table(['Configuration', 'Values'], $rows);

$server->start();
$this->server->start();
}
}
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
19 changes: 18 additions & 1 deletion DependencyInjection/SwooleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Driver/ConsoleDebugDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 19 additions & 8 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -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]
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']
2 changes: 1 addition & 1 deletion Server/Counter.php → Server/AtomicCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Swoole\Atomic;

final class Counter
final class AtomicCounter
{
private $counter;

Expand Down
23 changes: 1 addition & 22 deletions SwooleBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
}

0 comments on commit 42b0126

Please sign in to comment.