/psr-ex

PSR example implementations

Primary LanguagePHPMIT LicenseMIT

PSR Examples

ci.yml

PSR-3 logger implementation

PSR-11 container implementation

PSR-14 event dispatcher implementation

Usage

Logger

use Shirokovnv\PsrEx\Log\Formatter\BaseFormatter;
use Shirokovnv\PsrEx\Log\Handler\FileHandler;
use Shirokovnv\PsrEx\Log\Logger;

// Example logging to the file
$logger = new Logger(new FileHandler('log.txt'), new BaseFormatter());
$logger->log(\Psr\Log\LogLevel::DEBUG, 'debug message', [ 'context' => 'some context' ]);

Container

use Shirokovnv\PsrEx\Container\Container;

$container = new Container();

// Put into the container
$container->set(ServiceInterface::class, Service::class);

// Get from the container
// assert instanceOf Service::class
$instance = $container->get(ServiceInterface::class);

// Check existence
if ($container->has(ServiceInterface::class)) {
    // do stuff
}

Events

use Shirokovnv\PsrEx\Event\EventDispatcher;
use Shirokovnv\PsrEx\Event\ListenerProvider;

$event = new MyEvent();
$listener = new MyEventListener();

$listenerProvider = new ListenerProvider();
$eventDispatcher = new EventDispatcher($listenerProvider);

// Bind event and listener
$listenerProvider->addListener($event::class, $listener);

// Dispatch
$eventDispatcher->dispatch($event);

// Clear
$listenerProvider->clearListeners();

Testing

$ composer test

License

MIT. Please see the license file for more information.