From 4d73913bef7fac149bb5363d77b52d9201defcf8 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 29 Mar 2023 15:18:13 +0000 Subject: [PATCH] - First commit --- LICENSE | 21 +++++++++++++++++++ composer.json | 18 +++++++++++++++++ src/Event.php | 20 ++++++++++++++++++ src/EventManager.php | 45 +++++++++++++++++++++++++++++++++++++++++ src/EventMiddleware.php | 37 +++++++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 src/Event.php create mode 100644 src/EventManager.php create mode 100644 src/EventMiddleware.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e54356f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2023 Dave Mc Nicoll + +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. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c1d532f --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "mcnd/event", + "description": "An event library which interface with PHP 8 native attributes.", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Dave Mc Nicoll", + "email": "info@mcnd.ca" + } + ], + "require": {}, + "autoload": { + "psr-4": { + "Mcnd\\Event\\": "src/" + } + } +} diff --git a/src/Event.php b/src/Event.php new file mode 100644 index 0000000..3a84031 --- /dev/null +++ b/src/Event.php @@ -0,0 +1,20 @@ +make($this->class)->{$this->method}(... $arguments); + } +} \ No newline at end of file diff --git a/src/EventManager.php b/src/EventManager.php new file mode 100644 index 0000000..15823c6 --- /dev/null +++ b/src/EventManager.php @@ -0,0 +1,45 @@ +eventList[] = $event; + } + + public function eventFromType(string $type) : array + { + return array_filter($this->eventList, fn($ev) => $ev instanceof $type); + } + + public function pushEvent(Event $event) : void + { + $this->eventList[] = $event; + } + + public function eventFromName(string $name) : array + { + return array_filter($this->eventList, fn($ev) => $ev->name === $name); + } + + public function execute(string $name, ...$arguments) : void + { + foreach($this->eventFromName($name) as $event) { + $event->execute($this->container, ...$arguments); + } + + foreach($this->eventfromType($name) as $event) { + call_user_func_array([ $event, 'execute'], $arguments); + } + } +} \ No newline at end of file diff --git a/src/EventMiddleware.php b/src/EventMiddleware.php new file mode 100644 index 0000000..d4c9e0f --- /dev/null +++ b/src/EventMiddleware.php @@ -0,0 +1,37 @@ +container = $container; + $this->eventManager = $eventManager; + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface + { + return $handler->handle($request); + } + + public function fromAttributes(EventFetcher $fetcher) : self + { + foreach($fetcher->compile() as $event) { + $this->eventManager->pushEvent($event); + } + + return $this; + } +}