mnapoli/simplex
Composer 安装命令:
composer require mnapoli/simplex
包简介
Pimple fork with full container-interop support
README 文档
README
Simplex
Simplex is a Pimple 3 fork with full PSR-11 compliance and cross-framework service-provider support.
Simplex is a small dependency injection container for PHP.
Differences with Pimple
Simplex is a fork of Pimple's code. The only differences are the following:
Simplex\ContainerimplementsContainerInterface, which means the following methods exist:$container->get($id)which is an alias to$container[$id]$container->has($id)which is an alias toisset($container[$id])
- for symmetry reasons,
Simplex\Containeralso provides an additional method:$container->set($id, $value)which is an alias to$container[$id] = ...
- the constructor takes an optional
ContainerInterface $rootContainer = nullargument to support the delegate lookup feature: if provided, this container will be injected in factories instead - service providers have been completely replaced by container-interop's service providers: that allows to load cross-framework modules in this container
- it is possible to extend a scalar value with
$container->extend()(for compatibility reasons with cross-framework service providers)
Below is the documentation of Pimple/Simplex.
Installation
composer require mnapoli/simplex
Usage
Creating a container is a matter of creating a Container instance:
$container = new \Simplex\Container();
Defining Services
A service is an object that does something as part of a larger system. Examples of services: a database connection, a templating engine, or a mailer. Almost any global object can be a service.
Services are defined by anonymous functions that return an instance of an object:
// define some services $container['session_storage'] = function ($c) { return new SessionStorage('SESSION_ID'); }; $container['session'] = function ($c) { return new Session($c['session_storage']); };
Notice that the anonymous function has access to the current container instance, allowing references to other services or parameters.
As objects are only created when you get them, the order of the definitions does not matter.
Using the defined services is also very easy:
// get the session object $session = $container['session']; // the above call is roughly equivalent to the following code: // $storage = new SessionStorage('SESSION_ID'); // $session = new Session($storage);
Defining Factory Services
By default, each time you get a service, Pimple returns the same instance
of it. If you want a different instance to be returned for all calls, wrap your
anonymous function with the factory() method
$container['session'] = $container->factory(function ($c) { return new Session($c['session_storage']); });
Now, each call to $container['session'] returns a new instance of the
session.
Defining Parameters
Defining a parameter allows to ease the configuration of your container from the outside and to store global values:
// define some parameters $container['cookie_name'] = 'SESSION_ID'; $container['session_storage_class'] = 'SessionStorage';
If you change the session_storage service definition like below:
$container['session_storage'] = function ($c) { return new $c['session_storage_class']($c['cookie_name']); };
You can now easily change the cookie name by overriding the
session_storage_class parameter instead of redefining the service
definition.
Protecting Parameters
Because Pimple sees anonymous functions as service definitions, you need to
wrap anonymous functions with the protect() method to store them as
parameters:
$container['random_func'] = $container->protect(function () { return rand(); });
Modifying Services after Definition
In some cases you may want to modify a service definition after it has been
defined. You can use the extend() method to define additional code to be
run on your service just after it is created:
$container['session_storage'] = function ($c) { return new $c['session_storage_class']($c['cookie_name']); }; $container->extend('session_storage', function ($storage, $c) { $storage->...(); return $storage; });
The first argument is the name of the service to extend, the second a function that gets access to the object instance and the container.
Service providers
Simplex supports registering cross-framework service providers.
To register service providers, pass an array of service providers as first constructor argument.
$container = new \Simplex\Container([new MyServiceProvider()]);
mnapoli/simplex 适用场景与选型建议
mnapoli/simplex 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 124.17k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2016 年 03 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency injection」 「container」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mnapoli/simplex 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mnapoli/simplex 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mnapoli/simplex 相关的其它包
同方向 / 同关键字的高下载量 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
统计信息
- 总下载量: 124.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 24
- 依赖项目数: 23
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-07