tmilos/context
Composer 安装命令:
composer require tmilos/context
包简介
Hierarchical context tree PHP library
README 文档
README
A hierarchical context tree PHP library, with hierarchical parameter bags - context, and actions working with them.
ArrayContext
The ArrayContext is the default implementation of the Context interface. It acts as a hierarchical parameter bags, when
items can also be other contexts.
<?php $context = new ArrayContext(); // can set/get/has values like to parameter bag $context->set('foo', 123); $context->get('foo'); // 123 $context->has('foo'); // true // can iterate foreach ($context as $key => $value) { } // can create sub-contexts $subContext = $context->getOrCreate('sub', SomeContext:class); // can callback for creation of sub-context $subContext = $context->getOrCall('sub', function () use ($dependencies) { return new SomeContext($dependencies); }); // can get parent context $subContext->getParent() === $context; // can get root context $leafContext = $subContext->getOrCreate('leaf', ArrayContext:class); $leafContext->getTopParent() === $context; // can dump to array $context->set('bar', ['a' => 1, 'b' => 'x']); $context->toArray(); // ['foo' => 123, 'bar' => ['a' => 1, 'b' => 'x'], 'sub' => ['leaf' => []]]
ExceptionContext
The ExceptionContext holds single exception, and when other exception is added can chain with other ExceptionContext.
$context = new ExceptionContext(new \Exception('first')); $context->addException(new \Exception('second')); $context->addException(new \Exception('third')); $context->getException()->getMessage(); // first $context->getLastException()->getMessage(); // third
Action
The Action interface defines an action that executes on a Context.
Composite Action
The CompositeAction compose several actions into one, and when executed calls each child action in the order they were added.
Action Mapper
The ActionMapper is an invokable interface that gets called to return new instances of actions that will replace the old ones.
Can be used for example to wrap actions to record their execution times and to log details.
<?php $composite = new ArrayCompositeAction(); $composite->add($actionOne); $composite->add($actionTwo); $mapper = new ActionLogWrapper(); // some implementation of the ActionMapper interface $composite->map($mapper); // inner actions gets replaced with return values of the mapper
Catchable Error Action
The CatchableErrorAction is constructed with two actions. On execute first "main" action is called, and if it throws an
Exception, an ExceptionContext is added to the supplied context and second "error" action is called.
Abstract Wrapped Action
The AbstractWrappedAction is an abstract implementation of the Action interface, that takes inner action as constructor
argument, and on execute first calls own beforeAction(Context $context) protected method, then the inner action execute() method,
and finally own afterAction(Context $context).
tmilos/context 适用场景与选型建议
tmilos/context 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 361 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 12 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tmilos/context 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tmilos/context 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 361
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-12-06