Skip to content

Commit

Permalink
#20 Made tests green
Browse files Browse the repository at this point in the history
  • Loading branch information
awd-studio committed Jul 4, 2023
1 parent 3404525 commit c87f903
Show file tree
Hide file tree
Showing 49 changed files with 387 additions and 1,343 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('somedir');
->exclude('tests');

$config = new PhpCsFixer\Config();

Expand Down
46 changes: 25 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ There are a few predefined buses:

use AwdStudio\Bus\Handler\InMemoryHandlerLocator;
use AwdStudio\Bus\Middleware\CallbackMiddlewareChain;
use AwdStudio\Command\MiddlewareCommandBus;
use AwdStudio\Command\SimpleCommandBus;

class MyCommand {
// Messages might be any of PHP class.
Expand All @@ -245,7 +245,7 @@ $middleware->add(MyCommand::class, static function (MyCommand $command, callable
});
$chain = new CallbackMiddlewareChain($middleware);

$bus = new MiddlewareCommandBus($handlers, $chain);
$bus = new SimpleCommandBus($handlers, $chain);

$bus->handle(new MyCommand());
```
Expand All @@ -258,7 +258,7 @@ $bus->handle(new MyCommand());

use AwdStudio\Bus\Handler\InMemoryHandlerLocator;
use AwdStudio\Bus\Middleware\CallbackMiddlewareChain;
use AwdStudio\Query\MiddlewareQueryBus;
use AwdStudio\Query\SimpleQueryBus;

class MyQuery {
// Messages might be any of PHP class.
Expand All @@ -283,7 +283,7 @@ $middleware->add(MyQuery::class, static function (MyQuery $query, callable $next
});
$chain = new CallbackMiddlewareChain($middleware);

$bus = new MiddlewareQueryBus($handlers, $chain);
$bus = new SimpleQueryBus($handlers, $chain);

$result = $bus->handle(new MyQuery());

Expand All @@ -299,7 +299,7 @@ $result = $bus->handle(new MyQuery());

use AwdStudio\Bus\Handler\InMemoryHandlerLocator;
use AwdStudio\Bus\Middleware\CallbackMiddlewareChain;
use AwdStudio\Event\MiddlewareEventBus;
use AwdStudio\Event\SimpleEventBus;

class MyEvent {
// Messages might be any of PHP class.
Expand All @@ -323,7 +323,7 @@ $middleware->add(MyEvent::class, static function (MyEvent $event, callable $next
});
$chain = new CallbackMiddlewareChain($middleware);

$bus = new MiddlewareEventBus($subscribers, $chain);
$bus = new SimpleEventBus($subscribers, $chain);

$bus->handle(new MyEvent());

Expand All @@ -335,11 +335,12 @@ $bus->handle(new MyEvent());
## Subscribe on parents

The library allows subscribing not only on a certain class, but on all of its parents - either a parent or an implementation from any level.

```php
<?php

use AwdStudio\Bus\Handler\ParentsAwareHandlerRegistry;
use AwdStudio\Bus\Handler\PsrContainerHandlerRegistry;
use AwdStudio\Bus\Handler\ParentsAwareClassHandlerRegistry;
use AwdStudio\Bus\Handler\PsrContainerClassHandlerRegistry;
use Psr\Container\ContainerInterface;

class MyPsr11Container implements ContainerInterface {}
Expand All @@ -358,7 +359,7 @@ class Handler
public function __invoke(Baz $message): void {}
}

$handlerRegistry = new ParentsAwareHandlerRegistry(new PsrContainerHandlerRegistry(new MyPsr11Container()));
$handlerRegistry = new ParentsAwareClassHandlerRegistry(new PsrContainerClassHandlerRegistry(new MyPsr11Container()));
```


Expand All @@ -370,10 +371,11 @@ And, the library provides ability to use those containers as service locators fo

To use it, there is a decorator for a handler-locator, that can be used for registering handlers with just FCQN.
As a dependency it accepts any of `Psr\Container\ContainerInterface`, that supposed to resolve handlers.

```php
<?php

use AwdStudio\Bus\Handler\PsrContainerHandlerRegistry;
use AwdStudio\Bus\Handler\PsrContainerClassHandlerRegistry;
use AwdStudio\Bus\SimpleBus;
use Psr\Container\ContainerInterface;

