Completed
Push — 6.0 ( 9db11b...892b87 )
by yun
06:42
created

Pipeline::through()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, think\Exception. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Throwable;
16
17
class Pipeline
18
{
19
    protected $passable;
20
21
    protected $pipes = [];
22
23
    protected $exceptionHandler;
24
25
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $passable should have a doc-comment as per coding-style.
Loading history...
26
     * 初始数据
27
     * @param $passable
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
28
     * @return $this
29
     */
30
    public function send($passable)
31
    {
32
        $this->passable = $passable;
33
        return $this;
34
    }
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $pipes should have a doc-comment as per coding-style.
Loading history...
37
     * 调用栈
38
     * @param $pipes
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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
            });
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
64
65
        return $pipeline($this->passable);
66
    }
67
68
    /**
69
     * 设置异常处理器
70
     * @param callable $handler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
71
     * @return $this
72
     */
73
    public function whenException($handler)
74
    {
75
        $this->exceptionHandler = $handler;
76
        return $this;
77
    }
78
79
    protected function carry()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function carry()
Loading history...
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
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $passable should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $e should have a doc-comment as per coding-style.
Loading history...
93
     * 异常处理
94
     * @param $passable
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
95
     * @param $e
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
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