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

Commit

Permalink
fix(boot-manager): Don't boot not bootable objects
Browse files Browse the repository at this point in the history
- Service decorators `K911\Swoole\Server\RequestHandler\RequestHandlerInterface` were
autoconfigured with `swoole_bundle.bootable_service` tag, due to base
service being bootable
- Move most logic of boot manager to happen at compile time

Fixes #19
  • Loading branch information
k911 committed Oct 24, 2018
1 parent afc0bfc commit 8ad97a2
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->addDefaultsIfNotSet()
->children()
->booleanNode('debug_handler')
->defaultFalse()
->treatNullLike(false)
->defaultNull()
->end()
->booleanNode('trust_all_proxies_handler')
->defaultFalse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public function load(array $configs, ContainerBuilder $container): void
*/
private function registerHttpServer(array $config, ContainerBuilder $container): void
{
if (!empty($config['services'])) {
$this->registerHttpServerServices($config['services'], $container);
}
$this->registerHttpServerServices($config['services'], $container);

$container->setParameter('swoole.http_server.trusted_proxies', $config['trusted_proxies']);
$container->setParameter('swoole.http_server.trusted_hosts', $config['trusted_hosts']);
Expand Down Expand Up @@ -196,16 +194,16 @@ private function registerHttpServerServices(array $config, ContainerBuilder $con
$container->register(EntityManagerHandler::class)
->addArgument(new Reference(EntityManagerHandler::class.'.inner'))
->setAutowired(true)
->setAutoconfigured(true)
->setAutoconfigured(false)
->setPublic(false)
->setDecoratedService(RequestHandlerInterface::class, null, -20);
}

if ($config['debug_handler'] || (null === $config['debug_handler'] && $container->getParameter('kernel.debug'))) {
if ($config['debug_handler'] || (null === $config['debug_handler'] && $this->isDebug($container))) {
$container->register(DebugHttpKernelRequestHandler::class)
->addArgument(new Reference(DebugHttpKernelRequestHandler::class.'.inner'))
->setAutowired(true)
->setAutoconfigured(true)
->setAutoconfigured(false)
->setPublic(false)
->setDecoratedService(RequestHandlerInterface::class, null, -50);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Bridge/Symfony/Bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ services:

'K911\Swoole\Server\RequestHandler\RequestHandlerInterface':
class: K911\Swoole\Bridge\Symfony\HttpKernel\HttpKernelRequestHandler
autoconfigure: false

'K911\Swoole\Server\RequestHandler\LimitedRequestHandler':

'K911\Swoole\Server\LifecycleHandler\SigIntHandler':

'K911\Swoole\Server\Runtime\CallableBootManagerFactory':

'K911\Swoole\Server\Runtime\BootableInterface':
class: 'K911\Swoole\Server\Runtime\BootManager'
class: 'K911\Swoole\Server\Runtime\CallableBootManager'
factory: 'K911\Swoole\Server\Runtime\CallableBootManagerFactory:make'
arguments: [!tagged 'swoole_bundle.bootable_service']
autoconfigure: false

Expand Down
14 changes: 14 additions & 0 deletions src/Component/GeneratedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ public function map(callable $func): self
return new self($this->each($func));
}

private function filterItems(callable $func): Generator
{
foreach ($this->getIterator() as $item) {
if ($func($item)) {
yield $item;
}
}
}

public function filter(callable $func): self
{
return new self($this->filterItems($func));
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
/**
* Chain of services implementing BootableInterface.
*/
final class BootManager implements BootableInterface
final class CallableBootManager implements BootableInterface
{
private $booted;
private $services;
private $bootables;

/**
* @param iterable<BootableInterface> $services
* @param bool $booted
* @param iterable<callable> $bootables
* @param bool $booted
*/
public function __construct(iterable $services, bool $booted = false)
public function __construct(iterable $bootables, bool $booted = false)
{
$this->services = $services;
$this->bootables = $bootables;
$this->booted = $booted;
}

Expand All @@ -36,15 +36,9 @@ public function boot(array $runtimeConfiguration = []): void
Assertion::false($this->booted, 'Boot method has already been called. Cannot boot services multiple times.');
$this->booted = true;

$booted = [];

/** @var BootableInterface $service */
foreach ($this->services as $service) {
$id = \spl_object_id($service);
if (!isset($booted[$id])) {
$service->boot($runtimeConfiguration);
$booted[$id] = true;
}
/** @var callable $bootable */
foreach ($this->bootables as $bootable) {
$bootable($runtimeConfiguration);
}
}
}
34 changes: 34 additions & 0 deletions src/Server/Runtime/CallableBootManagerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\Runtime;

use Assert\Assertion;
use K911\Swoole\Component\GeneratedCollection;

final class CallableBootManagerFactory
{
public function make(iterable $bootableCollection, BootableInterface ...$bootables): CallableBootManager
{
$objectRegistry = [];
$isAlreadyRegistered = function (int $id) use (&$objectRegistry): bool {
$result = !isset($objectRegistry[$id]);
$objectRegistry[$id] = true;

return $result;
};

return new CallableBootManager(
(new GeneratedCollection($bootableCollection, ...$bootables))
->filter(function ($bootable) use ($isAlreadyRegistered): bool {
Assertion::isInstanceOf($bootable, BootableInterface::class);

return $isAlreadyRegistered(\spl_object_id($bootable));
})
->map(function (BootableInterface $bootable): callable {
return [$bootable, 'boot'];
})
);
}
}

0 comments on commit 8ad97a2

Please sign in to comment.