|
1
|
|
|
<?php namespace Comodojo\Extender\Task; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
|
4
|
|
|
use \Comodojo\Foundation\Events\Manager as EventsManager; |
|
5
|
|
|
use \Comodojo\Foundation\DataAccess\ArrayAccessTrait; |
|
6
|
|
|
use \Comodojo\Foundation\DataAccess\IteratorTrait; |
|
7
|
|
|
use \Comodojo\Foundation\DataAccess\CountableTrait; |
|
8
|
|
|
use \Comodojo\Daemon\Traits\LoggerTrait; |
|
9
|
|
|
use \Comodojo\Daemon\Traits\EventsTrait; |
|
10
|
|
|
use \Comodojo\Extender\Traits\ConfigurationTrait; |
|
11
|
|
|
use \Psr\Log\LoggerInterface; |
|
12
|
|
|
use \Iterator; |
|
13
|
|
|
use \ArrayAccess; |
|
14
|
|
|
use \Countable; |
|
15
|
|
|
use \Exception; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @package Comodojo Extender |
|
19
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
20
|
|
|
* @license MIT |
|
21
|
|
|
* |
|
22
|
|
|
* LICENSE: |
|
23
|
|
|
* |
|
24
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
25
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
26
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
27
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
28
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
29
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
30
|
|
|
* THE SOFTWARE. |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
class Table implements Iterator, ArrayAccess, Countable { |
|
35
|
|
|
|
|
36
|
|
|
use ArrayAccessTrait; |
|
37
|
|
|
use IteratorTrait; |
|
38
|
|
|
use CountableTrait; |
|
39
|
|
|
use LoggerTrait; |
|
40
|
|
|
use ConfigurationTrait; |
|
41
|
|
|
use EventsTrait; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
private $data = []; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct( |
|
49
|
|
|
Configuration $configuration, |
|
50
|
|
|
LoggerInterface $logger, |
|
51
|
|
|
EventsManager $events |
|
52
|
|
|
) { |
|
53
|
|
|
|
|
54
|
|
|
$this->setConfiguration($configuration); |
|
55
|
|
|
$this->setLogger($logger); |
|
56
|
|
|
$this->setEvents($events); |
|
57
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the task item |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $name |
|
64
|
|
|
* @return TaskItem|null |
|
65
|
|
|
*/ |
|
66
|
|
|
public function get($name) { |
|
67
|
|
|
|
|
68
|
|
|
if ( array_key_exists($name, $this->data) ) { |
|
69
|
|
|
return $this->data[$name]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return null; |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Add a new task to table |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $name |
|
80
|
|
|
* @param string $class |
|
81
|
|
|
* @param string $description |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public function add($name, $class, $description = null) { |
|
85
|
|
|
|
|
86
|
|
|
if ( array_key_exists($name, $this->data) ) { |
|
87
|
|
|
$this->logger->warning("Skipping duplicate task $name ($class)"); |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ( empty($name) || empty($class) || !class_exists($class) ) { |
|
92
|
|
|
$this->logger->warning("Skipping invalid task definition", array( |
|
93
|
|
|
"NAME" => $name, |
|
94
|
|
|
"CLASS" => $class, |
|
95
|
|
|
"DESCRIPTION"=> $description |
|
96
|
|
|
)); |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->data[$name] = new TaskItem( |
|
101
|
|
|
$this->getConfiguration(), |
|
102
|
|
|
$this->getEvents(), |
|
103
|
|
|
$this->getLogger(), |
|
104
|
|
|
$name, |
|
105
|
|
|
$class, |
|
106
|
|
|
$description |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
return true; |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Delete a task from table |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $name |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
public function delete($name) { |
|
120
|
|
|
|
|
121
|
|
|
if ( array_key_exists($name, $this->data) ) { |
|
122
|
|
|
unset($this->data[$name]); |
|
123
|
|
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return false; |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Load a bulk task list into the table |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $tasks |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
|
|
public function bulk(array $tasks) { |
|
137
|
|
|
|
|
138
|
|
|
$result = []; |
|
139
|
|
|
|
|
140
|
|
|
foreach($tasks as $task) { |
|
141
|
|
|
|
|
142
|
|
|
if ( empty($task['name']) || empty($task['class']) ) { |
|
143
|
|
|
|
|
144
|
|
|
$this->logger->warning("Skipping invalid task definition", array( |
|
145
|
|
|
"NAME" => $name, |
|
|
|
|
|
|
146
|
|
|
"CLASS" => $class, |
|
|
|
|
|
|
147
|
|
|
"DESCRIPTION"=> $description |
|
|
|
|
|
|
148
|
|
|
)); |
|
149
|
|
|
$result[] = false; |
|
150
|
|
|
|
|
151
|
|
|
} else { |
|
152
|
|
|
|
|
153
|
|
|
$result[] = $this->add($task['name'], $task['class'], empty($task['description']) ? null : $task['description']); |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $result; |
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.