Expand Down Expand Up @@ -406,7 +408,7 @@ class StdClassHandler
}

$serviceLocator = new MyPsr11Container([StdClassHandler::class]);
$handlerRegistry = new PsrContainerHandlerRegistry($serviceLocator);
$handlerRegistry = new PsrContainerClassHandlerRegistry($serviceLocator);

// To assign a handler use a defined method:
$handlerRegistry->register(\stdClass::class, StdClassHandler::class);
Expand All @@ -429,14 +431,15 @@ $bus->handle(new \stdClass()); // The handler will be executed
### Auto-register services

There is even a decorator to subscribe callbacks automatically, by their signature, that supposed to contain a type-hint as the very first parameter.

```php
<?php

use AwdStudio\Bus\Handler\AutoRegisterHandlersRegistry;
use AwdStudio\Bus\Handler\PsrContainerHandlerRegistry;
use AwdStudio\Bus\Handler\AutoRegisterHandlersRegistryClass;
use AwdStudio\Bus\Handler\PsrContainerClassHandlerRegistry;

$psrRegistry = new PsrContainerHandlerRegistry(new MyPsr11Container());
$autoRegistry = new AutoRegisterHandlersRegistry($psrRegistry);
$psrRegistry = new PsrContainerClassHandlerRegistry(new MyPsr11Container());
$autoRegistry = new AutoRegisterHandlersRegistryClass($psrRegistry);

// Now, you can add a callback to assign a handler automatically.
// Just be sure, that it has a correct type-hint of a message that it handles.
Expand All @@ -461,18 +464,19 @@ $autoRegistry->autoRegister(Handler::class);

If you don't like invokable services, or somehow need to use handlers that handle via different methods - this is not a problem at all.

Just pass the name of a method while registering:
Just pass the name of a method while registering:

```php
<?php

use AwdStudio\Bus\Handler\PsrContainerHandlerRegistry;
use AwdStudio\Bus\Handler\PsrContainerClassHandlerRegistry;

class Handler {
public function handle(\stdClass $message): void { }
}

// Any registry can manage with it out of the box
$psrRegistry = new PsrContainerHandlerRegistry(new MyPsr11Container());
$psrRegistry = new PsrContainerClassHandlerRegistry(new MyPsr11Container());
$psrRegistry->register(\stdClass::class, Handler::class, 'handle');
// The 3rd argument tells which method is in charge of handling.
```
Expand Down Expand Up @@ -527,14 +531,14 @@ The SimpleBus provides you an ability to handle messages with only handles.
```php
<?php

use AwdStudio\Bus\MiddlewareBus;
use AwdStudio\Bus\SimpleBus;

