1 | <?php |
||
2 | // +---------------------------------------------------------------------- |
||
3 | // | ThinkPHP [ WE CAN DO IT JUST THINK ] |
||
4 | // +---------------------------------------------------------------------- |
||
5 | // | Copyright (c) 2006~2021 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; |
||
0 ignored issues
–
show
|
|||
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 | 39 | public function send($passable) |
|
31 | { |
||
32 | 39 | $this->passable = $passable; |
|
33 | 39 | return $this; |
|
34 | } |
||
35 | |||
36 | /** |
||
37 | * 调用栈 |
||
38 | * @param $pipes |
||
39 | * @return $this |
||
40 | */ |
||
41 | 39 | public function through($pipes) |
|
42 | { |
||
43 | 39 | $this->pipes = is_array($pipes) ? $pipes : func_get_args(); |
|
44 | 39 | return $this; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * 执行 |
||
49 | * @param Closure $destination |
||
50 | * @return mixed |
||
51 | */ |
||
52 | 39 | public function then(Closure $destination) |
|
53 | { |
||
54 | 39 | $pipeline = array_reduce( |
|
55 | 39 | array_reverse($this->pipes), |
|
56 | 39 | $this->carry(), |
|
57 | function ($passable) use ($destination) { |
||
58 | try { |
||
59 | 39 | return $destination($passable); |
|
60 | 3 | } catch (Throwable | Exception $e) { |
|
61 | 3 | return $this->handleException($passable, $e); |
|
62 | } |
||
63 | 39 | } |
|
64 | ); |
||
65 | |||
66 | 39 | return $pipeline($this->passable); |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * 设置异常处理器 |
||
71 | * @param callable $handler |
||
72 | * @return $this |
||
73 | */ |
||
74 | 39 | public function whenException($handler) |
|
75 | { |
||
76 | 39 | $this->exceptionHandler = $handler; |
|
77 | 39 | return $this; |
|
78 | } |
||
79 | |||
80 | 39 | protected function carry() |
|
81 | { |
||
82 | return function ($stack, $pipe) { |
||
83 | return function ($passable) use ($stack, $pipe) { |
||
84 | try { |
||
85 | 9 | return $pipe($passable, $stack); |
|
86 | 3 | } catch (Throwable | Exception $e) { |
|
87 | 3 | return $this->handleException($passable, $e); |
|
88 | } |
||
89 | 9 | }; |
|
90 | 39 | }; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * 异常处理 |
||
95 | * @param $passable |
||
96 | * @param $e |
||
97 | * @return mixed |
||
98 | */ |
||
99 | 3 | protected function handleException($passable, Throwable $e) |
|
100 | { |
||
101 | 3 | if ($this->exceptionHandler) { |
|
102 | 3 | return call_user_func($this->exceptionHandler, $passable, $e); |
|
103 | } |
||
104 | throw $e; |
||
105 | } |
||
106 | |||
107 | } |
||
108 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: