1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-flow |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-flow/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-flow |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFlow; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Node\FileNodeInterface; |
17
|
|
|
use Graze\DataFlow\Flow\Runner\Run; |
18
|
|
|
use Graze\DataNode\NodeInterface; |
19
|
|
|
use Psr\Log\LoggerAwareInterface; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
use Psr\Log\LogLevel; |
22
|
|
|
use ReflectionClass; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Flow Facade |
26
|
|
|
* |
27
|
|
|
* @method static Flow run(FlowInterface ...$flows) |
28
|
|
|
* @method static Flow toAll(FlowInterface ...$flows) |
29
|
|
|
* @method static Flow first(callable $fn = null) |
30
|
|
|
* @method static Flow last(callable $fn = null) |
31
|
|
|
* @method static Flow filter(callable $fn = null) |
32
|
|
|
* @method static Flow map(callback $fn = null) |
33
|
|
|
* @method static Flow each(FlowInterface $flow) |
34
|
|
|
* @method static Flow callback(callable $fn = null) |
35
|
|
|
* @method static Flow makeDirectory($mode = 0777) |
36
|
|
|
* @method static Flow merge(FileNodeInterface $file, array $options = []) |
37
|
|
|
* @method static Flow compress($type, array $options = []) |
38
|
|
|
* @method static Flow decompress(array $options = []) |
39
|
|
|
* @method static Flow gzip() |
40
|
|
|
* @method static Flow gunzip() |
41
|
|
|
* @method static Flow zip() |
42
|
|
|
* @method static Flow unzip() |
43
|
|
|
* @method static Flow copyFile(FileNodeInterface $target); |
44
|
|
|
* @method static Flow copyFiles(FileNodeInterface $target); |
45
|
|
|
* @method static Flow moveFile(FileNodeInterface $target); |
46
|
|
|
* @method static Flow moveFiles(FileNodeInterface $target); |
47
|
|
|
* @method static Flow convertEncoding($newEncoding) |
48
|
|
|
* @method static Flow replaceText($from, $to) |
49
|
|
|
* @method static Flow tail($lines) |
50
|
|
|
* @method static Flow head($lines) |
51
|
|
|
*/ |
52
|
|
|
class Flow extends Run |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
44 |
View Code Duplication |
public function flow(NodeInterface $node) |
|
|
|
|
58
|
|
|
{ |
59
|
44 |
|
$this->log(LogLevel::NOTICE, "Running through {count} flows", ['count' => count($this->items)]); |
60
|
|
|
|
61
|
44 |
|
$current = $node; |
62
|
44 |
|
foreach ($this->items as $flow) { |
63
|
44 |
|
$current = $flow->flow($current); |
64
|
44 |
|
} |
65
|
44 |
|
return $current; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var FlowBuilderInterface |
70
|
|
|
*/ |
71
|
|
|
protected static $builder; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return FlowBuilderInterface |
75
|
|
|
*/ |
76
|
47 |
|
protected static function getBuilder() |
77
|
|
|
{ |
78
|
47 |
|
if (!static::$builder instanceof FlowBuilderInterface) { |
79
|
2 |
|
static::$builder = new Builder(); |
80
|
2 |
|
} |
81
|
47 |
|
return static::$builder; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param FlowBuilderInterface $builder |
86
|
|
|
*/ |
87
|
2 |
|
public static function setBuilder(FlowBuilderInterface $builder) |
88
|
|
|
{ |
89
|
2 |
|
static::$builder = $builder; |
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param LoggerInterface $logger |
94
|
|
|
* |
95
|
|
|
* @return null|void |
96
|
|
|
*/ |
97
|
1 |
|
public static function useLogger(LoggerInterface $logger) |
98
|
|
|
{ |
99
|
1 |
|
$builder = static::getBuilder(); |
100
|
|
|
|
101
|
1 |
|
if ($builder instanceof LoggerAwareInterface) { |
102
|
1 |
|
$builder->setLogger($logger); |
103
|
1 |
|
} |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $namespace |
108
|
|
|
*/ |
109
|
1 |
|
public static function with($namespace) |
110
|
|
|
{ |
111
|
1 |
|
self::getBuilder()->addNamespace($namespace); |
112
|
1 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $flowName |
116
|
|
|
* @param array $arguments |
117
|
|
|
* |
118
|
|
|
* @return Flow |
119
|
|
|
*/ |
120
|
44 |
|
public static function __callStatic($flowName, $arguments) |
121
|
|
|
{ |
122
|
44 |
|
$flow = new static(); |
123
|
44 |
|
return $flow->__call($flowName, $arguments); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $flowSpec |
128
|
|
|
* @param array $arguments |
129
|
|
|
* |
130
|
|
|
* @return Flow |
131
|
|
|
*/ |
132
|
45 |
|
public static function buildFlow($flowSpec, $arguments = []) |
133
|
|
|
{ |
134
|
45 |
|
return static::getBuilder()->buildFlow($flowSpec, $arguments); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $method |
139
|
|
|
* @param array $arguments |
140
|
|
|
* |
141
|
|
|
* @return self |
142
|
|
|
*/ |
143
|
45 |
|
public function __call($method, $arguments) |
144
|
|
|
{ |
145
|
45 |
|
return $this->add(static::buildFlow($method, $arguments)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Create instance validator. |
150
|
|
|
* |
151
|
|
|
* @return Flow |
152
|
|
|
*/ |
153
|
1 |
|
public static function create() |
154
|
|
|
{ |
155
|
1 |
|
$ref = new ReflectionClass(__CLASS__); |
156
|
1 |
|
return $ref->newInstanceArgs(func_get_args()); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.