arnapou/psr-container

v1.0.0 2024-09-09 17:04 UTC

This package is auto-updated.

Last update: 2024-09-09 15:06:38 UTC


README

pipeline coverage

KISS (Keep It Simple Stupid) PSR (PHP Standards Recommendations) 类。

安装

composer require arnapou/psr-container

packagist 👉️ arnapou/psr-container

何时使用此库

  • 你需要关于 PSR 的简单装饰器、代理、适配器等
  • 你需要涵盖基本内容的简单实现

示例 PSR-11 容器

服务定位器

$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 ....');

静态服务

/**
 * @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 版本

日期参考8.38.2
09/09/20241.0.x, 主要××