Skip to content

Commit

Permalink
Слушатель событий
Browse files Browse the repository at this point in the history
  • Loading branch information
remils committed Oct 16, 2023
0 parents commit d0d1eac
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
20 changes: 20 additions & 0 deletions LICENSE
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.
56 changes: 56 additions & 0 deletions README.md
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);
```
36 changes: 36 additions & 0 deletions composer.json
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"
]
}
}
140 changes: 140 additions & 0 deletions composer.lock

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

39 changes: 39 additions & 0 deletions src/Dispatcher.php
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);
}
}
}
}
9 changes: 9 additions & 0 deletions src/EventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Remils\EventListener;

interface EventInterface
{
}
11 changes: 11 additions & 0 deletions src/ListenerException.php
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
{
}
21 changes: 21 additions & 0 deletions src/ListenerInterface.php
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;
}

0 comments on commit d0d1eac

Please sign in to comment.