激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼

PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼

2020-05-20 14:31傻夢獸0 PHP教程

這篇文章主要介紹了PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Pipeline 設(shè)計(jì)模式

水管太長,只要有一處破了,就會漏水了,而且不利于復(fù)雜環(huán)境彎曲轉(zhuǎn)折使用。所以我們都會把水管分成很短的一節(jié)一節(jié)管道,然后最大化的讓管道大小作用不同,因地制宜,組裝在一起,滿足各種各樣的不同需求。

由此得出 Pipeline 的設(shè)計(jì)模式,就是將復(fù)雜冗長的流程 (processes) 截成各個(gè)小流程,小任務(wù)。每個(gè)最小量化的任務(wù)就可以復(fù)用,通過組裝不同的小任務(wù),構(gòu)成復(fù)雜多樣的流程 (processes)。

最后將「輸入」引入管道,根據(jù)每個(gè)小任務(wù)對輸入進(jìn)行操作 (加工、過濾),最后輸出滿足需要的結(jié)果。

你可以拿koa的中間件機(jī)制來做參考 ,也就是我們常說的削洋蔥思路

PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼

在前端里早期有一個(gè)工程打包工具gulp寫法就更能體現(xiàn)pipeline

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
gulp.task('css', function(){
 return gulp.src('client/templates/*.less')
  .pipe(less())
  .pipe(minifyCSS())
  .pipe(gulp.dest('build/css'))
});
 
gulp.task('js', function(){
 return gulp.src('client/javascript/*.js')
  .pipe(sourcemaps.init())
  .pipe(concat('app.min.js'))
  .pipe(sourcemaps.write())
  .pipe(gulp.dest('build/js'))
});
 
gulp.task('default', [ 'html', 'css', 'js' ]);

IlluminatePipeline

Laravel 框架中的中間件,就是利用 Illuminate\Pipeline 來實(shí)現(xiàn)的,本來想寫寫我對 「Laravel 中間件」源碼的解讀,但發(fā)現(xiàn)網(wǎng)上已經(jīng)有很多帖子都有表述了,所以本文就簡單說說如何使用 Illuminate\Pipeline

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public function demo(Request $request)
{
  $pipe1 = function ($payload, Closure $next) {
    $payload = $payload + 1;
    return $next($payload);
  };
 
  $pipe2 = function ($payload, Closure $next) {
    $payload = $payload * 3;
    return $next($payload);
  };
 
  $data = $request->input('data', 0);
 
  $pipeline = new Pipeline();
 
  return $pipeline
    ->send($data)
    ->through([$pipe1, $pipe2])
    ->then(function ($data) {
      return $data;
    });
}

今天主要學(xué)習(xí)學(xué)習(xí)「Pipeline」,順便推薦一個(gè) PHP 插件:league/pipeline

?
1
composer require league/pipeline

使用起來也很方便

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use League\Pipeline\Pipeline;
 
class TimesTwoStage
{
  public function __invoke($payload)
  {
    return $payload * 2;
  }
}
 
class AddOneStage
{
  public function __invoke($payload)
  {
    return $payload + 1;
  }
}
 
$pipeline = (new Pipeline)
  ->pipe(new TimesTwoStage)
  ->pipe(new AddOneStage);
 
// Returns 21
$pipeline->process(10);

接下來我們添加FastRouter在我的項(xiàng)目中使用。

PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼

上面的代碼修改成這樣

PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼

我們接下來看看 RespondJson 里做了什么.

?
1
2
3
4
5
6
7
8
9
10
<?php
namespace Platapps\Middlewares;
class RespondJson
{
  public function __invoke($payload)
  {
    header('Content-type:text/json');
    return $payload;
  }
}

就簡單的加了個(gè) header

我們試試把注釋到一個(gè)渠道

PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼

我們再次訪問的時(shí)候就變成

PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼

當(dāng)然這是很簡單的中間件,這種中間件遠(yuǎn)遠(yuǎn)不夠,這里是核心代碼,可以去這里看看,也比較簡單。

我們最終需要修改pipe這個(gè)方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
namespace League\Pipeline;
 
class Pipeline implements PipelineInterface
{
  /**
   * @var callable[]
   */
  private $stages = [];
 
  /**
   * @var ProcessorInterface
   */
  private $processor;
 
  public function __construct(ProcessorInterface $processor = null, callable ...$stages)
  {
    $this->processor = $processor ?? new FingersCrossedProcessor;
    $this->stages = $stages;
  }
 
