Skip to content

Commit

Permalink
Merge pull request #5 from pauci/php-8-1
Browse files Browse the repository at this point in the history
Add support for php 8.1, drop support for php 7.4
  • Loading branch information
pauci authored Jan 10, 2022
2 parents 6c83c03 + 18ce850 commit f057be4
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 147 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.1
coverage: none

- name: Get composer cache directory
Expand Down Expand Up @@ -46,7 +46,7 @@ jobs:

strategy:
matrix:
php-versions: ['7.4', '8.0']
php-versions: ['8.0', '8.1']

steps:
- name: Checkout code
Expand Down
16 changes: 10 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
"type": "library",
"license": "MIT",
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
}
},
"require": {
"php": "^7.4 | ^8.0",
"psr/container": "^1.0",
"pauci/cqrs": "^0.5.2"
"php": "~8.0.0||~8.1.0",
"pauci/cqrs": "^0.6.0",
"psr/container": "^1.0||^2.0"
},
"require-dev": {
"doctrine/orm": "^2.8",
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpstan/phpstan": "^0.12.58",
"phpstan/phpstan": "^1.2",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5"
"squizlabs/php_codesniffer": "^3.5",
"symfony/serializer": "^6.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 0 additions & 4 deletions example/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,5 @@
//'instance' => 'my_serializer_alias',
],
],

'event_type_map' => [
'App\Model\Entity\Event\OriginalEvent' => CQRSFactory\Event\RenamedEvent::class,
],
],
];
83 changes: 58 additions & 25 deletions src/AbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

namespace CQRSFactory;

use CQRSFactory\Exception\DomainException;
use Psr\Container\ContainerInterface;

/** @internal */
/**
* @phpstan-template T of object
* @internal
*/
abstract class AbstractFactory
{
private string $configKey;

public function __construct(string $configKey = 'cqrs_default')
/** @internal */
final public function __construct(string $configKey = 'cqrs_default')
{
$this->configKey = $configKey;
}

/**
* @param ContainerInterface $container
* @return mixed
* @phpstan-return T
*/
public function __invoke(ContainerInterface $container)
public function __invoke(ContainerInterface $container): object
{
return $this->createWithConfig($container, $this->configKey);
}
Expand All @@ -35,12 +39,10 @@ public function __invoke(ContainerInterface $container)
* ];
* </code>
*
* @param string $name
* @param array $arguments
* @return mixed
* @phpstan-return T
* @throws Exception\DomainException
*/
public static function __callStatic(string $name, array $arguments)
public static function __callStatic(string $name, array $arguments): object
{
if (!array_key_exists(0, $arguments) || !$arguments[0] instanceof ContainerInterface) {
throw new Exception\InvalidArgumentException(sprintf(
Expand All @@ -49,36 +51,39 @@ public static function __callStatic(string $name, array $arguments)
));
}

/** @phpstan-ignore-next-line */
return (new static($name))($arguments[0]);
}

/**
* Creates a new instance from a specified config.
*
* @param ContainerInterface $container
* @param string $configKey
* @return mixed
* @phpstan-return T
*/
abstract protected function createWithConfig(ContainerInterface $container, string $configKey);
abstract protected function createWithConfig(ContainerInterface $container, string $configKey): object;

/**
* Returns the default config.
*
* @phpstan-return array<string, mixed>
*/
abstract protected function getDefaultConfig(): array;

/**
* Retrieves the config for a specific section.
*
* @phpstan-return array<string, mixed>
*/
protected function retrieveConfig(ContainerInterface $container, string $configKey, string $section): array
{
/** @var array{cqrs?: array<string, array>} $applicationConfig */
$applicationConfig = $container->has('config') ? $container->get('config') : [];
$config = $applicationConfig['cqrs'][$section][$configKey] ?? [];
$sectionConfig = $applicationConfig['cqrs'][$section] ?? [];

if (array_key_exists($configKey, $sectionConfig)) {
return $sectionConfig[$configKey] + $this->getDefaultConfig();
}

return array_merge(
$this->getDefaultConfig(),
$config
);
return $this->getDefaultConfig();
}

/**
Expand All @@ -87,24 +92,52 @@ protected function retrieveConfig(ContainerInterface $container, string $configK
* If the container does not know about the dependency, it is pulled from a fresh factory. This saves the user from
* registering factories which they are not gonna access themself at all, and thus minimized configuration.
*
* @param ContainerInterface $container
* @param string $configKey
* @param string $section
* @param string $factoryClassName
* @return mixed
* @phpstan-template Dependency of object
* @phpstan-param class-string<AbstractFactory<Dependency>> $factoryClassName
* @phpstan-return Dependency
*/
protected function retrieveDependency(
ContainerInterface $container,
string $configKey,
string $section,
string $factoryClassName
) {
): object {
$containerKey = sprintf('cqrs.%s.%s', $section, $configKey);

if ($container->has($containerKey)) {
/** @phpstan-ignore-next-line */
return $container->get($containerKey);
}

return (new $factoryClassName($configKey))($container);
}

