1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Junty |
4
|
|
|
* |
5
|
|
|
* @author Gabriel Jacinto aka. GabrielJMJ <[email protected]> |
6
|
|
|
* @license MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Junty\TaskRunner\Runner; |
10
|
|
|
|
11
|
|
|
use Junty\TaskRunner\Runner\RunnerInterface; |
12
|
|
|
use Junty\TaskRunner\Task\{ |
13
|
|
|
Task, |
14
|
|
|
TaskInterface, |
15
|
|
|
Group, |
16
|
|
|
GroupInterface, |
17
|
|
|
TasksCollection, |
18
|
|
|
GroupsCollection |
19
|
|
|
}; |
20
|
|
|
|
21
|
|
|
class Runner implements RunnerInterface |
22
|
|
|
{ |
23
|
|
|
private $tasks; |
24
|
|
|
|
25
|
|
|
private $groups = []; |
26
|
|
|
|
27
|
|
|
private $order; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->tasks = new TasksCollection(); |
32
|
|
|
$this->groups = new GroupsCollection(); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
public function group($group, callable $tasks = null) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$_name = $group instanceof GroupInterface ? $group->getName() : $group; |
38
|
|
|
|
39
|
|
|
if ($this->tasks->containsKey($_name)) { |
40
|
|
|
throw new \Exception('Is not possible to register a group and a task with the same name.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->groups->set($group, $tasks); |
|
|
|
|
44
|
|
|
$this->order[] = 'group::' . $_name; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Registres a task |
49
|
|
|
* |
50
|
|
|
* @param string|TaskInterface $task |
51
|
|
|
* @param callable $callback |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function task($task, callable $callback = null) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$_name = $task instanceof TaskInterface ? $task->getName() : $task; |
56
|
|
|
|
57
|
|
|
if ($this->groups->containsKey($_name)) { |
|
|
|
|
58
|
|
|
throw new \Exception('Is not possible to register a group and a task with the same name.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->tasks->set($task, $callback); |
|
|
|
|
62
|
|
|
$this->order[] = 'task::' . $_name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Allows create a task setting a property |
67
|
|
|
* |
68
|
|
|
* @param string $task |
69
|
|
|
* @param callable|TaskInterface $callback |
70
|
|
|
*/ |
71
|
|
|
public function __set(string $task, $callback) |
72
|
|
|
{ |
73
|
|
|
if(!is_callable($callback) && !$callback instanceof TaskInterface) { |
74
|
|
|
throw new \InvalidArgumentException('Task must be callable or instanceof TaskInterface.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->task($task, $callback); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Organize tasks and groups order |
82
|
|
|
* If this method is executed and a task and a group is not in this list, it won't be executed |
83
|
|
|
* |
84
|
|
|
* @param string-variadic $tasks |
|
|
|
|
85
|
|
|
*/ |
86
|
|
|
public function order(string ...$names) |
87
|
|
|
{ |
88
|
|
|
foreach ($names as $key => $name) { |
89
|
|
|
if ($this->tasks->containsKey($name)) { |
90
|
|
|
$names[$key] = 'task::' . $name; |
91
|
|
|
} elseif ($this->groups->containsKey($name)) { |
|
|
|
|
92
|
|
|
$names[$key] = 'group::' . $name; |
93
|
|
|
} else { |
94
|
|
|
throw new \Exception('\'' . $name . '\' is not registred as task or group.'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->order = $names; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns all registred tasks |
103
|
|
|
* |
104
|
|
|
* @return TasksCollection |
105
|
|
|
*/ |
106
|
|
|
public function getTasks() : TasksCollection |
107
|
|
|
{ |
108
|
|
|
return $this->tasks; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getGroups() : GroupsCollection |
112
|
|
|
{ |
113
|
|
|
return $this->groups; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getOrder() : array |
117
|
|
|
{ |
118
|
|
|
return $this->order; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Runs all tasks |
123
|
|
|
*/ |
124
|
|
|
public function run() |
125
|
|
|
{ |
126
|
|
|
$all = $this->order; |
127
|
|
|
|
128
|
|
|
foreach ($all as $el) { |
129
|
|
|
$data = $this->getFromOrderData($el); |
130
|
|
|
|
131
|
|
|
switch ($data['type']) { |
132
|
|
|
case 'group': |
133
|
|
|
$this->runGroup($data['name']); |
134
|
|
|
break; |
135
|
|
|
case 'task': |
136
|
|
|
$this->runTask($data['name']); |
137
|
|
|
break; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Runs one single task |
144
|
|
|
* |
145
|
|
|
* @param string|TaskInterface $task |
146
|
|
|
*/ |
147
|
|
|
public function runTask($task) |
148
|
|
|
{ |
149
|
|
|
if (!is_string($task) && !$task instanceof TaskInterface) { |
150
|
|
|
throw new \Exception('Invalid task type: ' + gettype($task)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (is_string($task)) { |
154
|
|
|
if (!isset($this->tasks[$task])) { |
155
|
|
|
throw new \Exception('\'' . $task . '\' is not a registred task.'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$task = $task instanceof TaskInterface ? $task : $this->tasks[$task]; |
160
|
|
|
$cb = $task->getCallback(); |
161
|
|
|
$cb(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Runs a group of tasks |
166
|
|
|
* |
167
|
|
|
* @param string|GroupInterface $group |
168
|
|
|
*/ |
169
|
|
|
public function runGroup($group) |
170
|
|
|
{ |
171
|
|
|
$group = $group instanceof GroupInterface ? $group : $this->groups[$group]; |
172
|
|
|
$tasks = $group->getTasks(); |
173
|
|
|
|
174
|
|
|
foreach ($tasks as $task) { |
175
|
|
|
$this->runTask($task); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
View Code Duplication |
private function getFromOrderData($name) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$parts = explode('::', $name); |
182
|
|
|
$type = $parts[0]; |
183
|
|
|
unset($parts[0]); |
184
|
|
|
|
185
|
|
|
return ['type' => $type, 'name' => implode('::', $parts)]; |
186
|
|
|
} |
187
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..