|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Transfer. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE file located |
|
7
|
|
|
* in the root directory. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Transfer\Procedure; |
|
11
|
|
|
|
|
12
|
|
|
use Transfer\Adapter\CallbackAdapter; |
|
13
|
|
|
use Transfer\Adapter\SourceAdapterInterface; |
|
14
|
|
|
use Transfer\Adapter\TargetAdapterInterface; |
|
15
|
|
|
use Transfer\Adapter\Transaction\Request; |
|
16
|
|
|
use Transfer\Worker\CallbackWorker; |
|
17
|
|
|
use Transfer\Worker\SplitterWorker; |
|
18
|
|
|
use Transfer\Worker\WorkerInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Procedure builder. |
|
22
|
|
|
*/ |
|
23
|
|
|
class ProcedureBuilder |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var array Definitions |
|
27
|
|
|
*/ |
|
28
|
|
|
private $definitions; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string Current definition |
|
32
|
|
|
*/ |
|
33
|
|
|
private $context; |
|
34
|
|
|
|
|
35
|
9 |
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
9 |
|
$this->createProcedure('root'); |
|
38
|
9 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Creates a procedure. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $name Procedure name |
|
44
|
|
|
* |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
9 |
|
public function createProcedure($name) |
|
48
|
|
|
{ |
|
49
|
|
|
$definition = array( |
|
50
|
9 |
|
'parent' => $this->context, |
|
51
|
9 |
|
'name' => $name, |
|
52
|
9 |
|
'sources' => array(), |
|
53
|
9 |
|
'workers' => array(), |
|
54
|
9 |
|
'targets' => array(), |
|
55
|
9 |
|
'children' => array(), |
|
56
|
9 |
|
); |
|
57
|
|
|
|
|
58
|
9 |
|
$this->definitions[$name] = $definition; |
|
59
|
|
|
|
|
60
|
9 |
|
$this->context = $name; |
|
61
|
|
|
|
|
62
|
9 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Builds a procedure object. |
|
67
|
|
|
* |
|
68
|
|
|
* @return Procedure |
|
69
|
|
|
*/ |
|
70
|
9 |
|
public function getProcedure() |
|
71
|
|
|
{ |
|
72
|
9 |
|
return Procedure::createFromDefinition($this->definitions['root']); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Adds a source instruction to a procedure. |
|
77
|
|
|
* |
|
78
|
|
|
* @param SourceAdapterInterface|callable $adapter Source adapter |
|
79
|
|
|
* @param Request $request Request sent to source adapter |
|
80
|
|
|
* |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public function addSource($adapter, Request $request = null) |
|
84
|
|
|
{ |
|
85
|
3 |
|
if ($request === null) { |
|
86
|
3 |
|
$request = new Request(); |
|
87
|
3 |
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
$this->addDefinition( |
|
90
|
3 |
|
'sources', |
|
91
|
3 |
|
array((is_callable($adapter) ? new CallbackAdapter($adapter, null) : $adapter), $request) |
|
92
|
3 |
|
); |
|
93
|
|
|
|
|
94
|
3 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Adds a target instruction to a procedure. |
|
99
|
|
|
* |
|
100
|
|
|
* @param TargetAdapterInterface|callable $adapter Target adapter |
|
101
|
|
|
* |
|
102
|
|
|
* @return $this |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function addTarget($adapter) |
|
105
|
|
|
{ |
|
106
|
3 |
|
$this->addDefinition( |
|
107
|
3 |
|
'targets', |
|
108
|
3 |
|
is_callable($adapter) ? new CallbackAdapter(null, $adapter) : $adapter |
|
109
|
3 |
|
); |
|
110
|
|
|
|
|
111
|
3 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Adds a worker instruction to a procedure. |
|
116
|
|
|
* |
|
117
|
|
|
* @param WorkerInterface|callable $worker Worker |
|
118
|
|
|
* |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
3 |
|
public function addWorker($worker) |
|
122
|
|
|
{ |
|
123
|
3 |
|
$this->addDefinition( |
|
124
|
3 |
|
'workers', |
|
125
|
3 |
|
is_callable($worker) ? new CallbackWorker($worker) : $worker |
|
126
|
3 |
|
); |
|
127
|
|
|
|
|
128
|
3 |
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Pushes the array elements to the local storage. |
|
133
|
|
|
* |
|
134
|
|
|
* @return $this |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function split() |
|
137
|
|
|
{ |
|
138
|
1 |
|
return $this->addWorker(new SplitterWorker()); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Switches current procedure context to parent procedure. |
|
143
|
|
|
* |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
5 |
|
public function end() |
|
147
|
|
|
{ |
|
148
|
5 |
|
$definition = $this->definitions[$this->context]; |
|
149
|
|
|
|
|
150
|
5 |
|
$this->context = $this->definitions[$this->context]['parent']; |
|
151
|
|
|
|
|
152
|
5 |
|
$this->definitions[$this->context]['children'][] = $definition; |
|
153
|
|
|
|
|
154
|
5 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Adds definition. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $type |
|
161
|
|
|
* @param mixed $component |
|
162
|
|
|
*/ |
|
163
|
4 |
|
private function addDefinition($type, $component) |
|
164
|
|
|
{ |
|
165
|
4 |
|
$this->definitions[$this->context][$type][] = $component; |
|
166
|
4 |
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|