class MyBus extends MiddlewareBus
class MyBus extends SimpleBus
{
public function handle(object $message): string
{
$result = '';
foreach ($this->buildChains($message) as $chain) {
foreach ($this->handleMessage($message) as $chain) {
$result .= $chain();
}

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
"phpunit/phpunit": "^9.0",
"dg/bypass-finals": "^1.1",
"php-coveralls/php-coveralls": "^2.1",
"vimeo/psalm": "^3.4",
"vimeo/psalm": "^5.0",
"phpunit/php-code-coverage": "^9.2",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3"
"phpstan/phpstan-phpunit": "^1.3",
"psalm/plugin-symfony": "^5.0",
"psalm/plugin-phpunit": "^0.18.4"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@ parameters:
level: max
paths:
- src

ignoreErrors:
- '#PHPDoc tag @template [a-zA-Z0-9]+ for [a-zA-Z0-9\\_\s]+ with bound type callable is not supported\.#'
- '#PHPDoc tag @param for parameter [a-zA-Z0-9\$\_]+ with type [a-zA-Z0-9]+ is not subtype of native type callable\.#'
22 changes: 22 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedVariablesAndParams="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="src"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin"/>
</plugins>
</psalm>
9 changes: 9 additions & 0 deletions psalm.xml.dist.old
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
>
<projectFiles>
<directory name="src" />
</projectFiles>
</psalm>
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Bus/Exception/NoHandlerDefined.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ final class NoHandlerDefined extends BusException
{
public function __construct(object $message)
{
parent::__construct(\sprintf('No handlers for a message "%s"', \get_class($message)), 1, null);
parent::__construct(\sprintf('No handlers for a message "%s"', $message::class), 1, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,12 @@
use AwdStudio\Bus\Reader\MessageIdResolver;
use AwdStudio\Bus\Reader\ReflectionMessageIdReader;

/**
* @implements HandlerRegistry<callable(object $message, mixed ...$extraParams): mixed>
*/
final class AutoRegisterHandlersRegistry implements HandlerRegistry
final readonly class AutoClassHandlerRegistry implements ClassHandlerRegistry
{
/**
* @var \AwdStudio\Bus\Handler\HandlerRegistry
*
* @psalm-var HandlerRegistry<callable(object $message, mixed ...$extraParams): mixed>
*
* @phpstan-var HandlerRegistry<callable(object $message, mixed ...$extraParams): mixed>
*/
private $parent;

/** @var \AwdStudio\Bus\Reader\MessageIdResolver */
private $reader;

/**
* @param \AwdStudio\Bus\Handler\HandlerRegistry $parent
*
* @psalm-param HandlerRegistry<callable(object $message, mixed ...$extraParams): mixed> $parent
*
* @phpstan-param HandlerRegistry<callable(object $message, mixed ...$extraParams): mixed> $parent
*/
public function __construct(HandlerRegistry $parent, MessageIdResolver $reader = null)
{
$this->parent = $parent;
$this->reader = $reader ?? new ReflectionMessageIdReader();
public function __construct(
private ClassHandlerRegistry $parent,
private MessageIdResolver $reader = new ReflectionMessageIdReader()
) {
}

public function add(string $messageId, callable $handler): void
Expand All @@ -53,17 +31,17 @@ public function get(string $messageId): \Iterator
return $this->parent->get($messageId);
}

public function register(string $messageId, string $handlerId, string $handlerMethod = '__invoke'): void
public function register(string $messageId, string $handlerClass, string $handlerMethod = '__invoke'): void
{
$this->parent->register($messageId, $handlerId);
$this->parent->register($messageId, $handlerClass);
}

/**
* Registers a callback as a handler automatically, by the signature of the method in a service.
*
* @psalm-param callable(object $message, mixed ...$extraParams): mixed $handler
* @psalm-param callable(object $message): mixed $handler
*
* @phpstan-param callable(object $message, mixed ...$extraParams): mixed $handler
* @phpstan-param callable(object $message): mixed $handler
*/
public function autoAdd(callable $handler): void
{
Expand Down
21 changes: 21 additions & 0 deletions src/Bus/Handler/ClassHandlerRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace AwdStudio\Bus\Handler;

use AwdStudio\Bus\HandlerLocator;

interface ClassHandlerRegistry extends HandlerLocator
{
/**
* Registers a handler from a PSR-container as a message handler.
*
* @param class-string $messageId a message on which the handler subscribes on
* @param class-string $handlerClass an ID of a service that represents a handler in a container
* @param string $handlerMethod the name of a method that handles a message
*
* @throws \AwdStudio\Bus\Exception\InvalidHandler
*/
public function register(string $messageId, string $handlerClass, string $handlerMethod = '__invoke'): void;
}
32 changes: 0 additions & 32 deletions src/Bus/Handler/HandlerRegistry.php

This file was deleted.

21 changes: 4 additions & 17 deletions src/Bus/Handler/InMemoryHandlerLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,12 @@

use AwdStudio\Bus\HandlerLocator;

/**
* @psalm-external-mutation-free
*
* @implements HandlerLocator<callable(object $message, mixed ...$extraParams): mixed>
*/
final class InMemoryHandlerLocator implements HandlerLocator
{
/**
* @var array
*
* @psalm-var array<class-string, list<callable(object $message, mixed ...$extraParams): mixed>>
*
* @phpstan-var array<class-string, list<callable(object $message, mixed ...$extraParams): mixed>>
*/
private $handlers;

public function __construct()
{
$this->handlers = [];
public function __construct(
/** @phpstan-var array<class-string, list<callable>> */
private array $handlers = []
) {
}

public function add(string $messageId, callable $handler): void
Expand Down
Loading

0 comments on commit c87f903

Please sign in to comment.