juliangut/slim-php-di
Composer 安装命令:
composer require juliangut/slim-php-di
包简介
Slim Framework PHP-DI container integration
README 文档
README
Slim Framework PHP-DI container integration
PHP-DI dependency injection container integration for Slim framework.
Installation
Best way to install is using Composer:
composer require juliangut/slim-php-di
Then require_once the autoload file:
require_once './vendor/autoload.php';
Usage
Use Jgut\Slim\PHPDI\ContainerBuilder to create PHP-DI container and extract Slim's App from it
use Jgut\Slim\PHPDI\Configuration; use Jgut\Slim\PHPDI\ContainerBuilder; use Psr\Container\ContainerInterface; use Slim\App; $container = ContainerBuilder::build(new Configuration()); $app = $container->get(App::class); // same as $app = \Slim\Factory\AppFactory::createFromContainer($container); // Register your services if not provided as definitions $container->set('service_one', function (ContainerInterface $container): ServiceOne { return new ServiceOne($container->get('service_two')); }); // Set your routes $app->run();
In order to register services in the container it's way better to do it in definition files
Configuration
use Jgut\Slim\PHPDI\Configuration; use Jgut\Slim\PHPDI\ContainerBuilder; $settings = [ 'useAttributes' => true, 'compilationPath' => '/path/to/compiled/container', ]; $configuration = new Configuration($settings); // Settings can be set after creation $configuration->setProxiesPath(sys_get_temp_dir()); $configuration->setDefinitions('/path/to/definition/files'); $container = ContainerBuilder::build($configuration);
PHP-DI settings
useAutoWiringwhether to use auto wiring (true by default)useAttributeswhether to use attributes (false by default)useDefinitionCache, whether to use definition cache (false by default)wrapContainerwrapping container (none by default)proxiesPathpath where PHP-DI creates its proxy files (none by default)compilationPathpath where PHP-DI creates its compiled container (none by default)
Refer to PHP-DI documentation to learn more about container configurations
Additional settings
definitionsan array of paths to definition files/directories or arrays of definitions. Definitions are loaded in order of appearancecontainerClasscontainer class used on the build. Must implement\Psr\Container\ContainerInterface,\DI\FactoryInterfaceand\DI\InvokerInterface(\Jgut\Slim\PHPDI\Containerby default)
Container array access shorthand
Default \Jgut\Slim\PHPDI\Container container allows shorthand array access by concatenating array keys with dots. If any key in the chain is not defined, normal Psr\Container\NotFoundExceptionInterface exception is thrown
use Jgut\Slim\PHPDI\Configuration; use Jgut\Slim\PHPDI\ContainerBuilder; $container = ContainerBuilder::build(new Configuration([])); $configs = [ 'database' => [ 'dsn' => 'mysql://root:pass@localhost/my_ddbb', ], ]; $container->set('configs', $configs); $container->get('configs')['database']['dsn']; $container->get('configs.database.dsn'); // same as above
Notice
Be careful though not to shadow any array key by using dots in keys themselves
use Jgut\Slim\PHPDI\Configuration; use Jgut\Slim\PHPDI\ContainerBuilder; $container = ContainerBuilder::build(new Configuration([])); $configs = [ 'foo' => [ 'bar' => [ 'baz' => 'shadowed!', // <== watch out! ], ], 'foo.bar' => 'bingo!', ]; $container->set('configs', $configs); $container->get('configs.foo.bar'); // bingo! $container->get('configs.foo.bar.baz'); // NotFoundExceptionInterface thrown
The easiest way to avoid this from ever happening is by NOT using dots in array keys
Invocation strategy
By default, slim-php-di sets a custom invocation strategy that employs PHP-DI's Invoker to fulfill callable parameters, it lets you do things like this
use Jgut\Slim\PHPDI\Configuration; use Jgut\Slim\PHPDI\ContainerBuilder; use Psr\Http\Message\ResponseInterface; use Slim\App; $container = ContainerBuilder::build(new Configuration([])); $app = $container->get(App::class); $app->get('/hello/{name}', function (ResponseInterface $response, string $name, \PDO $connection): ResponseInterface { // $name will be injected from request arguments // $connection will be injected directly from the container $response->getBody()->write('Hello ' . $name); return $response; }); $app->run();
If you prefer default Slim's Slim\Handlers\Strategies\RequestResponse strategy or any other of your choosing you only have to set it in a definition file
use Slim\Handlers\Strategies\RequestResponse; use Slim\Interfaces\InvocationStrategyInterface; use function DI\create; return [ InvocationStrategyInterface::class => create(RequestResponse::class), ];
Console command
use Symfony\Component\Console\Application; use Jgut\Slim\PHPDI\Command\ListCommand; /** @var \Slim\App $app */ $container = $app->getContainer(); $cli = new Application('Slim CLI'); $cli->add(new ListCommand($container)); $app->run();
List container definitions
List defined container definitions supporting searching
php -f cli.php slim:container:list --help
Migration from 3.x
- PHP minimum required version is PHP 8.0
- Moved to PHP-DI 7. Annotations have been removed, use Attributes
Contributing
Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.
See file CONTRIBUTING.md
License
See file LICENSE included with the source code for a copy of the license terms.
juliangut/slim-php-di 适用场景与选型建议
juliangut/slim-php-di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30.57k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2015 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency injection」 「container」 「slim」 「php-di」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 juliangut/slim-php-di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 juliangut/slim-php-di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 juliangut/slim-php-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
统计信息
- 总下载量: 30.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 13
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-08-14