|
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|TaskInterface $task |
|
69
|
|
|
* @param callable $callback |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __set(string $task, callable $callback) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->task($task, $callback); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Organize tasks and groups order |
|
78
|
|
|
* If this method is executed and a task and a group is not in this list, it won't be executed |
|
79
|
|
|
* |
|
80
|
|
|
* @param string-variadic $tasks |
|
|
|
|
|
|
81
|
|
|
*/ |
|
82
|
|
|
public function order(string ...$names) |
|
83
|
|
|
{ |
|
84
|
|
|
foreach ($names as $key => $name) { |
|
85
|
|
|
if ($this->tasks->containsKey($name)) { |
|
86
|
|
|
$names[$key] = 'task::' . $name; |
|
87
|
|
|
} elseif ($this->groups->containsKey($name)) { |
|
|
|
|
|
|
88
|
|
|
$names[$key] = 'group::' . $name; |
|
89
|
|
|
} else { |
|
90
|
|
|
throw new \Exception('\'' . $name . '\' is not registred as task or group.'); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->order = $names; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns all registred tasks |
|
99
|
|
|
* |
|
100
|
|
|
* @return TasksCollection |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getTasks() : TasksCollection |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->tasks; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getGroups() : GroupsCollection |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->groups; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getOrder() : array |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->order; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Runs all tasks |
|
119
|
|
|
*/ |
|
120
|
|
|
public function run() |
|
121
|
|
|
{ |
|
122
|
|
|
$all = $this->order; |
|
123
|
|
|
|
|
124
|
|
|
foreach ($all as $el) { |
|
125
|
|
|
$data = $this->getFromOrderData($el); |
|
126
|
|
|
|
|
127
|
|
|
switch ($data['type']) { |
|
128
|
|
|
case 'group': |
|
129
|
|
|
$this->runGroup($data['name']); |
|
130
|
|
|
break; |
|
131
|
|
|
case 'task': |
|
132
|
|
|
$this->runTask($data['name']); |
|
133
|
|
|
break; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Runs one single task |
|
140
|
|
|
* |
|
141
|
|
|
* @param string|TaskInterface $task |
|
142
|
|
|
*/ |
|
143
|
|
|
public function runTask($task) |
|
144
|
|
|
{ |
|
145
|
|
|
if (!is_string($task) && !$task instanceof TaskInterface) { |
|
146
|
|
|
throw new \Exception('Invalid task type: ' + gettype($task)); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if (is_string($task)) { |
|
150
|
|
|
if (!isset($this->tasks[$task])) { |
|
151
|
|
|
throw new \Exception('\'' . $task . '\' is not a registred task.'); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$task = $task instanceof TaskInterface ? $task : $this->tasks[$task]; |
|
156
|
|
|
$cb = $task->getCallback(); |
|
157
|
|
|
$cb(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Runs a group of tasks |
|
162
|
|
|
* |
|
163
|
|
|
* @param string|GroupInterface $group |
|
164
|
|
|
*/ |
|
165
|
|
|
public function runGroup($group) |
|
166
|
|
|
{ |
|
167
|
|
|
$group = $group instanceof GroupInterface ? $group : $this->groups[$group]; |
|
168
|
|
|
$tasks = $group->getTasks(); |
|
169
|
|
|
|
|
170
|
|
|
foreach ($tasks as $task) { |
|
171
|
|
|
$this->runTask($task); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
View Code Duplication |
private function getFromOrderData($name) |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
|
|
$parts = explode('::', $name); |
|
178
|
|
|
$type = $parts[0]; |
|
179
|
|
|
unset($parts[0]); |
|
180
|
|
|
|
|
181
|
|
|
return ['type' => $type, 'name' => implode('::', $parts)]; |
|
182
|
|
|
} |
|
183
|
|
|
} |
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..