1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\Console\CommandCron; |
4
|
|
|
|
5
|
|
|
use DocBlockReader\Reader; |
6
|
|
|
use Symfony\Component\Console\Application; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Ivan Shcherbak <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class CronTasksFinder { |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Application $application |
17
|
|
|
* @param callable|null $annotationFinder |
18
|
|
|
* @return CronTaskInfo[] |
19
|
|
|
*/ |
20
|
1 |
|
public function getCronTasks(Application $application, callable $annotationFinder = null) { |
21
|
|
|
|
22
|
1 |
|
if ($annotationFinder === null) { |
23
|
1 |
|
$annotationFinder = function (Reader $reader) { |
24
|
1 |
|
return $reader->getParameter('crontab'); |
25
|
1 |
|
}; |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
$tasks = []; |
29
|
|
|
|
30
|
1 |
|
foreach ($application->all() as $command) { |
31
|
1 |
|
$reader = new Reader(get_class($command)); |
32
|
|
|
|
33
|
1 |
|
$crontabAnnotations = $annotationFinder($reader); |
34
|
|
|
|
35
|
1 |
|
if (empty($crontabAnnotations)) { |
36
|
1 |
|
continue; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
$crontabAnnotations = (array) $crontabAnnotations; |
40
|
|
|
|
41
|
1 |
|
foreach ($crontabAnnotations as $crontabExpression) { |
42
|
1 |
|
$commandArguments = preg_split('!\s+!', $crontabExpression, -1, PREG_SPLIT_NO_EMPTY); |
43
|
1 |
|
array_splice($commandArguments, 0, 5); |
44
|
|
|
|
45
|
1 |
|
$commandArguments = implode(' ', $commandArguments); |
46
|
1 |
|
$preparedExpression = trim(str_replace([' /', $commandArguments], [' */', ''], ' ' . $crontabExpression)); |
47
|
1 |
|
$tasks[] = new CronTaskInfo($preparedExpression, $command, $commandArguments); |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $tasks; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
} |