Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PreInvoke and PostInvoke events #29

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@
"ext-json": "*",
"google/common-protos": "^3.1|^4.0",
"google/protobuf": "^3.7",
"spiral/roadrunner-worker": "^3.0",
"psr/event-dispatcher": "^1.0",
"spiral/goridge": "^4.0",
"spiral/roadrunner": "^2023.1 || ^2024.1"
"spiral/roadrunner": "^2023.1 || ^2024.1",
"spiral/roadrunner-worker": "^3.0"
},
"require-dev": {
"jetbrains/phpstorm-attributes": "^1.0",
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<InvalidAttribute errorLevel="suppress" />
<UndefinedAttributeClass errorLevel="suppress" />
<DocblockTypeContradiction errorLevel="suppress" />
<MissingClassConstType errorLevel="suppress" />
</issueHandlers>
<projectFiles>
<directory name="src" />
Expand Down
32 changes: 32 additions & 0 deletions src/Event/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\GRPC\Event;

use Psr\EventDispatcher\StoppableEventInterface;

class Event implements StoppableEventInterface
{
private bool $propagationStopped = false;

/**
* {@inheritdoc}
*/
public function isPropagationStopped(): bool
{
return $this->propagationStopped;
}

/**
* Stops the propagation of the event to further event listeners.
*
* If multiple event listeners are connected to the same event, no
* further event listener will be triggered once any trigger calls
* stopPropagation().
*/
public function stopPropagation(): void
{
$this->propagationStopped = true;
}
}
20 changes: 20 additions & 0 deletions src/Event/PostInvokeEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\GRPC\Event;

use Google\Protobuf\Internal\Message;

final class PostInvokeEvent extends Event
{
public function __construct(
private readonly Message $output,
) {
}

public function getOutput(): Message
{
return $this->output;
}
}
27 changes: 27 additions & 0 deletions src/Event/PreInvokeEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\GRPC\Event;

use Google\Protobuf\Internal\Message;
use Spiral\RoadRunner\GRPC\ContextInterface;

final class PreInvokeEvent extends Event
{
public function __construct(
private readonly ContextInterface $ctx,
private readonly Message $input,
) {
}

public function getContext(): ContextInterface
{
return $this->ctx;
}

public function getInput(): Message
{
return $this->input;
}
}
12 changes: 12 additions & 0 deletions src/Invoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Spiral\RoadRunner\GRPC;

use Google\Protobuf\Internal\Message;
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\RoadRunner\GRPC\Event\PostInvokeEvent;
use Spiral\RoadRunner\GRPC\Event\PreInvokeEvent;
use Spiral\RoadRunner\GRPC\Exception\InvokeException;

final class Invoker implements InvokerInterface
Expand All @@ -17,6 +20,11 @@ final class Invoker implements InvokerInterface
'Method %s input type must be an instance of %s, ' .
'but the input is type of %s';

public function __construct(
private readonly ?EventDispatcherInterface $dispatcher = null
) {
}

public function invoke(
ServiceInterface $service,
Method $method,
Expand All @@ -28,11 +36,15 @@ public function invoke(

$input = $input instanceof Message ? $input : $this->makeInput($method, $input);

$this->dispatcher?->dispatch(new PreInvokeEvent($ctx, $input));

/** @var Message $message */
$message = $callable($ctx, $input);

\assert($this->assertResultType($method, $message));

$this->dispatcher?->dispatch(new PostInvokeEvent($message));

try {
return $message->serializeToString();
} catch (\Throwable $e) {
Expand Down
Loading