This repository was archived by the owner on Jan 23, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +115
-25
lines changed
Expand file tree Collapse file tree 3 files changed +115
-25
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: inhere
5+ * Date: 2017/10/16
6+ * Time: 下午11:59
7+ * @link https://github.com/ztsu/pipe/blob/master/src/Pipeline.php
8+ */
9+
10+ namespace Inhere \Library \Components ;
11+
12+ use Inhere \Library \Interfaces \PipelineInterface ;
13+
14+ /**
15+ * Class Pipeline
16+ * @package Inhere\Library\Components
17+ */
18+ class Pipeline implements PipelineInterface
19+ {
20+ /** @var \SplObjectStorage */
21+ private $ stages ;
22+
23+ public function __construct ()
24+ {
25+ $ this ->stages = new \SplObjectStorage ;
26+ }
27+
28+ /**
29+ * {@inheritdoc}
30+ */
31+ public function add (callable $ stage )
32+ {
33+ if ($ stage instanceof $ this ) {
34+ $ stage ->add (function ($ payload ) {
35+ return $ this ->invokeStage ($ payload );
36+ });
37+ }
38+
39+ $ this ->stages ->attach ($ stage );
40+
41+ return $ this ;
42+ }
43+
44+ /**
45+ * {@inheritdoc}
46+ */
47+ public function run ($ payload )
48+ {
49+ $ this ->stages ->rewind ();
50+
51+ return $ this ->invokeStage ($ payload );
52+ }
53+
54+ /**
55+ * {@inheritdoc}
56+ */
57+ public function __invoke ($ payload )
58+ {
59+ return $ this ->run ($ payload );
60+ }
61+
62+ private function invokeStage ($ payload )
63+ {
64+ $ stage = $ this ->stages ->current ();
65+ $ this ->stages ->next ();
66+
67+ if (is_callable ($ stage )) {
68+ return $ stage ($ payload , function ($ payload ) {
69+ return $ this ->invokeStage ($ payload );
70+ });
71+ }
72+
73+ return $ payload ;
74+ }
75+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: inhere
5+ * Date: 2017/10/17
6+ * Time: 上午12:01
7+ */
8+
9+ namespace Inhere \Library \Interfaces ;
10+
11+ /**
12+ * Interface PipelineInterface
13+ * @package Inhere\Library\Interfaces
14+ */
15+ interface PipelineInterface
16+ {
17+ /**
18+ * Adds stage to the pipelene
19+ *
20+ * @param callable $stage
21+ * @return $this
22+ */
23+ public function add (callable $ stage );
24+
25+ /**
26+ * Runs pipeline with initial value
27+ *
28+ * @param mixed $payload
29+ * @return mixed
30+ */
31+ public function run ($ payload );
32+
33+ /**
34+ * Makes pipeline callable. Does same as {@see run()}
35+ *
36+ * @param mixed $payload
37+ * @return mixed
38+ */
39+ public function __invoke ($ payload );
40+ }
You can’t perform that action at this time.
0 commit comments