sugiphp/container

简单的 PHP 依赖注入容器系统

1.1.0 2016-04-18 12:23 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:55:37 UTC


README

Build Status

SugiPHP Container 的第 2 版实现了 ContainerInterface

安装

# stable version (when available)
composer require sugiphp/container ~2.0

# development
composer require sugiphp/container ~2.@dev

使用

容器可以存储两种不同的数据类型:对象(服务)和参数。

存储值

<?php
$container = new Container();
// store a parameter
$container->set("param", "value");
// store an object
$container->set("pdo", function() {
	return new PDO("mysql:dbname=testdb;host=127.0.0.1", "user", "pass");
});
?>

获取之前存储的值和对象

<?php
$container->get("param"); // returns "value"
$container->get("unset"); // will throw an NotFoundException
$db = $container->get("pdo"); // returns instance of a PDO (not the closure itself, but the result);
// later in a code...
$db1 = $container->get("pdo"); // returns the SAME instance of the PDO (not new instance!) ($db1 === $db)

// if you need a new instance of the PDO you can force it with factory() method
$db2 = $container->factory("pdo"); // returns new instance of the PDO.
// the second instance is not stored in a container, so if you use factory again
$db3 = $container->factory("pdo"); // you'll get third instance which is different from the instances above

$db4 = $container->get("pdo"); // will return same instance as the first one ($db4 === $db === $db1)
?>

始终获取最新副本(新实例)

<?php
// Wrap closure in factory method
$container->set("rand", $container->factory(function() {
	return mt_rand();
}));

$rand1 = $container->get("rand");
$rand2 = $container->get("rand");
// both values will differ (unless your are extremely lucky)
?>

获取存储的闭包时保持原样

<?php
$closure = $container->raw("pdo"); // this will return the closure, not the result
// so you can invoke it and make a new PDO instance
$db = $closure();
?>

始终获取原始服务

<?php
$container->set("name", $container->raw(function() {
	return "John";
}));

is_string($container->get("name")); // FALSE
// actually it will return stored closure
?>

检查键的存在性

<?php
$container->set("param", "value");
$container->set("null", NULL);

$container->has("param"); // TRUE
$container->has("null"); // TRUE
$container->has("unset"); // FALSE
?>

删除键

要删除之前存储的键,请使用 delete($key) 方法

覆盖键并锁定它们

<?php
// set a "name"
$container->set("name", "John");
$container->get("name"); // "John"
// override a "name"
$container->set("name", "John Doe");
$container->get("name"); // "John Doe"

// lock a key
$container->lock("name");
// now if you try to override "name"
$container->set("name", "Foo Bar"); // will throw ContainerException
// or try to delete that key
$container->delete("name"); // will throw ContainerException
?>

注意,没有 unlock() 方法。

数组访问

容器实现了内置的 PHP ArrayAccess 类,这意味着您可以使用数组表示法来存储、检索、检查和删除值

<?php
$container["foo"] = "bar";
echo $container["foo"]; // prints bar
$container["pdo"] = function () {
    return new PDO("mysql:dbname=testdb;host=127.0.0.1", "user", "pass");
};
$db = $container["pdo"]; // returns instance of the PDO class
// checking for existence
isset($container["foo"]); // TRUE
// delete a key
unset("foo");
// checking for existence
isset($container["foo"]); // FALSE
?>

注意,与典型的数组不同,尝试获取未设置的键将抛出错误,容器将保持沉默并返回 NULL。

您还可以使用 foreach 构造。

属性访问

容器使用魔法 __set__get 方法允许通过属性进行访问

$container->set("foo") = "bar";
$container["foo"] = "bar";
$container->foo = "bar";
// All of the above are doing the same job

$container->get("foo"); // "bar"
$container["foo"]; // "bar"
$container->foo; // "bar"