/**
* @phpstan-template Service of object
* @phpstan-param class-string<Service> $className
* @phpstan-return Service
*/
protected function retrieveService(
ContainerInterface $container,
array $config,
string $key,
string $className
): object {
$service = $config[$key] ?? null;

if (is_string($service)) {
$service = $container->get($service);
}

if (!$service instanceof $className) {
throw new DomainException(sprintf(
'Service "%s" must be an instance of %s, got %s',
$key,
$className,
get_debug_type($service)
));
}

return $service;
}
}
64 changes: 34 additions & 30 deletions src/CommandBusFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,54 @@
namespace CQRSFactory;

use CQRS\CommandHandling\CommandBusInterface;
use CQRS\CommandHandling\CommandHandlerLocator;
use CQRS\CommandHandling\PsrContainerCommandHandlerLocator;
use CQRS\CommandHandling\SequentialCommandBus;
use CQRS\HandlerResolver\CommandHandlerResolver;
use CQRS\HandlerResolver\ContainerHandlerResolver;
use CQRSFactory\Exception\InvalidArgumentException;
use CQRS\CommandHandling\TransactionManager\TransactionManagerInterface;
use CQRS\EventHandling\Publisher\EventPublisherInterface;
use Psr\Container\ContainerInterface;

/**
* @phpstan-type CommandBusConfig array{
* class: class-string<CommandBusInterface>,
* handlers: array<class-string, string>,
* transaction_manager: string,
* event_publisher: string
* }
* @phpstan-extends AbstractFactory<CommandBusInterface>
*/
class CommandBusFactory extends AbstractFactory
{
/**
* @param ContainerInterface $container
* @param string $configKey
* @return CommandBusInterface
* @throws InvalidArgumentException
*/
protected function createWithConfig(ContainerInterface $container, string $configKey): CommandBusInterface
{
/** @var CommandBusConfig $config */
$config = $this->retrieveConfig($container, $configKey, 'command_bus');

return new $config['class'](
new CommandHandlerLocator(
$config['handlers'],
new ContainerHandlerResolver(
if ($config['class'] === SequentialCommandBus::class) {
return new SequentialCommandBus(
new PsrContainerCommandHandlerLocator(
$container,
$config['handlers']
),
$this->retrieveDependency(
$container,
new CommandHandlerResolver()
$config['transaction_manager'],
'transaction_manager',
TransactionManagerFactory::class
),
$this->retrieveDependency(
$container,
$config['event_publisher'],
'event_publisher',
EventPublisherFactory::class
)
),
$this->retrieveDependency(
$container,
$config['transaction_manager'],
'transaction_manager',
TransactionManagerFactory::class
),
$this->retrieveDependency(
$container,
$config['event_publisher'],
'event_publisher',
EventPublisherFactory::class
)
);
);
}

return new $config['class'];
}

/**
* {@inheritdoc}
* @phpstan-return CommandBusConfig
*/
protected function getDefaultConfig(): array
{
Expand Down
7 changes: 0 additions & 7 deletions src/Event/RenamedEvent.php

This file was deleted.

36 changes: 18 additions & 18 deletions src/EventBusFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@
namespace CQRSFactory;

use CQRS\EventHandling\EventBusInterface;
use CQRS\EventHandling\EventHandlerLocator;
use CQRS\EventHandling\Exception\InvalidArgumentException;
use CQRS\EventHandling\PsrContainerEventHandlerLocator;
use CQRS\EventHandling\SynchronousEventBus;
use CQRS\HandlerResolver\ContainerHandlerResolver;
use CQRS\HandlerResolver\EventHandlerResolver;
use Psr\Container\ContainerInterface;

/**
* @phpstan-type EventBusConfig array{
* class: class-string<EventBusInterface>,
* handlers: array<class-string, array<string|array{handler: string, priority?: int}>>
* }
* @phpstan-extends AbstractFactory<EventBusInterface>
*/
class EventBusFactory extends AbstractFactory
{
/**
* @param ContainerInterface $container
* @param string $configKey
* @return EventBusInterface
* @throws InvalidArgumentException
*/
protected function createWithConfig(ContainerInterface $container, string $configKey): EventBusInterface
{
/** @var EventBusConfig $config */
$config = $this->retrieveConfig($container, $configKey, 'event_bus');

return new $config['class'](
new EventHandlerLocator(
$config['handlers'],
new ContainerHandlerResolver(
if ($config['class'] === SynchronousEventBus::class) {
return new SynchronousEventBus(
new PsrContainerEventHandlerLocator(
$container,
new EventHandlerResolver()
$config['handlers'],
)
)
);
);
}

return new $config['class'];
}

/**
* {@inheritdoc}
* @phpstan-return EventBusConfig
*/
protected function getDefaultConfig(): array
{
Expand Down
Loading

0 comments on commit f057be4

Please sign in to comment.