搜索
首页php框架Laravel关于Laravel Pipeline的解读

关于Laravel Pipeline的解读

Jun 21, 2021 pm 05:06 PM
aoplaravelphp

下面由laravel教程栏目给大家解读Laravel Pipeline,希望对需要的朋友有所帮助!

大家好,今天给大家介绍下Laravel框架的Pipeline。
它是一个非常好用的组件,能够使代码的结构非常清晰。 Laravel的中间件机制便是基于它来实现的。

通过Pipeline,可以轻松实现APO编程。

官方GIT地址

https://github.com/illuminate/pipeline

下面的代码是我实现的一个简化版本:

class Pipeline
{

    /**
     * The method to call on each pipe
     * @var string
     */
    protected $method = 'handle';

    /**
     * The object being passed throw the pipeline
     * @var mixed
     */
    protected $passable;

    /**
     * The array of class pipes
     * @var array
     */
    protected $pipes = [];

    /**
     * Set the object being sent through the pipeline
     *
     * @param $passable
     * @return $this
     */
    public function send($passable)
    {
        $this->passable = $passable;
        return $this;
    }

    /**
     * Set the method to call on the pipes
     * @param array $pipes
     * @return $this
     */
    public function through($pipes)
    {
        $this->pipes = $pipes;
        return $this;
    }

    /**
     * @param \Closure $destination
     * @return mixed
     */
    public function then(\Closure $destination)
    {
        $pipeline = array_reduce(array_reverse($this->pipes), $this->getSlice(), $destination);
        return $pipeline($this->passable);
    }


    /**
     * Get a Closure that represents a slice of the application onion
     * @return \Closure
     */
    protected function getSlice()
    {
        return function($stack, $pipe){
            return function ($request) use ($stack, $pipe) {
                return $pipe::{$this->method}($request, $stack);
            };
        };
    }

}

此类主要逻辑就在于then和getSlice方法。通过array_reduce,生成一个接受一个参数的匿名函数,然后执行调用。

简单使用示例

class ALogic
{
    public static function handle($data, \Clourse $next)
    {
        print "开始 A 逻辑";
        $ret = $next($data);
        print "结束 A 逻辑";
        return $ret;
    }
}

class BLogic
{
    public static function handle($data, \Clourse $next)
    {
        print "开始 B 逻辑";
        $ret = $next($data);
        print "结束 B 逻辑";
        return $ret;
    }
}

class CLogic
{
    public static function handle($data, \Clourse $next)
    {
        print "开始 C 逻辑";
        $ret = $next($data);
        print "结束 C 逻辑";
        return $ret;
    }
}
$pipes = [
    ALogic::class,
    BLogic::class,
    CLogic::class
];

$data = "any things";
(new Pipeline())->send($data)->through($pipes)->then(function($data){ print $data;});
运行结果:
"开始 A 逻辑"
"开始 B 逻辑"
"开始 C 逻辑"
"any things"
"结束 C 逻辑"
"结束 B 逻辑"
"结束 A 逻辑"

AOP示例

AOP 的优点就在于动态的添加功能,而不对其它层次产生影响,可以非常方便的添加或者删除功能。

class IpCheck
{
    public static function handle($data, \Clourse $next)
    {
        if ("IP invalid") { // IP 不合法
            throw Exception("ip invalid");
        }
        return $next($data);
    }
}

class StatusManage
{
    public static function handle($data, \Clourse $next)
    {
        // exec 可以执行初始化状态的操作
        $ret = $next($data)
        // exec 可以执行保存状态信息的操作
        return $ret;
    }
}

$pipes = [
    IpCheck::class,
    StatusManage::class,
];

(new Pipeline())->send($data)->through($pipes)->then(function($data){ "执行其它逻辑";});

以上是关于Laravel Pipeline的解读的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:segmentfault。如有侵权,请联系admin@php.cn删除
Laravel:迁移和模型之间有什么区别?Laravel:迁移和模型之间有什么区别?May 16, 2025 am 12:15 AM

