搜索
首页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版本如何简化开发?Apr 24, 2025 pm 05:01 PM

thelatestlaravelververversionenhancesdevelopmentwith:1)简化的inimpliticmodelbinding,2)增强EnhancedeloquentcapabibilitionswithNewqueryMethods和3)改善了supportorfortormodernphpfortornphpforternphpfeatureserslikenamedargenamedArgonedArgonsemandArgoctess,makecodingMoreftermeforefterMealiteFficeAndEnjoyaigaigaigaigaigaiganigaborabilyaboipaigyAndenjoyaigobyabory。

在哪里可以找到最新的Laravel版本的发行说明?在哪里可以找到最新的Laravel版本的发行说明?Apr 24, 2025 pm 04:53 PM

你可以在laravel.com/docs找到最新Laravel版本的发布说明。1)发布说明提供了新功能、错误修复和改进的详细信息。2)它们包含示例和解释,帮助理解新功能的应用。3)注意新功能的潜在复杂性和向后兼容性问题。4)定期审查发布说明可以保持更新并激发创新。

远程工具包:在分布式团队中保持连接的基本工具远程工具包:在分布式团队中保持连接的基本工具Apr 24, 2025 pm 04:37 PM

theessentialToolSforStayingConnectedIndistributedTeamSinclude:1)CommunicationToolSlikeZoom,Microsoftteams,Slack和DiscordForeFforeffectiveCommunication; 2)ProjectManagementtoolsSuchastrello,Asana,Asana和JirafortaskManagementAngeandWorkFloworGanization jiralagementtoolssuchastrello,Asana和Jiraforterforloganization;

Laravel的影响:简化网络开发Laravel的影响:简化网络开发Apr 21, 2025 am 12:18 AM

Laravel通过简化Web开发过程和提供强大功能脱颖而出。其优势包括:1)简洁的语法和强大的ORM系统,2)高效的路由和认证系统,3)丰富的第三方库支持,使得开发者能专注于编写优雅的代码并提高开发效率。

Laravel:前端还是后端?澄清框架的角色Laravel:前端还是后端?澄清框架的角色Apr 21, 2025 am 12:17 AM

laravelispredminandermanthandermanthandermanthandermanthermanderframework,设计Forserver-SideLogic,databasemagement,andapideplupment,thryitalsosupportsfortfortsfrontenddevelopmentwithbladeTemplates。

Laravel vs. Python:探索性能和可扩展性Laravel vs. Python:探索性能和可扩展性Apr 21, 2025 am 12:16 AM

Laravel和Python在性能和可扩展性方面的表现各有优劣。Laravel通过异步处理和队列系统提升性能,但受PHP限制在高并发时可能有瓶颈;Python利用异步框架和强大的库生态系统表现出色,但在多线程环境下受GIL影响。

Laravel vs. Python(与框架):比较分析Laravel vs. Python(与框架):比较分析Apr 21, 2025 am 12:15 AM

Laravel适合团队熟悉PHP且需功能丰富的项目,Python框架则视项目需求而定。1.Laravel提供优雅语法和丰富功能,适合需要快速开发和灵活性的项目。2.Django适合复杂应用,因其“电池包含”理念。3.Flask适用于快速原型和小型项目,提供极大灵活性。

Laravel的前端:探索可能性Laravel的前端:探索可能性Apr 20, 2025 am 12:19 AM

Laravel可以用于前端开发。1)使用Blade模板引擎生成HTML。2)集成Vite管理前端资源。3)构建SPA、PWA或静态网站。4)结合路由、中间件和EloquentORM创建完整Web应用。

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

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

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。