装饰器模式构建请求处理管道

来源:互联网 发布:video.js ie8无法使用 编辑:程序博客网 时间:2024/06/05 16:49

1.array_reduce 传值

2.匿名函数返回

3.执行顺序是不断通过reduce迭代遍历的回调


<?php



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//
//PHP中array_reduce()使用
array_reduce — 用回调函数迭代地将数组简化为单一的值
给定一个数组:


$ar = array(1,2,3,4,5);


如果要求得这个数组中各个元素之和。


方法一、


很自然的用foreach实现


$sum = 0;
foreach ($ar as $v) {
    $sum+=$v;
}
echo $sum;
方法二、


我们可以用array_reduce实现。它是专门用来迭代数组的。该函数最多接收三个参数。


第一个参数接收数组


第二个参数函数名,也可以是匿名函数,函数有两个参数,分别代表$result和$item


第三个参数(可选),该参数将被当成是数组中的第一个值来处理,或者如果数组为空的话就作为最终返回值。


复制代码
function leijia($a,$b){
    $a+=$b;
    return $a;
}
$result = array_reduce($ar ,leijia);
echo $result;
复制代码
继续优化,在PHP5.3以上中可以用匿名函数了。使代码更精简。


$result = array_reduce($ar ,function($a,$b){
    $a+=$b;
    return $a;
});
//装饰器模式
interface Decorator {


    public function display();
}


class Xiaofang implements Decorator {


    private $name;


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


    public function display() {
        echo '我是' . $this->name . '我出门了';
    }


}


class Finery implements Decorator {


    private $component;


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


    public function display() {
        $this->component->display();
    }


}


class Shoes extends Finery {


    public function display() {
        echo '穿上鞋子';
        parent::display();
    }


}


class Skirt extends Finery {


    public function display() {
        echo '穿上裙子';
        parent::display();
    }


}


class Fire extends Finery {


    public function display() {
        echo '出门前先整理头发';
        parent::display();
        echo '出门后再整理一下头发';
    }


}


$xiaofang = new Xiaofang('小芳');
$shoes = new Shoes($xiaofang);
$skirt = new Skirt($shoes);
$fire = new Fire($skirt);
$fire->display();




//利用回调函数实现装饰器模式
//array_reduce 遍历,同时使用回调函数给出返回值
interface Middleware {


    public static function handle(Closure $next);
}


class VerifyCsrfToken implements Middleware {


    public static function handle(Closure $next) {
        echo '验证csrf-token<br>';
        $next();
    }


}


class ShareErrorFromSession implements Middleware {


    public static function handle(Closure $next) {
        echo 'session 中有error变量,共享它<br>';
        $next();
    }


}


class StartSession implements Middleware {


    public static function handle(Closure $next) {
        echo '开启session<br>';
        $next();
        echo '保存数据 关闭session<br>';
    }


}


Class AddQueueCookiesToResponse implements Middleware {


    public static function handle(Closure $next) {
        $next();
        echo '添加下一次请求需要的cookie<br>';
    }


}


class EncryptCookie implements Middleware {


    public static function handle(Closure $next) {
        echo '对输入请求的cookie解密<br>';
        $next();
        echo '对输出响应的cookie加密<br>';
    }


}


class CheckForMaintenaceMode implements Middleware {


    public static function handle(Closure $next) {
        echo '确定当前程序是否处于可维护状态<br>';
        $next();
    }


}


function getSlice() {
    return function ($stack, $pipe) {
        return function() use($stack, $pipe) {
            echo '<pre>';
            
                    echo $pipe;
            print_r($stack);
    
            return $pipe::handle($stack);
            //CheckForMaintenaceMode::handle($firstSlice);
        };
    };
}


function then() {
    $pipes = [
        'CheckForMaintenaceMode',
        'EncryptCookie',
        'AddQueueCookiesToResponse',
        'StartSession',
        'ShareErrorFromSession',
        'VerifyCsrfToken',
    ];


    $firstSlice = function() {
        echo '请求向路由传递,返回响应<br>';
    };


    $pipes = array_reverse($pipes);
    call_user_func(array_reduce($pipes, getSlice(), $firstSlice));
}


then();
原创粉丝点击