迁移在Laravelmanagedatabaseschema中,同时shandledatainterAction.1)迁移术语,允许创造,修改,且deletionoftables.2)models representDataAndDataAndDataAndDataAndDataAndDataAndDataAndDataAndProvideAnInterForceNtactaction,EnablingCrudoPerations

Laravel:使用软删除或物理删除更好吗?Laravel:使用软删除或物理删除更好吗?May 16, 2025 am 12:15 AM

SoftDeletsinlaraveRareBetterTernaverainteraldatialdataAndRecoverability,而骨质骨骼验证了forderableford.fordableablefordataminimization和Privacy.1)softerdeleteseThesoftDeletDeletEstrait,允许restrestoratorralityandaudtrails和mayincroredatabasesize.2)物理

Laravel软删除:实施的综合指南Laravel软删除:实施的综合指南May 16, 2025 am 12:11 AM

softDeletsinlesinlaravelareafareafareathataLowSyOutOmarkRecordSdeletedwithOutreMovingThemfromthedataBase.toimplementsoftesoftdeletes:1)addtheSoftDeletDeletEstraittRaittRaiteRemodoyourModeNClandInClandEnCludEthedEtelet_Atcolundelet_atcolumn.2)

了解Laravel迁移:数据库架构控制变得容易了解Laravel迁移:数据库架构控制变得容易May 16, 2025 am 12:09 AM

laravelmigrationsareefectectivectiveDueTotheirversionControlandRoranderibalsible,slepliningDatabasemagementInwebDevelopment.1)heSpapsulatesCheMachangeNphpClasses,允许easyerollbacks

Laravel迁移:数据库开发的最佳实践Laravel迁移:数据库开发的最佳实践May 16, 2025 am 12:01 AM

LaravelMigrationsareArareBestWhenFollowingTheSepractices:1)用户清除,描述性formigrations,例如'addemailtouserstable'.2)ensuremigrationsareReereSareReverSiblewitha'down'method.3)考虑到the the the the the the the the the the the the the the broaderimptactondataintegnegrityAndegrityAndegrinegrityAndertality.4)optimizeperformanceb

Laravel   Vue.js 开发单页面应用(SPA)教程Laravel Vue.js 开发单页面应用(SPA)教程May 15, 2025 pm 09:54 PM

使用Laravel和Vue.js可以构建单页面应用(SPA)。1)在Laravel中定义API路由和控制器,处理数据逻辑。2)在Vue.js中创建组件化前端,实现用户界面和数据交互。3)配置CORS和使用axios进行数据交互。4)利用VueRouter实现路由管理,提升用户体验。

如何在Laravel中创建自定义辅助函数?如何在Laravel中创建自定义辅助函数?May 15, 2025 pm 09:51 PM

在Laravel中创建自定义辅助函数的步骤是:1.在composer.json中添加自动加载配置;2.运行composerdump-autoload更新自动加载器;3.在app/Helpers目录下创建并定义函数。这些函数能简化代码,提高可读性和可维护性,但需注意命名冲突和测试性。

Laravel中的数据库事务(Transactions)如何处理?Laravel中的数据库事务(Transactions)如何处理?May 15, 2025 pm 09:48 PM

在Laravel中处理数据库事务时,应使用DB::transaction方法,并注意以下要点:1.使用lockForUpdate()锁定记录;2.通过try-catch块处理异常,并在需要时手动回滚或提交事务;3.考虑事务的性能,缩短执行时间;4.避免死锁,可使用attempts参数重试事务。这段摘要完整地概括了如何在Laravel中优雅地处理事务,并提炼了文章中的核心要点和最佳实践。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

北端:融合系统,解释
1 个月前By尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
4 周前By尊渡假赌尊渡假赌尊渡假赌
<🎜>掩盖:探险33-如何获得完美的色度催化剂
2 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用