arnapou/psr-container

Library - PSR-11.

v1.0.2 2024-09-24 21:21 UTC

This package is auto-updated.

Last update: 2024-11-14 18:31:01 UTC


README

pipeline coverage

KISS (Keep It Simple Stupid) PSR (PHP Standards Recommendations) classes.

Installation

composer require arnapou/psr-container

packagist 👉️ arnapou/psr-container

When it is worth to use this library

  • you need simple decorators, proxies, adapters, ... about PSR's
  • you need simple implementations covering the basics

Example PSR-11 Container

Service Locator

$container = new \Arnapou\Psr\Psr11Container\ServiceLocator();
$container->registerFactory(
    'db',
    static function() {
        $pdo = new PDO($dsn, $username, $password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        return $pdo;
    }
);

$container->get('db')->query('SELECT ....');

Static Services

/**
 * @method static \PDO db()
 */
class Services extends \Arnapou\Psr\Psr11Container\StaticServices
{
    public function getFactories(): iterable
    {
        yield 'db' => static function() {
            $pdo = new PDO($dsn, $username, $password);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            
            return $pdo;
        };
    }
}

Services::db()->query('SELECT ....');

Php versions

DateRef8.38.2
09/09/20241.0.x, main××