phapi/di
最新稳定版本:1.0.0
Composer 安装命令:
composer require phapi/di
包简介
Phapi Dependency Injection Container
README 文档
README
Phapi has a built in Dependency Injector Container that can be used to store both objects and parameters. It's easy to use and it gives the opportunity to replace many of the built in functionalities since Phapi uses the Container to store most of it's objects, dependencies and parameters.
The container can be accessed in an endpoint by using the $this->container parameter.
Installation
The package is installed by default by the Phapi framework. Installing the package to use is separately can be done by using composer:
$ composer require phapi/di:1.*
Defining parameters
The easiest example is to store parameters in the container:
<?php // Set parameter $container['param'] = 'value'; // Get parameter echo $container['param']; // Output: value
Defining objects and dependencies
Objects and its dependencies are defined by anonymous functions that returns an instance of an object, you have two options you can use to define them. Either by using:
<?php // Define database configuration $container['dbUserName'] = 'root'; $container['dbPassword'] = '1234'; // Add DB connection to container $container['dbConn'] = function ($container) { return new PDO( 'mysql:host=localhost;dbname=test', $container['dbUserName'], $container['dbPassword'] ); };
Or by using the bind() method:
<?php // Define database configuration $container['dbUserName'] = 'root'; $container['dbPassword'] = '1234'; // Add DB connection to container $container->bind('dbConn', function ($container) { return new PDO( 'mysql:host=localhost;dbname=test', $container['dbUserName'], $container['dbPassword'] ); });
Notice that the function has access to the container instance which makes it possible to fetch parameters and dependencies from the container.
By default the container will return the same instance of the object each time you get it. The bind() method takes a third optional parameter for defining if the singleton or multiton pattern should be used while creating the object. The default is singleton. Change it to multiton and each call to $container['dbConn'] will now create and return a new instance.Example:
<?php // Define database configuration $container['dbUserName'] = 'root'; $container['dbPassword'] = '1234'; // Add DB connection to container $container->bind('dbConn', function ($container) { return new PDO( 'mysql:host=localhost;dbname=test', $container['dbUserName'], $container['dbPassword'] ); }, \Phapi::TYPE_MULTITON);
The order of the definitions doesn't matter since the objects are created when you get them.
Retrieving objects and/or parameters
Using the object is easy:
<?php // Use DB connection $container['dbConn']->query('SELECT ...');
Or use the make() method:
<?php // Get DB connection $db = $container->make('dbConn'); // Use the connection $db->query('Select ...');
Removing objects and/or parameters
The container implements the \ArrayAccess interface which makes it possible to use as a regular array. Removing an object or a parameter is just a matter of unsetting it:
<?php unset($container['dbConn']);
Validators
In some cases you might want to ensure a certain key has a specific type of value. Phapi needs the $container['log'] to be a PSR-3 compatible logger. To enforce this a validator is assigned to the log key.
<?php // Register validator $container->addValidator('log', new \Phapi\Container\Validator\Log($this));
The validators validate() method is a simple check if the provided logger is PSR-3 compatible:
<?php /** * Validates the configured logger. If no logger is configured or * if the configured logger isn't PSR-3 compliant an instance of * NullLogger will be used instead. * * The PSR-3 package includes a NullLogger that doesn't do * anything with the input but it also prevents the application * from failing. * * This simplifies the development since we don't have to check * if there actually are a valid cache to use. We can just ask * the Cache (even if its a NullCache) and we will get a response. */ public function validate($logger) { $original = $logger; if (is_callable($logger)) { $logger = $logger($this->container); } // Check if logger is an instance of the PSR-3 logger interface if ($logger instanceof LoggerInterface) { return $original; } // A PSR-3 compatible log writer hasn't been configured so we // don't know if it is compatible with Phapi. Therefore we // create an instance of the NullLogger instead return function ($container) { return new NullLogger(); }; }
As you can see in this example we don't raise an error even if the provided logger is invalid. This is done since we don't want the application to break because of a faulty logger. In other cases it's probably more suitable to throw an exception.
License
Phapi Di Container is licensed under the MIT License - see the license.md file for details
Contribute
Contribution, bug fixes etc are always welcome.
phapi/di 适用场景与选型建议
phapi/di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.81k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「di」 「dependency injection container」 「phapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phapi/di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phapi/di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phapi/di 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Dependency injection container for the Monolith framework.
The PSR-11 container bridges
Http Microframework for Hack
PHP Dependency Injection Container
统计信息
- 总下载量: 3.81k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-12