aviator/pipe
Composer 安装命令:
composer require aviator/pipe
包简介
Chain callables using a simple wrapper class.
README 文档
README
PHP code can be frustratingly opaque, especially when dealing with nested functions. Why not chain those functions instead?
$filter = function ($item) { return $item === 'SOME'; }; // Offputting one-liner echo implode('.', array_filter(explode('-', strtoupper(trim(' some-value'))), $filter)) // Multiple assignments $value = ' some-value'; $value = trim($value); $value = strtoupper($value); $value = explode('-', $value); $value = array_filter($value, $filter); echo implode('.', $value); // Easy to read pipe echo take(' some-value') ->trim() ->strtoupper() ->explode('-', '$$') ->array_filter($filter) ->implode('.', '$$') ->get(); // prints 'SOME'
Installation
Via Composer:
composer require aviator/pipe
Testing
Via Composer:
composer test
Usage
Get a Pipe object:
$value = new Pipe('value'); $value = Pipe::take('value'); $value = take('value');
Then you can chain callables:
$value->pipe('strtoupper');
And get the mutated value:
echo $value->get(); // prints 'VALUE'
The pipe method is chainable:
echo Pipe::take(' value') ->pipe('trim') ->pipe('strtoupper') ->get(); // prints 'VALUE'
Pipe uses a magic __call to redirect other methods to the pipe method, so you don't have to use pipe(...) at all:
echo Pipe::take(' value') ->trim() ->strtoupper() ->get(); // prints 'VALUE'
Arguments
You can use callables with arguments:
echo Pipe::take('value') ->str_repeat(3) ->strtoupper() ->get(); // prints 'VALUEVALUEVALUE'
Pipe will always pass the value you're mutating ('value' in the example above) as the first parameter.
This works most of the time, but since PHP has some unique parameter ordering, there are cases where it doesn't. In these cases you can use the placeholder, by default $$, to represent the mutating value.
For example, implode():
echo Pipe::take(['some', 'array']) ->implode('.', '$$') ->get(); // prints 'some.array'
Because implode() takes the input value as its second parameter, we tell Pipe where to put it using '$$'. Then when called the value is swapped in.
Closures
You may pipe any callable, including a closure:
$closure = function ($item) { return $item . '-postfixed'; }; echo Pipe::take('value') ->pipe($closure) ->get(); // prints 'value-postfixed'
Other Stuff
License
This package operates under the MIT License (MIT). Please see LICENSE for more information.
Thanks
This is largely based on Sebastiaan Luca's idea and his Pipe\Item class.
aviator/pipe 适用场景与选型建议
aviator/pipe 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.91k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「pipe」 「PHP7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aviator/pipe 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aviator/pipe 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aviator/pipe 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
php7ify is a project that brings new php7 classes and exceptions to php 5.x.
Functional programming utilities for composing, decorating, and controlling function execution
Minimalist and Typed Immutable Collections
Creates an XML file for a Single Euro Payments Area (SEPA) Credit Transfer.
CLI tooling with zero configuration required.
php proc_open wrapper class for shell command process management.
统计信息
- 总下载量: 2.91k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-06