laravel 利用pipe管道解耦逻辑代码

7 篇文章 0 订阅

前言

在保存数据库前,我们需要对某些字段做处理,如下

1. 对一些特殊字符串过滤掉 如 * \ /等 

2. 对js的脚本代码过滤掉

下面简单的例子

/*
过滤特殊字符串
*/
class RemoveWord
{
    public function handle($content, Closure $next)
    {
        $content = str_replace(['*', '.'] , '', $content);
        return $next($content);
    }
}

/*
删除js脚本
*/
class RemoveScript
{
    public function handle($content, Closure $next)
    {
        return $next(strip_tags($content));
    }
}

$data = (new PipeLine)->send('<script>alert("\\\你好啊*!!")')  //要被处理的字符串
    ->through([
        RemoveWord::class,
        RemoveScript::class
    ])
    ->then(function($content) {
        //保存数据库
        return $content;
    });


/*
模拟array_reduce执行原理的方法
*/
function custom_array_reduce($arr, callable $callback, $init = null)
{
    $ret = $init;

    foreach ($arr as $item) {
        $ret = $callback($ret, $item);
    }
    return $ret;
}

/*
自个儿写的类似laravel的管道类
*/
class PipeLine
{
    protected $method = 'handle';

    public $passable;
    public function send($passable)
    {
        $this->passable = $passable;

        return $this;
    }
    public function through($pipes)
    {
        $this->pipes = is_array($pipes) ? $pipes : func_get_args();

        return $this;
    }
    public function then($destination)
    {
        $pipeline = custom_array_reduce(
            array_reverse($this->pipes), $this->carry(), $this->prepareDestination($destination)
        );
        return $pipeline($this->passable);
    }

    public function carry()
    {
        return function ($stack, $pipe) {
            return function ($passable) use ($stack, $pipe) {
                    if (is_callable($pipe)) {
                        // If the pipe is a callable, then we will call it directly, but otherwise we
                        // will resolve the pipes out of the dependency container and call it with
                        // the appropriate method and arguments, returning the results back out.
                        return $pipe($passable, $stack);
                    } elseif (! is_object($pipe)) {
                        list($name, $parameters) = $this->parsePipeString($pipe);
                        $pipe = new $pipe;
                        $parameters = array_merge([$passable, $stack], $parameters);

                    } else {
                        // If the pipe is already an object we'll just make a callable and pass it to
                        // the pipe as-is. There is no need to do any extra parsing and formatting
                        // since the object we're given was already a fully instantiated object.
                        $parameters = [$passable, $stack];
                    }

                    $carry = method_exists($pipe, $this->method)
                        ? $pipe->{$this->method}(...$parameters)
                        : $pipe(...$parameters);

                    return $carry;
            };
        };
    }

    /**
     * Get the final piece of the Closure onion.
     *
     * @param  \Closure  $destination
     * @return \Closure
     */
    protected function prepareDestination(Closure $destination)
    {
        return function ($passable) use ($destination) {
            return $destination($passable);
        };
    }

    /**
     * Parse full pipe string to get name and parameters.
     *
     * @param  string  $pipe
     * @return array
     */
    protected function parsePipeString($pipe)
    {
        list($name, $parameters)= array_pad(explode(':', $pipe, 2), 2, []);

        if (is_string($parameters)) {
            $parameters = explode(',', $parameters);
        }

        return [$name, $parameters];
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值