- First commit
This commit is contained in:
		
						commit
						4d73913bef
					
				
							
								
								
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							@ -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.
 | 
			
		||||
							
								
								
									
										18
									
								
								composer.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								composer.json
									
									
									
									
									
										Normal file
									
								
							@ -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/"
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										20
									
								
								src/Event.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/Event.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Mcnd\Event;
 | 
			
		||||
 | 
			
		||||
use Psr\Container\ContainerInterface;
 | 
			
		||||
 | 
			
		||||
class Event
 | 
			
		||||
{
 | 
			
		||||
    public function __construct(
 | 
			
		||||
        public string $name,
 | 
			
		||||
        public string $class,
 | 
			
		||||
        public string $method,
 | 
			
		||||
        public \Notes\Attribute $attribute,
 | 
			
		||||
    ) {}
 | 
			
		||||
 | 
			
		||||
    public function execute(ContainerInterface $container, ... $arguments) : mixed
 | 
			
		||||
    {
 | 
			
		||||
        return $container->make($this->class)->{$this->method}(... $arguments);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								src/EventManager.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/EventManager.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,45 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Mcnd\Event;
 | 
			
		||||
 | 
			
		||||
use Psr\Container\ContainerInterface;
 | 
			
		||||
 | 
			
		||||
class EventManager
 | 
			
		||||
{
 | 
			
		||||
    public array $eventList = [];
 | 
			
		||||
 | 
			
		||||
    public function __construct(
 | 
			
		||||
        public ContainerInterface $container,
 | 
			
		||||
    ) {}
 | 
			
		||||
 | 
			
		||||
    public function eventRegister(object $event) : void
 | 
			
		||||
    {
 | 
			
		||||
        $this->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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										37
									
								
								src/EventMiddleware.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/EventMiddleware.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,37 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Mcnd\Event;
 | 
			
		||||
 | 
			
		||||
use Notes\Event\EventFetcher;
 | 
			
		||||
use Psr\Http\Message\ResponseFactoryInterface,
 | 
			
		||||
    Psr\Container\ContainerInterface,
 | 
			
		||||
    Psr\Http\Message\ResponseInterface,
 | 
			
		||||
    Psr\Http\Message\ServerRequestInterface,
 | 
			
		||||
    Psr\Http\Server\MiddlewareInterface,
 | 
			
		||||
    Psr\Http\Server\RequestHandlerInterface;
 | 
			
		||||
 | 
			
		||||
class EventMiddleware implements MiddlewareInterface
 | 
			
		||||
{
 | 
			
		||||
    public ContainerInterface $container;
 | 
			
		||||
 | 
			
		||||
    public EventManager $eventManager;
 | 
			
		||||
 | 
			
		||||
    public function __construct(ContainerInterface $container, EventManager $eventManager) {
 | 
			
		||||
        $this->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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user