francma/container

PSR-11 容器实现

dev-master 2019-07-05 19:04 UTC

This package is auto-updated.

Last update: 2024-09-06 06:50:41 UTC


README

Build Status Software License

此包符合以下规范:PSR-1PSR-2PSR-4PSR-11。如果您发现合规性问题,请通过pull request发送补丁。

安装

通过Composer

$ composer require francma/container

使用

$di = new Container([
    'mysql' => [
        'host' => 'localhost',
        'dbname' => 'database',
        'port' => 3306,
        'charset' => 'utf8mb4',
        'password' => 'password',
        'user' => 'root',
    ],
    PDO::class => function (Container $di): PDO {
        $cfg = $di->get('mysql');
        $options = [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES => false,
        ];
        $dsn = "mysql:host={$cfg['host']};dbname={$cfg['dbname']};port={$cfg['port']};charset={$cfg['charset']}";

        return new PDO($dsn, $cfg['user'], $cfg['password'], $options);
    },
]);

$db1 = $di->get(PDO::class);
echo $db1->query("SELECT 1")->fetch(PDO::FETCH_COLUMN), PHP_EOL;

$db2 = $di->get(PDO::class);
assert(spl_object_hash($db1) === spl_object_hash($db2));

需求

此版本支持的PHP版本如下。

  • PHP 7.1
  • PHP 7.2
  • PHP 7.3
  • PHP 7.4

测试

$ composer test
$ composer phpcs

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件