-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d0d1eac
Showing
9 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2023 Sergey Zatulivetrov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Слушатель событий | ||
|
||
Реализация простого слушателя событий. | ||
|
||
## Пример использования | ||
|
||
```php | ||
<?php | ||
|
||
use Remils\EventListener\Dispatcher; | ||
use Remils\EventListener\EventInterface; | ||
use Remils\EventListener\ListenerInterface; | ||
|
||
require './vendor/autoload.php'; | ||
|
||
class Entity | ||
{ | ||
public string $name; | ||
} | ||
|
||
class CreateEntityEvent implements EventInterface | ||
{ | ||
public string $name; | ||
} | ||
|
||
class CreateEntityListener implements ListenerInterface | ||
{ | ||
public function getEventNamespace(): string | ||
{ | ||
return CreateEntityEvent::class; | ||
} | ||
|
||
/** | ||
* @param CreateEntityEvent $event | ||
* @return void | ||
*/ | ||
public function handle(EventInterface $event): void | ||
{ | ||
$entity = new Entity(); | ||
$entity->name = $event->name; | ||
|
||
var_dump($entity); | ||
} | ||
} | ||
|
||
$listeners = [ | ||
new CreateEntityListener(), | ||
]; | ||
|
||
$dispatcher = new Dispatcher($listeners); | ||
|
||
$event = new CreateEntityEvent(); | ||
$event->name = 'Анатолий'; | ||
|
||
$dispatcher->dispatch($event); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "remils/event-listener", | ||
"type": "library", | ||
"version": "1.0.0", | ||
"description": "Слушатель событий", | ||
"keywords": [ | ||
"event", | ||
"listener" | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Remils\\EventListener\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Sergey Zatulivetrov", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"homepage": "https://github.com/remils/event-listener", | ||
"license": "MIT", | ||
"require": { | ||
"php": "^8.1" | ||
}, | ||
"require-dev": { | ||
"squizlabs/php_codesniffer": "3.*", | ||
"phpstan/phpstan": "^1.10" | ||
}, | ||
"scripts": { | ||
"analyse": [ | ||
"vendor/bin/phpcs -d memory_limit=256M --standard=PSR12 src --colors -p", | ||
"vendor/bin/phpstan analyse -l 7 src" | ||
] | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Remils\EventListener; | ||
|
||
final class Dispatcher | ||
{ | ||
/** | ||
* @param ListenerInterface[] $listeners | ||
*/ | ||
public function __construct( | ||
protected array $listeners = [], | ||
) { | ||
} | ||
|
||
/** | ||
* @param EventInterface $event | ||
* @return void | ||
*/ | ||
public function dispatch(EventInterface $event): void | ||
{ | ||
foreach ($this->listeners as $listener) { | ||
if (!$listener instanceof ListenerInterface) { | ||
throw new ListenerException( | ||
message: sprintf( | ||
'Слушатель %s не реализует интерфейс %s.', | ||
$listener::class, | ||
ListenerInterface::class, | ||
), | ||
); | ||
} | ||
|
||
if ($listener->getEventNamespace() === $event::class) { | ||
$listener->handle($event); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Remils\EventListener; | ||
|
||
interface EventInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Remils\EventListener; | ||
|
||
use Exception; | ||
|
||
final class ListenerException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Remils\EventListener; | ||
|
||
interface ListenerInterface | ||
{ | ||
/** | ||
* Пространство имени для события, которое ждет слушатель | ||
* @return string | ||
*/ | ||
public function getEventNamespace(): string; | ||
|
||
/** | ||
* Обработчик для события | ||
* @param EventInterface $event | ||
* @return void | ||
*/ | ||
public function handle(EventInterface $event): void; | ||
} |