1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* See class comment |
4
|
|
|
* |
5
|
|
|
* PHP Version 5 |
6
|
|
|
* |
7
|
|
|
* @category Netresearch |
8
|
|
|
* @package Netresearch\Kite |
9
|
|
|
* @subpackage Service |
10
|
|
|
* @author Christian Opitz <[email protected]> |
11
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
12
|
|
|
* @link http://www.netresearch.de |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Netresearch\Kite\Service; |
16
|
|
|
|
17
|
|
|
use Netresearch\Kite\Job; |
18
|
|
|
use Netresearch\Kite\Task; |
19
|
|
|
use Netresearch\Kite\Workflow; |
20
|
|
|
use Netresearch\Kite\Variables; |
21
|
|
|
use Netresearch\Kite\Exception; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Task factory |
25
|
|
|
* |
26
|
|
|
* @category Netresearch |
27
|
|
|
* @package Netresearch\Kite |
28
|
|
|
* @subpackage Service |
29
|
|
|
* @author Christian Opitz <[email protected]> |
30
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
31
|
|
|
* @link http://www.netresearch.de |
32
|
|
|
*/ |
33
|
|
|
class Factory |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var Console |
37
|
|
|
*/ |
38
|
|
|
protected $console; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $namespaces = [ |
44
|
|
|
'task' => [ |
45
|
|
|
'Netresearch\Kite\Task' |
46
|
|
|
], |
47
|
|
|
'workflow' => [ |
48
|
|
|
'Netresearch\Kite\Workflow' |
49
|
|
|
] |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Construct factory |
54
|
|
|
* |
55
|
|
|
* @param Console $console Optional console (passed to Job) |
56
|
|
|
*/ |
57
|
|
|
public function __construct(Console $console) |
58
|
|
|
{ |
59
|
|
|
$this->console = $console; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create a job |
64
|
|
|
* |
65
|
|
|
* @param string $job The job name |
66
|
|
|
* |
67
|
|
|
* @return Job |
68
|
|
|
*/ |
69
|
|
|
public function createJob($job) |
70
|
|
|
{ |
71
|
|
|
$jobInstance = new Job($this->console); |
72
|
|
|
return $jobInstance->setFromArray($this->console->getConfig()->getJobConfiguration($job)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create/validate a workflow |
77
|
|
|
* |
78
|
|
|
* @param string|Workflow $workflow The workflow definition |
79
|
|
|
* @param Variables $variables Parent object for the workflow |
80
|
|
|
* |
81
|
|
|
* @return Workflow |
82
|
|
|
*/ |
83
|
|
|
public function createWorkflow($workflow, Variables $variables) |
84
|
|
|
{ |
85
|
|
|
if (is_string($workflow)) { |
86
|
|
|
$className = $this->getWorkflowClassName($workflow); |
87
|
|
|
$workflow = new $className($variables); |
88
|
|
|
} |
89
|
|
|
if (!$workflow instanceof Workflow) { |
90
|
|
|
throw new Exception('Workflow must extend \Netresearch\Kite\Domain\Model\Workflow'); |
91
|
|
|
} |
92
|
|
|
return $workflow; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create a task |
97
|
|
|
* |
98
|
|
|
* @param string|array|callable $task The task name, it's properies or a callable |
99
|
|
|
* @param Variables $variables The parent variables object for the task |
100
|
|
|
* @param array $options Options for the task |
101
|
|
|
* |
102
|
|
|
* @return \Netresearch\Kite\Task |
103
|
|
|
*/ |
104
|
|
|
public function createTask($task, Variables $variables, array $options = array()) |
105
|
|
|
{ |
106
|
|
|
if (is_array($task)) { |
107
|
|
|
if (count($task) === 2 && array_key_exists(0, $task) && array_key_exists(1, $task) && is_callable($task)) { |
108
|
|
|
$options['callback'] = $task; |
109
|
|
|
$task = 'callback'; |
110
|
|
|
} else { |
111
|
|
|
$options += $task; |
112
|
|
|
if (array_key_exists('workflow', $options)) { |
113
|
|
|
unset($options['workflow']); |
114
|
|
|
$task = $this->createWorkflow($task['workflow'], $variables); |
115
|
|
|
} else { |
116
|
|
|
unset($options['type']); |
117
|
|
|
$task = array_key_exists('type', $task) ? $task['type'] : 'sub'; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} elseif ($task instanceof \Closure) { |
121
|
|
|
$options['callback'] = $task; |
122
|
|
|
$task = 'callback'; |
123
|
|
|
} |
124
|
|
|
if (is_string($task)) { |
125
|
|
|
$className = $this->getTaskClassName($task); |
126
|
|
|
$task = new $className($variables); |
127
|
|
|
} |
128
|
|
|
if ($task instanceof Task) { |
129
|
|
|
$task->setFromArray($options); |
130
|
|
|
} else { |
131
|
|
|
throw new Exception('Invalid task definition'); |
132
|
|
|
} |
133
|
|
|
return $task; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the full class name from a task definition |
138
|
|
|
* |
139
|
|
|
* @param string $definition Last part of class name without postfix or full class name |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getTaskClassName($definition) |
144
|
|
|
{ |
145
|
|
|
return $this->getClassName($definition, 'task'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the full class name from a workflow definition |
150
|
|
|
* |
151
|
|
|
* @param string $definition Last part of class name without postfix or full class name |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function getWorkflowClassName($definition) |
156
|
|
|
{ |
157
|
|
|
return $this->getClassName($definition, 'workflow', false); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the full class name from a definition |
162
|
|
|
* |
163
|
|
|
* @param string $definition Last part of class name without postfix or full class name |
164
|
|
|
* @param string $type "workflow" or "task" currently |
165
|
|
|
* @param bool $postfixType Whether $type is required as postfix at the class name |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
protected function getClassName($definition, $type, $postfixType = true) |
170
|
|
|
{ |
171
|
|
|
$ucType = ucfirst($type); |
172
|
|
|
if (!strpos($definition, '\\')) { |
173
|
|
|
$taskClass = str_replace(' ', '\\', ucwords(str_replace('-', ' ', $definition))); |
174
|
|
|
$taskClass = $taskClass . ($postfixType ? $ucType : ''); |
175
|
|
|
foreach ($this->namespaces[$type] as $namespace) { |
176
|
|
|
$potentialClass = '\\' . $namespace . '\\' . $taskClass; |
177
|
|
|
if (class_exists($potentialClass)) { |
178
|
|
|
return $potentialClass; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} elseif (!is_subclass_of(ltrim($definition, '\\'), 'Netresearch\\Kite\\' . $ucType)) { |
182
|
|
|
throw new Exception($definition .' must extend Netresearch\\Kite\\' . $ucType); |
183
|
|
|
} |
184
|
|
|
return $definition; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the namespaces - either for a specific type or all |
189
|
|
|
* |
190
|
|
|
* @param string|null $type workflow or tasks |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
|
|
public function getNamespaces($type = null) |
195
|
|
|
{ |
196
|
|
|
return $type ? $this->namespaces[$type] : $this->namespaces; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
?> |
200
|
|
|
|