From 0ccd58144fed6d442fc169e0361c57e63f872caf Mon Sep 17 00:00:00 2001 From: Maxim <862272+max107@users.noreply.github.com> Date: Tue, 1 Dec 2020 13:05:37 +0300 Subject: [PATCH] fix #25 (#26) --- composer.json | 124 +++++++++++++++-------------- src/Services/EventTypeResolver.php | 38 ++++++++- 2 files changed, 100 insertions(+), 62 deletions(-) diff --git a/composer.json b/composer.json index c9fdac4..5836440 100644 --- a/composer.json +++ b/composer.json @@ -1,63 +1,69 @@ { - "description": "The LeNats is a Symfony bundle. it's implements event-based architecture with nats streaming on php", - "name": "vseinstrumentiru/lenats", - "type": "symfony-bundle", - "license": "GPL-3.0-only", - "keywords": ["nats", "nats streaming", "cloud events", "symfony", "php", "event-driven"], - "authors": [ - { - "name": "Ilya Shtrikul", - "email": "shtricul@gmail.comm" - } - ], - "config": { - "preferred-install": { - "*": "dist" + "description": "The LeNats is a Symfony bundle. it's implements event-based architecture with nats streaming on php", + "name": "vseinstrumentiru/lenats", + "type": "symfony-bundle", + "license": "GPL-3.0-only", + "keywords": [ + "nats", + "nats streaming", + "cloud events", + "symfony", + "php", + "event-driven" + ], + "authors": [ + { + "name": "Ilya Shtrikul", + "email": "shtricul@gmail.comm" + } + ], + "config": { + "preferred-install": { + "*": "dist" + }, + "sort-packages": true }, - "sort-packages": true - }, - "autoload": { - "psr-4": { - "LeNats\\": "src/", - "GPBMetadata\\": "gen/GPBMetadata/", - "NatsStreamingProtocol\\": "gen/NatsStreamingProtocol/" - } - }, - "autoload-dev": { - "psr-4": { - "LeNats\\Tests\\": "tests/" + "autoload": { + "psr-4": { + "LeNats\\": "src/", + "GPBMetadata\\": "gen/GPBMetadata/", + "NatsStreamingProtocol\\": "gen/NatsStreamingProtocol/" + } + }, + "autoload-dev": { + "psr-4": { + "LeNats\\Tests\\": "tests/" + } + }, + "require": { + "php": "^7.3", + "ext-ctype": "*", + "ext-iconv": "*", + "ext-json": "*", + "clue/block-react": "^1.3", + "google/protobuf": "^3.7", + "jms/serializer-bundle": "^2.4|^3.3", + "mhujer/jms-serializer-uuid": "^1.0|^3.1", + "psr/event-dispatcher": "^0.6|^1.0", + "rakit/validation": "^1.1", + "react/promise-timer": "^1.5", + "react/socket": "^1.2", + "react/stream": "^1.1", + "symfony/console": "^4.4", + "symfony/event-dispatcher": "^4.4", + "symfony/framework-bundle": "^4.4", + "symfony/yaml": "^4.4" + }, + "require-dev": { + "phpstan/phpstan": "^0.11.2", + "phpstan/phpstan-symfony": "^0.11.1", + "symfony/phpunit-bridge": "^4.4", + "symplify/easy-coding-standard": "^5.4", + "phpunit/phpunit": "^8.0" + }, + "scripts": { + "ecs": "vendor/bin/ecs check ./src --fix", + "test": "bin/phpunit", + "stan": "vendor/bin/phpstan analyze ./src" } - }, - "require": { - "php": "^7.3", - "ext-ctype": "*", - "ext-iconv": "*", - "ext-json": "*", - "clue/block-react": "^1.3", - "google/protobuf": "^3.7", - "illuminate/support": "^5.8", - "jms/serializer-bundle": "^2.4|^3.3", - "mhujer/jms-serializer-uuid": "^1.0|^3.1", - "psr/event-dispatcher": "^0.6|^1.0", - "rakit/validation": "^1.1", - "react/promise-timer": "^1.5", - "react/socket": "^1.2", - "react/stream": "^1.1", - "symfony/console": "^4.4", - "symfony/event-dispatcher": "^4.4", - "symfony/framework-bundle": "^4.4", - "symfony/yaml": "^4.4" - }, - "require-dev": { - "phpstan/phpstan": "^0.11.2", - "phpstan/phpstan-symfony": "^0.11.1", - "symfony/phpunit-bridge": "^4.4", - "symplify/easy-coding-standard": "^5.4", - "phpunit/phpunit": "^8.0" - }, - "scripts": { - "ecs": "vendor/bin/ecs check ./src --fix", - "test": "bin/phpunit", - "stan": "vendor/bin/phpstan analyze ./src" - } } diff --git a/src/Services/EventTypeResolver.php b/src/Services/EventTypeResolver.php index 1cc6270..e8f903b 100644 --- a/src/Services/EventTypeResolver.php +++ b/src/Services/EventTypeResolver.php @@ -2,8 +2,6 @@ namespace LeNats\Services; -use Illuminate\Support\Str; - class EventTypeResolver { /** @var string[] */ @@ -19,7 +17,7 @@ public function getClass(string $eventType): ?string $class = null; foreach ($this->types as $typeWildcard => $eventClass) { - if ($typeWildcard === $eventType || Str::is($typeWildcard, $eventType)) { + if ($typeWildcard === $eventType || $this->is($typeWildcard, $eventType)) { $class = $eventClass; break; } @@ -27,4 +25,38 @@ public function getClass(string $eventType): ?string return $class; } + + /** + * Determine if a given string matches a given pattern. + * + * @param string|array $pattern + * @param string $value + * @return bool + */ + public function is($pattern, $value) + { + if (empty($pattern)) { + return false; + } + + // If the given value is an exact match we can of course return true right + // from the beginning. Otherwise, we will translate asterisks and do an + // actual pattern match against the two strings to see if they match. + if ($pattern == $value) { + return true; + } + + $pattern = preg_quote($pattern, '#'); + + // Asterisks are translated into zero-or-more regular expression wildcards + // to make it convenient to check if the strings starts with the given + // pattern such as "library/*", making any string check convenient. + $pattern = str_replace('\*', '.*', $pattern); + + if (preg_match('#^'.$pattern.'\z#u', $value) === 1) { + return true; + } + + return false; + } }