|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: yunwuxin <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
namespace think; |
|
12
|
|
|
|
|
13
|
|
|
use Closure; |
|
14
|
|
|
use Exception; |
|
|
|
|
|
|
15
|
|
|
use Throwable; |
|
16
|
|
|
|
|
17
|
|
|
class Pipeline |
|
18
|
|
|
{ |
|
19
|
|
|
protected $passable; |
|
20
|
|
|
|
|
21
|
|
|
protected $pipes = []; |
|
22
|
|
|
|
|
23
|
|
|
protected $exceptionHandler; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
|
|
|
|
|
26
|
|
|
* 初始数据 |
|
27
|
|
|
* @param $passable |
|
|
|
|
|
|
28
|
|
|
* @return $this |
|
29
|
|
|
*/ |
|
30
|
|
|
public function send($passable) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->passable = $passable; |
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
|
|
|
|
|
37
|
|
|
* 调用栈 |
|
38
|
|
|
* @param $pipes |
|
|
|
|
|
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
|
|
public function through($pipes) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->pipes = is_array($pipes) ? $pipes : func_get_args(); |
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* 执行 |
|
49
|
|
|
* @param Closure $destination |
|
|
|
|
|
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
|
|
public function then(Closure $destination) |
|
53
|
|
|
{ |
|
54
|
|
|
$pipeline = array_reduce( |
|
55
|
|
|
array_reverse($this->pipes), |
|
56
|
|
|
$this->carry(), |
|
57
|
|
|
function ($passable) use ($destination) { |
|
58
|
|
|
try { |
|
59
|
|
|
return $destination($passable); |
|
60
|
|
|
} catch (Throwable|Exception $e) { |
|
61
|
|
|
return $this->handleException($passable, $e); |
|
62
|
|
|
} |
|
63
|
|
|
}); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
return $pipeline($this->passable); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* 设置异常处理器 |
|
70
|
|
|
* @param callable $handler |
|
|
|
|
|
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
|
|
public function whenException($handler) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->exceptionHandler = $handler; |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function carry() |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
return function ($stack, $pipe) { |
|
82
|
|
|
return function ($passable) use ($stack, $pipe) { |
|
83
|
|
|
try { |
|
84
|
|
|
return $pipe($passable, $stack); |
|
85
|
|
|
} catch (Throwable|Exception $e) { |
|
86
|
|
|
return $this->handleException($passable, $e); |
|
87
|
|
|
} |
|
88
|
|
|
}; |
|
89
|
|
|
}; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
|
|
|
|
|
93
|
|
|
* 异常处理 |
|
94
|
|
|
* @param $passable |
|
|
|
|
|
|
95
|
|
|
* @param $e |
|
|
|
|
|
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function handleException($passable, Throwable $e) |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->exceptionHandler) { |
|
101
|
|
|
return call_user_func($this->exceptionHandler, $passable, $e); |
|
102
|
|
|
} |
|
103
|
|
|
throw $e; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: