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\Foundation\Logging\LoggerTrait; |
9
|
|
|
use \Comodojo\Foundation\Events\EventsTrait; |
10
|
|
|
use \Comodojo\Foundation\Base\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
|
|
|
$this->logger->debug("Task $name ($class) in table"); |
110
|
|
|
|
111
|
|
|
return true; |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Delete a task from table |
117
|
|
|
* |
118
|
|
|
* @param string $name |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function delete($name) { |
122
|
|
|
|
123
|
|
|
if ( array_key_exists($name, $this->data) ) { |
124
|
|
|
unset($this->data[$name]); |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return false; |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Load a bulk task list into the table |
134
|
|
|
* |
135
|
|
|
* @param array $tasks |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
public function addBulk(array $tasks) { |
139
|
|
|
|
140
|
|
|
$result = []; |
141
|
|
|
|
142
|
|
|
foreach($tasks as $name => $task) { |
143
|
|
|
|
144
|
|
|
if ( empty($task['class']) ) { |
145
|
|
|
|
146
|
|
|
$this->logger->warning("Missing class for task $name"); |
147
|
|
|
$result[] = false; |
148
|
|
|
|
149
|
|
|
} else { |
150
|
|
|
|
151
|
|
|
$result[] = $this->add($name, $task['class'], empty($task['description']) ? null : $task['description']); |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $result; |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public static function create( |
162
|
|
|
Configuration $configuration, |
163
|
|
|
LoggerInterface $logger, |
164
|
|
|
EventsManager $events |
165
|
|
|
) { |
166
|
|
|
|
167
|
|
|
return new Table($configuration, $logger, $events); |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|