美文网首页
手写一个laravel(九) 中间件

手写一个laravel(九) 中间件

作者: mafa1993 | 来源:发表于2020-12-14 20:40 被阅读0次

手写一个laravel(九) 中间件

  1. 中间件本质即为闭包
  2. 使用管道实现,核心为array_reduce函数,因为array_reduce可以将返回值传给下次执行,第一个中间件执行完的结果返回以后可以作为第二个中间件的输入
  3. 在app/Htpp/Middlewares下创建两个中间件类,用于测试
  4. 在/app/http下的kernel.php对中间件类进行注册
  5. 在src下创建建Pipline类
  6. 将kernel中的路由分发改为由pipline实现

源码见个人git https://github.com/mafa1993/slaravel

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/23 0023
 * Time: 20:54
 */

//用于中间件测试

class MID1
{
    public function handle(Closure $next){
        echo '中间件1'.PHP_EOL;

        $next();
        echo '中间件1 end';
    }
}

class MID2
{
    public function handle(Closure $next){
        echo '中间件2'.PHP_EOL;

        $next();
        echo '中间件2 end';
    }
}


class con
{
    public function index(){
        echo 'con 方法';
    }
}

class Pipeline
{
    //用来保存需要执行的中间件
    protected $pipes = [
        MID1::class,
        MID2::class
    ];

    public function then(Closure $des){
        return array_reduce($this->pipes,function ($res,$pipe){
            return function ()use($res,$pipe){
                return $pipe::handle($res);
            };
        },$des);
    }
}

$con = function (){
  (new con)->index();
};
$pi = new Pipeline();
($pi->then($con))();

/**
 * des         res      pip
 * con函数
 *
 */

(function (){
    ECHO '中间2';
    function (){
        echo '中间1';
        $con();
        echo '中间1 end';;
    }
    ECHO '中间2 end';
})();

pipline类

<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/28 0028
 * Time: 16:39
 */

namespace Slaravel\Pipline;

class Pipline
{

    protected $pipes;
    protected $passable;
    protected $app;
    protected $method = 'handle';

    public function __construct($app)
    {
        $this->app = $app;
    }

    public function then(\Closure $closure){
        $res = array_reduce(
            $this->pipes,  //所有需要执行的中间件
            $this->carry(),
            $closure
        );
        return $res($this->passable);
    }

    public function carry(){
        return function ($stack,$pipe){
            return function ($passable)use($stack,$pipe){
                if(is_callable($pipe)){
                    return $pipe($passable,$stack);
                }elseif(!is_object($pipe)){
                    //类先实例化
                    $pipe = $this->app->make($pipe);
                    $params = [$passable,$stack];
                }
                return method_exists($pipe,$this->method) ? $pipe->{$this->method}(...$params) : $pipe(...$params);  //默认执行中间件的handle方法

            };
        };
    }

/*array_reduce(
$this->pipes,  //所有需要执行的中间件
    function ($passable)use($stack,$pipe){

                return $pipe($passable,$stack);
             //默认执行中间件的handle方法

        ;
    },
$closure
);
    1. $res = mid1::hadle($passable,$con)
    2. $res = mid2::hadle($passable,$res())*/


    /**
     * 用来获取中间件
     * @param $pipes
     * @return object
     */
    public function through($pipes){
        $this->pipes = $pipes;
        return $this;
    }

    /**
     * @param $passable
     * @return object
     */
    public function send($passable){
        $this->passable = $passable;
        return $this;
    }
}

相关文章

  • 手写一个laravel(九) 中间件

    手写一个laravel(九) 中间件 中间件本质即为闭包 使用管道实现,核心为array_reduce函数,因为a...

  • laravel Pipeline 原理的详细解析

    laravel 中间件使用了 Pipeline vendor\laravel\framework\src\Illu...

  • Laravel学习笔记-中间件

    Laravel 中间件是什么? 简而言之,中间件在 laravel 中的作用就是过滤 HTTP 请求,根据不同的请...

  • [教程] 大白话 Laravel 中间件

    文章转自:https://learnku.com/laravel/t/27426 Laravel 中间件是什么? ...

  • Laravel 文档阅读:中间件

    简介 中间件用来过滤项目中的 HTTP 请求,实际上 Laravel 项目中大量使用了中间件。例如,Laravel...

  • 64. 中间件

    Laravel 中间件提供了一种方便的机制来过滤进入应用的 HTTP 请求。 Laravel 自带了一些中间件,包...

  • 翻译

    Laravel 的路由中间件 简介 创建中间件 注册中间件全局中间件为路由指定中间件中间件组 中间件参数 Term...

  • Laravel 中的面试题(一)

    1、什么是http中间件? HTTP中间件是一种用于过滤HTTP请求的技术。Laravel包含一个中间件,用 于检...

  • 记录一次解决Vue跨域请求接口404问题

    前端使用VUE接口使用 laravel服务器用的是宝塔 1.修改laravel的cors,添加一个cors的中间件...

  • Laravel中间件

    在Laravel有两种类型的中间件:全局中间件、路由中间件$middleware属性用于注册全局中间件,$rout...

网友评论

      本文标题:手写一个laravel(九) 中间件

      本文链接:https://www.haomeiwen.com/subject/prsbgktx.html