Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | n/a |
0 / 0 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
|||
SchedulerAbstract | n/a |
0 / 0 |
n/a |
0 / 0 |
14 | n/a |
0 / 0 |
|||
getBin | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
setBin | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
guessBin | n/a |
0 / 0 |
n/a |
0 / 0 |
4 | |||||
run | n/a |
0 / 0 |
n/a |
0 / 0 |
6 | |||||
create | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
update | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
deleteByName | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
delete | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
normalize | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
getAllByName | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
reload | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Utils\TaskSchedule |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Utils\TaskSchedule; |
16 | |
17 | use phpOMS\System\File\PathException; |
18 | |
19 | /** |
20 | * Scheduler abstract. |
21 | * |
22 | * @package phpOMS\Utils\TaskSchedule |
23 | * @license OMS License 2.0 |
24 | * @link https://jingga.app |
25 | * @since 1.0.0 |
26 | * @codeCoverageIgnore |
27 | */ |
28 | abstract class SchedulerAbstract |
29 | { |
30 | /** |
31 | * Bin path. |
32 | * |
33 | * @var string |
34 | * @since 1.0.0 |
35 | */ |
36 | private static string $bin = ''; |
37 | |
38 | /** |
39 | * Get git binary. |
40 | * |
41 | * @return string |
42 | * |
43 | * @since 1.0.0 |
44 | */ |
45 | public static function getBin() : string |
46 | { |
47 | return self::$bin; |
48 | } |
49 | |
50 | /** |
51 | * Set git binary. |
52 | * |
53 | * @param string $path Git path |
54 | * |
55 | * @return void |
56 | * |
57 | * @throws PathException |
58 | * |
59 | * @since 1.0.0 |
60 | */ |
61 | public static function setBin(string $path) : void |
62 | { |
63 | if (\realpath($path) === false) { |
64 | throw new PathException($path); |
65 | } |
66 | |
67 | self::$bin = \realpath($path); |
68 | } |
69 | |
70 | /** |
71 | * Gues git binary. |
72 | * |
73 | * @return bool |
74 | * |
75 | * @since 1.0.0 |
76 | */ |
77 | public static function guessBin() : bool |
78 | { |
79 | if (self::$bin !== '') { |
80 | return true; |
81 | } |
82 | |
83 | $paths = [ |
84 | 'c:/WINDOWS/system32/schtasks.exe', |
85 | 'd:/WINDOWS/system32/schtasks.exe', |
86 | 'e:/WINDOWS/system32/schtasks.exe', |
87 | 'f:/WINDOWS/system32/schtasks.exe', |
88 | '/usr/bin/crontab', |
89 | '/usr/local/bin/crontab', |
90 | '/usr/local/sbin/crontab', |
91 | '/usr/sbin/crontab', |
92 | '/bin/crontab', |
93 | '/sbin/crontab', |
94 | ]; |
95 | |
96 | foreach ($paths as $path) { |
97 | if (\is_file($path)) { |
98 | self::setBin($path); |
99 | |
100 | return true; |
101 | } |
102 | } |
103 | |
104 | return false; |
105 | } |
106 | |
107 | /** |
108 | * Run command |
109 | * |
110 | * @param string $cmd Command to run |
111 | * |
112 | * @return string |
113 | * |
114 | * @throws \Exception |
115 | * |
116 | * @since 1.0.0 |
117 | */ |
118 | protected function run(string $cmd) : string |
119 | { |
120 | $cmd = 'cd ' . \escapeshellarg(\dirname(self::$bin)) . ' && ' . \basename(self::$bin) . ' ' . $cmd; |
121 | |
122 | $pipes = []; |
123 | $desc = [ |
124 | 1 => ['pipe', 'w'], |
125 | 2 => ['pipe', 'w'], |
126 | ]; |
127 | |
128 | $resource = \proc_open($cmd, $desc, $pipes, __DIR__, null); |
129 | if ($resource === false) { |
130 | return ''; |
131 | } |
132 | |
133 | $stdout = \stream_get_contents($pipes[1]); |
134 | $stderr = \stream_get_contents($pipes[2]); |
135 | |
136 | foreach ($pipes as $pipe) { |
137 | \fclose($pipe); |
138 | } |
139 | |
140 | $status = \proc_close($resource); |
141 | |
142 | if ($status === -1 || $stderr !== '') { |
143 | throw new \Exception((string) $stderr); |
144 | } |
145 | |
146 | return $stdout === false ? '' : \trim($stdout); |
147 | } |
148 | |
149 | /** |
150 | * Create task |
151 | * |
152 | * @param TaskAbstract $task Task to create |
153 | * |
154 | * @return void |
155 | * |
156 | * @since 1.0.0 |
157 | */ |
158 | abstract public function create(TaskAbstract $task) : void; |
159 | |
160 | /** |
161 | * Update task |
162 | * |
163 | * @param TaskAbstract $task Task to update |
164 | * |
165 | * @return void |
166 | * |
167 | * @since 1.0.0 |
168 | */ |
169 | abstract public function update(TaskAbstract $task) : void; |
170 | |
171 | /** |
172 | * Delete task by name |
173 | * |
174 | * @param string $name Task name |
175 | * |
176 | * @return void |
177 | * |
178 | * @since 1.0.0 |
179 | */ |
180 | abstract public function deleteByName(string $name) : void; |
181 | |
182 | /** |
183 | * Delete task |
184 | * |
185 | * @param TaskAbstract $task Task to delete |
186 | * |
187 | * @return void |
188 | * |
189 | * @since 1.0.0 |
190 | */ |
191 | abstract public function delete(TaskAbstract $task) : void; |
192 | |
193 | /** |
194 | * Normalize run result for easier parsing |
195 | * |
196 | * @param string $raw Raw command output |
197 | * |
198 | * @return string Normalized string for parsing |
199 | * |
200 | * @since 1.0.0 |
201 | */ |
202 | protected function normalize(string $raw) : string |
203 | { |
204 | return \str_replace("\r\n", "\n", $raw); |
205 | } |
206 | |
207 | /** |
208 | * Get all jobs/tasks by name |
209 | * |
210 | * @param string $name Name of the job |
211 | * @param bool $exact Name has to be exact |
212 | * |
213 | * @return array |
214 | * |
215 | * @since 1.0.0 |
216 | */ |
217 | abstract public function getAllByName(string $name, bool $exact = true) : array; |
218 | |
219 | /**+ |
220 | * Reload the jobs |
221 | * |
222 | * @return void |
223 | * @since 1.0.0 |
224 | */ |
225 | abstract public function reload() : void; |
226 | } |