|
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\Helper\OptionalLoggerTrait; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
19
|
|
|
use Psr\Log\LogLevel; |
|
20
|
|
|
use ReflectionClass; |
|
21
|
|
|
|
|
22
|
|
|
class Builder implements FlowBuilderInterface, LoggerAwareInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use OptionalLoggerTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string[] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $namespaces = [ |
|
30
|
|
|
'', // allow Fully qualified class names to be built |
|
31
|
|
|
'Graze\\DataFlow\\Flow\\', |
|
32
|
|
|
'Graze\\DataFlow\\Flow\\Collection\\', |
|
33
|
|
|
'Graze\\DataFlow\\Flow\\File\\', |
|
34
|
|
|
'Graze\\DataFlow\\Flow\\File\\Compression\\', |
|
35
|
|
|
'Graze\\DataFlow\\Flow\\Runner\\', |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Add a namespace to check for flow names within |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $namespace |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function addNamespace($namespace) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->namespaces[] = $namespace; |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return string[] |
|
52
|
|
|
*/ |
|
53
|
50 |
|
public function getNamespaces() |
|
54
|
|
|
{ |
|
55
|
50 |
|
return $this->namespaces; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Create a new Flow based on a name |
|
60
|
|
|
* |
|
61
|
|
|
* @param mixed $flowName |
|
62
|
|
|
* @param array $arguments |
|
63
|
|
|
* |
|
64
|
|
|
* @return FlowInterface |
|
65
|
|
|
*/ |
|
66
|
51 |
|
public function buildFlow($flowName, array $arguments = []) |
|
67
|
|
|
{ |
|
68
|
51 |
|
if ($flowName instanceof FlowInterface) { |
|
69
|
1 |
|
return $flowName; |
|
70
|
|
|
} |
|
71
|
50 |
|
foreach ($this->getNamespaces() as $namespace) { |
|
72
|
50 |
|
$className = $namespace . ucfirst($flowName); |
|
73
|
50 |
|
if (!class_exists($className)) { |
|
74
|
50 |
|
continue; |
|
75
|
|
|
} |
|
76
|
49 |
|
$reflection = new ReflectionClass($className); |
|
77
|
49 |
|
if (!$reflection->isSubclassOf(FlowInterface::class)) { |
|
78
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
79
|
1 |
|
"'%s' from flowName: '%s' is not a valid DataFlow", |
|
80
|
1 |
|
$className, |
|
81
|
|
|
$flowName |
|
82
|
1 |
|
)); |
|
83
|
|
|
} |
|
84
|
48 |
|
$this->log(LogLevel::DEBUG, "Building flow: {class}", ['class' => $className]); |
|
85
|
48 |
|
$flow = $reflection->newInstanceArgs($arguments); |
|
86
|
48 |
|
if ($this->logger && ($flow instanceof LoggerAwareInterface)) { |
|
87
|
1 |
|
$flow->setLogger($this->logger); |
|
88
|
1 |
|
} |
|
89
|
48 |
|
return $flow; |
|
90
|
1 |
|
} |
|
91
|
1 |
|
throw new InvalidArgumentException(sprintf("'%s' is not a valid flow name", $flowName)); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|