These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php namespace Comodojo\Extender; |
||
2 | |||
3 | use \Spyc; |
||
4 | |||
5 | /** |
||
6 | * Tasks table |
||
7 | * |
||
8 | * @package Comodojo extender |
||
9 | * @author Marco Giovinazzi <[email protected]> |
||
10 | * @license GPL-3.0+ |
||
11 | * |
||
12 | * LICENSE: |
||
13 | * |
||
14 | * This program is free software: you can redistribute it and/or modify |
||
15 | * it under the terms of the GNU Affero General Public License as |
||
16 | * published by the Free Software Foundation, either version 3 of the |
||
17 | * License, or (at your option) any later version. |
||
18 | * |
||
19 | * This program is distributed in the hope that it will be useful, |
||
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
22 | * GNU Affero General Public License for more details. |
||
23 | * |
||
24 | * You should have received a copy of the GNU Affero General Public License |
||
25 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
26 | */ |
||
27 | |||
28 | class TasksTable { |
||
29 | |||
30 | /** |
||
31 | * Tasks database (a simple array!). |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $tasks = array(); |
||
36 | |||
37 | /** |
||
38 | * Current logger |
||
39 | * |
||
40 | * @var /Monolog/logger |
||
41 | */ |
||
42 | private $logger = null; |
||
43 | |||
44 | /** |
||
45 | * Class constructor |
||
46 | * |
||
47 | * @param /Monolog/Logger $logger |
||
0 ignored issues
–
show
|
|||
48 | */ |
||
49 | 30 | public function __construct(\Monolog\Logger $logger) { |
|
50 | |||
51 | 30 | $this->logger = $logger; |
|
52 | |||
53 | 30 | } |
|
54 | |||
55 | /** |
||
56 | * Register a task |
||
57 | * |
||
58 | * @param string $name Task name (unique) |
||
59 | * @param string $class The target class to invoke |
||
60 | * @param string $description A brief description for the task |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | 21 | final public function add($name, $class, $description) { |
|
65 | |||
66 | 21 | if ( empty($name) || empty($class) ) { |
|
67 | |||
68 | $this->logger->warning("Skipping task due to invalid definition", array( |
||
69 | "NAME" => $name, |
||
70 | "CLASS" => $class, |
||
71 | "DESCRIPTION"=> $description |
||
72 | )); |
||
73 | |||
74 | return false; |
||
75 | |||
76 | } |
||
77 | |||
78 | 21 | $this->tasks[$name] = array( |
|
79 | 21 | "description" => $description, |
|
80 | "class" => $class |
||
81 | 21 | ); |
|
82 | |||
83 | 21 | return true; |
|
84 | |||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Delete a task from registry |
||
89 | * |
||
90 | * @param string $name Task name (unique) |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | 3 | final public function remove($name) { |
|
95 | |||
96 | 3 | if ( $this->registered($name) ) { |
|
97 | |||
98 | 3 | unset($this->tasks[$name]); |
|
99 | |||
100 | 3 | return true; |
|
101 | |||
102 | 3 | } else return false; |
|
103 | |||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Check if task is registered |
||
108 | * |
||
109 | * @param string $task Task name (unique) |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | 3 | final public function isRegistered($task) { |
|
114 | |||
115 | 3 | return $this->registered($task); |
|
116 | |||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get task description |
||
121 | * |
||
122 | * @param string $task Task name (unique) |
||
123 | * |
||
124 | * @return string|null |
||
125 | */ |
||
126 | 3 | final public function getDescription($task) { |
|
127 | |||
128 | 3 | if ( $this->registered($task) ) return $this->tasks[$task]["description"]; |
|
129 | |||
130 | else return null; |
||
131 | |||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get task class |
||
136 | * |
||
137 | * @param string $task Task name (unique) |
||
138 | * |
||
139 | * @return string|null |
||
140 | */ |
||
141 | 3 | final public function getClass($task) { |
|
142 | |||
143 | 3 | if ( $this->registered($task) ) { |
|
144 | |||
145 | 3 | return str_replace('\\\\', '\\', $this->tasks[$task]["class"]); |
|
146 | |||
147 | } else { |
||
148 | |||
149 | return null; |
||
150 | |||
151 | } |
||
152 | |||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Get whole table |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | 3 | final public function getTasks($sort = false) { |
|
161 | |||
162 | 3 | if ( $sort === true ) ksort($this->tasks); |
|
163 | |||
164 | 3 | return $this->tasks; |
|
165 | |||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Create a TaskTable and load tasks from EXTENDER_TASKS_CONFIG |
||
170 | * |
||
171 | * @param \Monolog\Logger $logger |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | 30 | View Code Duplication | public static function load(\Monolog\Logger $logger) { |
176 | |||
177 | 30 | $table = new TasksTable($logger); |
|
178 | |||
179 | 30 | if ( defined("EXTENDER_TASKS_CONFIG") && is_readable(EXTENDER_TASKS_CONFIG) ) { |
|
180 | |||
181 | 30 | $tasks = Spyc::YAMLLoad(EXTENDER_TASKS_CONFIG); |
|
182 | |||
183 | 30 | foreach ($tasks as $task => $parameters) { |
|
184 | |||
185 | $description = empty($parameters["data"]["description"]) ? null : $parameters["data"]["description"]; |
||
186 | |||
187 | $table->add($task, $parameters["data"]["class"], $description); |
||
188 | |||
189 | 30 | } |
|
190 | |||
191 | 30 | } |
|
192 | |||
193 | 30 | return $table; |
|
194 | |||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Check if task is registered |
||
199 | * |
||
200 | * @param string $task Task name (unique) |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | 12 | private function registered($task) { |
|
205 | |||
206 | 12 | if ( array_key_exists($task, $this->tasks) ) return true; |
|
207 | |||
208 | 3 | else return false; |
|
209 | |||
210 | } |
||
211 | |||
212 | } |
||
213 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.