Repository navigation
Expand file tree
/
Copy pathCrontabManager.php
More file actions
70 lines (62 loc) · 1.67 KB
/
Copy pathCrontabManager.php
File metadata and controls
70 lines (62 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Crontab;
class CrontabManager
{
/**
* @var array<string, Crontab>
*/
protected array $crontabs = [];
public function __construct(protected Parser $parser)
{
}
public function register(Crontab $crontab): bool
{
if (! $this->isValidCrontab($crontab) || ! $crontab->isEnable()) {
return false;
}
$this->crontabs[$crontab->getName()] = $crontab;
return true;
}
/**
* @return Crontab[]
*/
public function parse(): array
{
$result = [];
$crontabs = $this->getCrontabs();
$last = time();
foreach ($crontabs as $key => $crontab) {
if (! $crontab instanceof Crontab) {
unset($this->crontabs[$key]);
continue;
}
$time = $this->parser->parse($crontab->getRule(), $last, $crontab->getTimezone());
if ($time) {
foreach ($time as $t) {
$result[] = (clone $crontab)->setExecuteTime($t);
}
}
}
return $result;
}
/**
* @return array<string, Crontab>
*/
public function getCrontabs(): array
{
return $this->crontabs;
}
public function isValidCrontab(Crontab $crontab): bool
{
return $crontab->getName() && $crontab->getRule() && $crontab->getCallback() && $this->parser->isValid($crontab->getRule());
}
}