  public function pipe(callable $stage): PipelineInterface
  {
    $pipeline = clone $this;
    $pipeline->stages[] = $stage;
 
    return $pipeline;
  }
 
  public function process($payload)
  {
    return $this->processor->process($payload, ...$this->stages);
  }
 
  public function __invoke($payload)
  {
    return $this->process($payload);
  }
}

這么多框架里面我這里建議拿Tp6的來做參考,功能還算夠用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <[email protected]>
// +----------------------------------------------------------------------
namespace think;
 
use Closure;
use Exception;
use Throwable;
 
class Pipeline
{
  protected $passable;
 
  protected $pipes = [];
 
  protected $exceptionHandler;
 
  /**
   * 初始數(shù)據(jù)
   * @param $passable
   * @return $this
   */
  public function send($passable)
  {
    $this->passable = $passable;
    return $this;
  }
 
  /**
   * 調(diào)用棧
   * @param $pipes
   * @return $this
   */
  public function through($pipes)
  {
    $this->pipes = is_array($pipes) ? $pipes : func_get_args();
    return $this;
  }
 
  /**
   * 執(zhí)行
   * @param Closure $destination
   * @return mixed
   */
  public function then(Closure $destination)
  {
    $pipeline = array_reduce(
      array_reverse($this->pipes),
      $this->carry(),
      function ($passable) use ($destination) {
        try {
          return $destination($passable);
        } catch (Throwable | Exception $e) {
          return $this->handleException($passable, $e);
        }
      });
 
    return $pipeline($this->passable);
  }
 
  /**
   * 設(shè)置異常處理器
   * @param callable $handler
   * @return $this
   */
  public function whenException($handler)
  {
    $this->exceptionHandler = $handler;
    return $this;
  }
 
  protected function carry()
  {
    return function ($stack, $pipe) {
      return function ($passable) use ($stack, $pipe) {
        try {
          return $pipe($passable, $stack);
        } catch (Throwable | Exception $e) {
          return $this->handleException($passable, $e);
        }
      };
    };
  }
 
  /**
   * 異常處理
   * @param $passable
   * @param $e
   * @return mixed
   */
  protected function handleException($passable, Throwable $e)
  {
    if ($this->exceptionHandler) {
      return call_user_func($this->exceptionHandler, $passable, $e);
    }
    throw $e;
  }
}

這種寫法有什么好?

其實(shí)就好就好在,你在處理一個(gè)請求的過程中,分配任務(wù)的時(shí)候,在處理的過程,每個(gè)中間的人,只要做自己處理的請求和結(jié)果還有請求即可。讓當(dāng)數(shù)據(jù)到達(dá)Controller里的時(shí)候,顯示業(yè)務(wù)邏輯的時(shí)候更加強(qiáng)大

到此這篇關(guān)于PHP Pipeline 實(shí)現(xiàn)中間件的示例代碼的文章就介紹到這了,更多相關(guān)PHP Pipeline 中間件內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://segmentfault.com/a/1190000022443612

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天堂在线中文资源 | 国产在线欧美 | 精品一区二区三区中文字幕老牛 | 国产精品视频在线免费观看 | 亚洲视频欧美 | 国产精品久久久久久久久久东京 | 成人免费在线视频 | 久久久av亚洲男天堂 | 亚洲视频在线免费看 | 久久久久国产成人精品亚洲午夜 | 一起草av在线 | hd极品free性xxx一护士 | 92自拍视频 | 亚洲精品一区国产精品丝瓜 | 久久久国产一级片 | 国产欧美日韩在线不卡第一页 | 国产精品免费观看视频 | 精品国产一区二 | 日韩毛片一区二区三区 | 国产精品99精品 | videos真实高潮xxxx | 中文字幕亚洲视频 | 夏目友人帐第七季第一集 | 国产污网站在线观看 | a视频在线看 | 日韩精品99久久久久久 | 99日韩精品视频 | 青青青在线免费 | 一级大黄毛片 | 毛片国产 | 免费看黄色一级片 | 国产精品自拍99 | 国产噜噜噜噜久久久久久久久 | 欧美乱淫 | 最新一区二区三区 | 一区二区三区日韩在线观看 | 欧美性黄 | 成人毛片在线免费观看 | 在线播放亚洲精品 | 欧美视频在线一区二区三区 | 中文字幕h|