Skip to content

Commit

Permalink
RoadRunner Lock Plugin Integration (#94)
Browse files Browse the repository at this point in the history
Adds RoadRunner lock plugin support
  • Loading branch information
butschster committed Dec 20, 2023
1 parent f428e6b commit 9206d2a
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 174 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected const LOAD = [
RoadRunnerBridge\TcpBootloader::class, // Optional, if it needs to work with TCP plugin
RoadRunnerBridge\MetricsBootloader::class, // Optional, if it needs to work with metrics plugin
RoadRunnerBridge\LoggerBootloader::class, // Optional, if it needs to work with app-logger plugin
RoadRunnerBridge\LockBootloader::class, // Optional, if it needs to work with lock plugin
RoadRunnerBridge\ScaffolderBootloader::class, // Optional, to generate Centrifugo handlers and TCP services via Scaffolder
RoadRunnerBridge\CommandBootloader::class,
// ...
Expand Down
35 changes: 30 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,57 @@
"license": "MIT",
"homepage": "https://spiral.dev",
"support": {
"issues": "https://github.com/spiral/framework/issues",
"source": "https://github.com/spiral/roadrunner-bridge"
"issues": "https://github.com/spiral/roadrunner-bridge/issues",
"source": "https://github.com/spiral/roadrunner-bridge",
"docs": "https://spiral.dev/docs",
"forum": "https://forum.spiral.dev",
"chat": "https://discord.gg/V6EK4he"
},
"authors": [
{
"name": "Anton Titov (wolfy-j)",
"email": "[email protected]"
},
{
"name": "Pavel Buchnev (butschster)",
"name": "Pavel Butchnev (butschster)",
"email": "[email protected]"
},
{
"name": "Aleksei Gagarin (roxblnfk)",
"email": "[email protected]"
},
{
"name": "Maksim Smakouz (msmakouz)",
"email": "[email protected]"
}
],
"require": {
"php": ">=8.1",
"psr/simple-cache": "^3.0",
"psr/http-factory": "^1.0.2",
"grpc/grpc": "^1.42",
"roadrunner-php/centrifugo": "^2.0",
"spiral/roadrunner-http": "^3.0",
"spiral/roadrunner-grpc": "^3.2",
"spiral/roadrunner-jobs": "^4.0",
"spiral/roadrunner-kv": "^4.0",
"spiral/roadrunner-tcp": "^3.0",
"spiral/roadrunner-metrics": "^3.0",
"roadrunner-php/app-logger": "^1.0",
"spiral/framework": "^3.7",
"roadrunner-php/centrifugo": "^2.0",
"roadrunner-php/lock": "^1.0",
"spiral/serializer": "^3.7",
"spiral/scaffolder": "^3.7"
},
"require-dev": {
"spiral/framework": "^3.7",
"spiral/testing": "^2.6.1",
"phpunit/phpunit": "^10.1",
"vimeo/psalm": "^5.0",
"spiral/nyholm-bridge": "^1.2"
},
"suggest": {
"ext-protobuf": "For better performance, install the protobuf C extension."
},
"autoload": {
"psr-4": {
"Spiral\\RoadRunnerBridge\\": "src"
Expand All @@ -54,6 +69,16 @@
"Spiral\\Tests\\": "tests/src"
}
},
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/roadrunner-server"
}
],
"scripts": {
"test": "vendor/bin/phpunit",
"psalm": "vendor/bin/psalm --no-cache --config=psalm.xml ./src"
},
"config": {
"sort-packages": true,
"allow-plugins": {
Expand Down
43 changes: 24 additions & 19 deletions src/Bootloader/CacheBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,35 @@

final class CacheBootloader extends Bootloader
{
protected const DEPENDENCIES = [
RoadRunnerBootloader::class,
];

protected const SINGLETONS = [
FactoryInterface::class => [self::class, 'initStorageFactory'],
SerializerInterface::class => DefaultSerializer::class,
];

protected const BINDINGS = [
StorageInterface::class => [self::class, 'initDefaultStorage'],
];
public function defineDependencies(): array
{
return [
RoadRunnerBootloader::class,
];
}

private function initStorageFactory(RPCInterface $rpc, SerializerInterface $serializer): FactoryInterface
public function defineSingletons(): array
{
return new Factory($rpc, $serializer);
return [
FactoryInterface::class => static fn (
RPCInterface $rpc,
SerializerInterface $serializer,
): FactoryInterface => new Factory($rpc, $serializer),

SerializerInterface::class => DefaultSerializer::class,
];
}

/**
* @param non-empty-string $driver
*/
private function initDefaultStorage(FactoryInterface $factory, string $driver): StorageInterface
public function defineBindings(): array
{
return $factory->select($driver);
return [
StorageInterface::class =>
/** @param non-empty-string $driver */
static fn (
FactoryInterface $factory,
string $driver,
): StorageInterface => $factory->select($driver),
];
}

public function init(BaseCacheBootloader $cacheBootloader): void
Expand Down
30 changes: 15 additions & 15 deletions src/Bootloader/CentrifugoBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@

final class CentrifugoBootloader extends Bootloader
{
protected const SINGLETONS = [
RegistryInterface::class => [self::class, 'initServiceRegistry'],
Interceptor\RegistryInterface::class => [self::class, 'initInterceptorRegistry'],
CentrifugoWorkerInterface::class => CentrifugoWorker::class,
ErrorHandlerInterface::class => LogErrorHandler::class,
CentrifugoApiInterface::class => RPCCentrifugoApi::class,
];

public function __construct(
private readonly ConfiguratorInterface $config,
) {
}

public function defineSingletons(): array
{
return [
RegistryInterface::class => [self::class, 'initServiceRegistry'],
Interceptor\RegistryInterface::class => [self::class, 'initInterceptorRegistry'],
CentrifugoWorkerInterface::class => CentrifugoWorker::class,
ErrorHandlerInterface::class => LogErrorHandler::class,
CentrifugoApiInterface::class => RPCCentrifugoApi::class,
];
}

private function initConfig(): void
{
$this->config->setDefaults(
Expand All @@ -49,17 +52,14 @@ private function initConfig(): void
);
}

public function init(
BroadcastingBootloader $broadcasting,
): void {
public function init(BroadcastingBootloader $broadcasting): void
{
$this->initConfig();
$broadcasting->registerDriverAlias(Broadcast::class, 'centrifugo');
}

public function boot(
AbstractKernel $kernel,
Dispatcher $dispatcher,
): void {
public function boot(AbstractKernel $kernel, Dispatcher $dispatcher): void
{
$kernel->addDispatcher($dispatcher);
}

Expand Down
42 changes: 24 additions & 18 deletions src/Bootloader/GRPCBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,29 @@

final class GRPCBootloader extends Bootloader
{
protected const DEPENDENCIES = [
RoadRunnerBootloader::class,
];

protected const SINGLETONS = [
Server::class => Server::class,
InvokerInterface::class => [self::class, 'initInvoker'],
LocatorInterface::class => ServiceLocator::class,
ProtoFilesRepositoryInterface::class => [self::class, 'initProtoFilesRepository'],
GeneratorRegistryInterface::class => [self::class, 'initGeneratorRegistry'],
];

public function __construct(
private readonly ConfiguratorInterface $config
private readonly ConfiguratorInterface $config,
) {
}

public function defineDependencies(): array
{
return [
RoadRunnerBootloader::class,
];
}

public function defineSingletons(): array
{
return [
Server::class => Server::class,
InvokerInterface::class => [self::class, 'initInvoker'],
LocatorInterface::class => ServiceLocator::class,
ProtoFilesRepositoryInterface::class => [self::class, 'initProtoFilesRepository'],
GeneratorRegistryInterface::class => [self::class, 'initGeneratorRegistry'],
];
}

public function init(): void
{
$this->initGrpcConfig();
Expand Down Expand Up @@ -79,7 +85,7 @@ private function initGrpcConfig(): void
ConfigGenerator::class,
BootloaderGenerator::class,
],
]
],
);
}

Expand All @@ -90,7 +96,7 @@ public function addInterceptor(string|CoreInterceptorInterface|Autowire $interce
{
$this->config->modify(
GRPCConfig::CONFIG,
new Append('interceptors', null, $interceptor)
new Append('interceptors', null, $interceptor),
);
}

Expand All @@ -106,10 +112,10 @@ private function initInvoker(
GRPCConfig $config,
ContainerInterface $container,
FactoryInterface $factory,
BaseInvoker $invoker
BaseInvoker $invoker,
): InvokerInterface {
$core = new InterceptableCore(
new InvokerCore($invoker)
new InvokerCore($invoker),
);

foreach ($config->getInterceptors() as $interceptor) {
Expand All @@ -131,7 +137,7 @@ private function initProtoFilesRepository(GRPCConfig $config): ProtoFilesReposit
private function initGeneratorRegistry(
GRPCConfig $config,
ContainerInterface $container,
FactoryInterface $factory
FactoryInterface $factory,
): GeneratorRegistryInterface {
$registry = new GeneratorRegistry();
foreach ($config->getGenerators() as $generator) {
Expand Down
39 changes: 21 additions & 18 deletions src/Bootloader/HttpBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,31 @@

final class HttpBootloader extends Bootloader
{
protected const DEPENDENCIES = [
RoadRunnerBootloader::class,
BaseHttpBootloader::class,
];
public function defineDependencies(): array
{
return [
RoadRunnerBootloader::class,
BaseHttpBootloader::class,
];
}

protected const SINGLETONS = [
ErrorHandlerInterface::class => LogErrorHandler::class,
PSR7Worker::class => PSR7WorkerInterface::class,
PSR7WorkerInterface::class => [self::class, 'initPSR7Worker'],
];
public function defineSingletons(): array
{
return [
ErrorHandlerInterface::class => LogErrorHandler::class,
PSR7Worker::class => PSR7WorkerInterface::class,

PSR7WorkerInterface::class => static fn (
WorkerInterface $worker,
ServerRequestFactoryInterface $requests,
StreamFactoryInterface $streams,
UploadedFileFactoryInterface $uploads,
): PSR7WorkerInterface => new PSR7Worker($worker, $requests, $streams, $uploads),
];
}

public function boot(KernelInterface $kernel, FactoryInterface $factory): void
{
$kernel->addDispatcher($factory->make(Dispatcher::class));
}

private function initPSR7Worker(
WorkerInterface $worker,
ServerRequestFactoryInterface $requests,
StreamFactoryInterface $streams,
UploadedFileFactoryInterface $uploads,
): PSR7WorkerInterface {
return new PSR7Worker($worker, $requests, $streams, $uploads);
}
}
26 changes: 26 additions & 0 deletions src/Bootloader/LockBootloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerBridge\Bootloader;

use RoadRunner\Lock\Lock;
use RoadRunner\Lock\LockInterface;
use Spiral\Boot\Bootloader\Bootloader;

final class LockBootloader extends Bootloader
{
public function defineDependencies(): array
{
return [
RoadRunnerBootloader::class,
];
}

public function defineSingletons(): array
{
return [
LockInterface::class => Lock::class,
];
}
}
Loading

0 comments on commit 9206d2a

Please sign in to comment.