arnapou/psr-container

Library - PSR-11.

v1.1.1 2024-12-02 14:22 UTC

This package is auto-updated.

Last update: 2024-12-02 13:25:03 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.48.38.2
24/11/20241.1.x, main×××
09/09/20241.0.x××