Skip to content

Commit

Permalink
Merge pull request #10 from teamsquad-io/9-simplify-goassistedconsume…
Browse files Browse the repository at this point in the history
…r-and-autoloadereventmapgenerator

9 simplify goassistedconsumer and autoloadereventmapgenerator
  • Loading branch information
tomasvts committed Oct 11, 2022
2 parents 1655a21 + 7253429 commit 63a97c8
Show file tree
Hide file tree
Showing 13 changed files with 376 additions and 115 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/.phpbench/
*.cache
tests/Unit/Infrastructure/eventMapFile.php
auto_*.php
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
"psalm": "./vendor/bin/psalm",
"phpstan": "./vendor/bin/phpstan",
"csfix": "./vendor/bin/php-cs-fixer fix",
"csrun": "./vendor/bin/php-cs-fixer fix --dry-run || ./vendor/bin/php-cs-fixer fix"
"csrun": "./vendor/bin/php-cs-fixer fix --dry-run || ./vendor/bin/php-cs-fixer fix",
"generate-consumer-config": [
"composer dump-autoload -o",
"TeamSquad\\Tests\\Unit\\Interfaces\\SampleConfigGeneratorController::generateConsumerConfig"
]
},
"config": {
"platform": {
Expand Down
24 changes: 12 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ parameters:
- stubs/Config.stub
paths:
- %currentWorkingDirectory%/src/
excludePaths:
- %currentWorkingDirectory%/tests/
2 changes: 1 addition & 1 deletion src/Domain/Exception/FileNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class FileNotFound extends Exception
{
public function __construct(string $file)
{
parent::__construct("Configuration path file not found: {$file}");
parent::__construct(sprintf('Configuration path file not found: %s', $file));
}
}
97 changes: 81 additions & 16 deletions src/Infrastructure/AutoloadConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,129 @@

namespace TeamSquad\EventBus\Infrastructure;

use TeamSquad\EventBus\Domain\Exception\FileNotFound;
use TeamSquad\EventBus\Domain\Exception\InvalidArguments;

use function is_array;
use function is_string;

class AutoloadConfig
{
/** @var string List of events namespaces or paths to be included in the event bus */
public const WHITE_LIST_CONFIG_KEY = 'white_list';
/** @var string List of events namespaces or paths to be excluded from the event bus */
public const BLACK_LIST_CONFIG_KEY = 'black_list';
/** @var string Prefix to be added to the consumer that will be created by Amqp2fcgi */
public const CONSUMER_QUEUE_LISTEN_NAME_KEY = 'consumer_queue_listen_name';
/** @var string Exchange name */
public const EVENT_BUS_EXCHANGE_NAME_KEY = 'event_bus_exchange_name';
/** @var string Path to the configuration file */
public const CONFIGURATION_PATH_KEY = 'configuration_path';

/** @var array<string, array<string>|string> */
private array $config;

/**
* @param array<string, array<string>|string> $configuration
*
* @psalm-param array{
* consumer_queue_listen_name?: string,
* event_bus_exchange_name?: string,
* configuration_path?: string,
* white_list?: array<string>|string,
* black_list?: array<string>|string
* } $configuration
*
* @throws InvalidArguments
*/
public function __construct(array $configuration = [])
public function __construct(array $configuration)
{
$mandatoryKeys = [
self::CONSUMER_QUEUE_LISTEN_NAME_KEY,
self::EVENT_BUS_EXCHANGE_NAME_KEY,
self::CONFIGURATION_PATH_KEY,
];
foreach ($mandatoryKeys as $mandatoryKey) {
if (!isset($configuration[$mandatoryKey])) {
throw new InvalidArguments(sprintf("Configuration key '%s' is not set", $mandatoryKey));
}
}

$this->config = $configuration;
}

/**
* Prefix to be added to the consumer that will be created by Amqp2fcgi
*
* @return string
*/
public function consumerQueueExchangeListenName(): string
{
/** @var string $exchangeName */
$exchangeName = $this->config['consumer_queue_listen_name'] ?? 'my_company.event.listen';
$exchangeName = $this->config[self::CONSUMER_QUEUE_LISTEN_NAME_KEY];
return $exchangeName;
}

/**
* Exchange name
*
* @return string
*/
public function eventBusExchangeName(): string
{
/** @var string $exchangeName */
$exchangeName = $this->config['event_bus_exchange_name'] ?? 'my_company.event_bus';
$exchangeName = $this->config[self::EVENT_BUS_EXCHANGE_NAME_KEY] ?? 'my_company.event_bus';
return $exchangeName;
}

/**
* @throws FileNotFound
* Path to the configuration file
*
* @return string
*/
public function configurationPath(): string
{
/** @var string $configurationPath */
$configurationPath = $this->config['configuration_path'] ?? null;
if (!$configurationPath) {
throw new FileNotFound('Configuration "configuration_path" is not set');
}
$configurationPath = $this->config[self::CONFIGURATION_PATH_KEY];
return $configurationPath;
}

/**
* Check if the class is included in the white list
*
* @param string $className
*
* @return bool
*/
public function isIncludedInWhiteList(string $className): bool
{
/** @var null|string|array<array-key, string> $includedClassNames */
$includedClassNames = $this->config[self::WHITE_LIST_CONFIG_KEY] ?? [];
if (empty($includedClassNames)) {
return true;
}
return $this->isClassNameInConfig($includedClassNames, $className);
return $this->isClassNameInList($includedClassNames, $className);
}

/**
* Check if the class is included in the black list
*
* @param string $className
*
* @return bool
*/
public function isIncludedInBlackList(string $className): bool
{
/** @var null|string|array<array-key, string> $excludedClassNames */
$excludedClassNames = $this->config[self::BLACK_LIST_CONFIG_KEY] ?? [];
if (empty($excludedClassNames)) {
return false;
}
return $this->isClassNameInConfig($excludedClassNames, $className);
return $this->isClassNameInList($excludedClassNames, $className);
}

/**
* List of events namespaces or paths to be included in the event bus
*
* @return array<array-key, string>
*/
public function getWhiteList(): array
Expand All @@ -89,6 +140,8 @@ public function getWhiteList(): array
}

/**
* List of events namespaces or paths to be excluded from the event bus
*
* @return array<array-key, string>
*/
public function getBlackList(): array
Expand All @@ -101,16 +154,28 @@ public function getBlackList(): array
return $arr;
}

public function hasWhiteList(): bool
{
return !empty($this->config[self::WHITE_LIST_CONFIG_KEY]);
}

public function hasBlackList(): bool
{
return !empty($this->config[self::BLACK_LIST_CONFIG_KEY]);
}

/**
* @param string|array<array-key, string> $config
* Check if the class is included in the given list
*
* @param string|array<array-key, string> $list
* @param string $className
*
* @return bool
*/
private function isClassNameInConfig($config, string $className): bool
private function isClassNameInList($list, string $className): bool
{
if (is_array($config)) {
foreach ($config as $includedClassName) {
if (is_array($list)) {
foreach ($list as $includedClassName) {
if (stripos($className, $includedClassName) !== false) {
return true;
}
Expand All @@ -119,6 +184,6 @@ private function isClassNameInConfig($config, string $className): bool
return false;
}

return stripos($className, $config) !== false;
return stripos($className, $list) !== false;
}
}
50 changes: 43 additions & 7 deletions src/Infrastructure/AutoloaderEventMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use TeamSquad\EventBus\Domain\Listen;
use TeamSquad\EventBus\Domain\RenamedEvent;

use function dirname;

/**
* AutoloaderEventMapGenerator generates an event map using autoloaded classes
* and annotations.
Expand All @@ -26,7 +28,7 @@
class AutoloaderEventMapGenerator implements EventMapGenerator
{
/** @var array<string, class-string<Event>> */
private static $eventMap;
private static array $eventMap;
private string $vendorPath;
private ?string $eventMapFilePath;
private AutoloadConfig $config;
Expand All @@ -36,19 +38,23 @@ class AutoloaderEventMapGenerator implements EventMapGenerator
* @param string|null $eventMapFilePath if null, the event map will not be saved
* @param array<string, array<string>|string> $configuration
*
* @psalm-param array{
* consumer_queue_listen_name?: string,
* event_bus_exchange_name?: string,
* configuration_path?: string,
* white_list?: array<string>|string,
* black_list?: array<string>|string
* } $configuration
*
* @throws UnknownEventException
* @throws InvalidArguments
*
* @psalm-suppress MixedAssignment
*/
public function __construct(string $vendorFolder, ?string $eventMapFilePath, array $configuration = [])
public function __construct(string $vendorFolder, ?string $eventMapFilePath, array $configuration)
{
$this->config = new AutoloadConfig($configuration);
$this->vendorPath = $vendorFolder;
$this->eventMapFilePath = $eventMapFilePath;
if ($this->eventMapFilePath && is_file($this->eventMapFilePath)) {
self::$eventMap = require $this->eventMapFilePath;
} else {
if (!$this->loadEventMapFile($this->eventMapFilePath)) {
$this->generate();
}
}
Expand Down Expand Up @@ -153,4 +159,34 @@ private function save(): void
fwrite($fp, $sprintf);
fclose($fp);
}

/**
* @throws InvalidArguments
*
* @psalm-suppress MixedAssignment
*/
private function loadEventMapFile(?string $eventMapFilePath): bool
{
if (!$eventMapFilePath) {
return false;
}

// Check if the directory where the event map file should be saved exists
$eventMapDirectory = dirname($eventMapFilePath);
if (is_dir($eventMapDirectory)) {
if (is_file($eventMapFilePath)) {
self::$eventMap = require $eventMapFilePath;
return true;
}

return false;
}

throw new InvalidArguments(
sprintf(
'The directory where the event map file should be saved does not exist: %s',
$eventMapDirectory
)
);
}
}

0 comments on commit 63a97c8

Please sign in to comment.