|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MyBuilder\Bundle\CronosBundle\Exporter; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Annotation; |
|
6
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
7
|
|
|
use MyBuilder\Bundle\CronosBundle\Annotation\Cron as CronAnnotation; |
|
8
|
|
|
use MyBuilder\Cronos\Formatter\Cron as CronFormatter; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
|
|
11
|
|
|
class AnnotationCronExporter |
|
12
|
|
|
{ |
|
13
|
|
|
public const ALL_SERVERS = 'all'; |
|
14
|
|
|
|
|
15
|
|
|
/** @var Reader */ |
|
16
|
|
|
private $annotationsReader; |
|
17
|
|
|
|
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
private $config = []; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(Reader $annotationsReader) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->annotationsReader = $annotationsReader; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function setConfig(array $config): void |
|
27
|
|
|
{ |
|
28
|
|
|
$this->config = $config; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Export the cron for the given commands and server |
|
33
|
|
|
*/ |
|
34
|
|
|
public function export(array $commands, array $options): CronFormatter |
|
35
|
|
|
{ |
|
36
|
|
|
$cron = $this->createCronConfiguration(); |
|
37
|
|
|
|
|
38
|
|
|
foreach ($commands as $command) { |
|
39
|
|
|
if ($command instanceof Command) { |
|
40
|
|
|
$cron = $this->parseAnnotations($cron, $command, $options); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $cron; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function createCronConfiguration(): CronFormatter |
|
48
|
|
|
{ |
|
49
|
|
|
$cron = new CronFormatter; |
|
50
|
|
|
$configurator = new ArrayHeaderConfigurator($cron->header()); |
|
51
|
|
|
$configurator->configureFrom($this->config); |
|
52
|
|
|
|
|
53
|
|
|
return $cron; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function parseAnnotations(CronFormatter $cron, Command $command, array $options): CronFormatter |
|
57
|
|
|
{ |
|
58
|
|
|
foreach ($this->getAnnotations($command) as $annotation) { |
|
59
|
|
|
if ($this->annotationBelongsToServer($annotation, $options['serverName'])) { |
|
60
|
|
|
$cron = $this->addLine($command, $annotation, $options, $cron); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $cron; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function annotationBelongsToServer(Annotation $annotation, string $serverName): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return |
|
70
|
|
|
$annotation instanceof CronAnnotation |
|
71
|
|
|
&& ($serverName === self::ALL_SERVERS || $annotation->server === $serverName); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function addLine(Command $command, CronAnnotation $annotation, array $options, CronFormatter $cron): CronFormatter |
|
75
|
|
|
{ |
|
76
|
|
|
if ($annotation->comment !== null) { |
|
77
|
|
|
$cron->comment($annotation->comment); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ($command->getDescription()) { |
|
81
|
|
|
$cron->comment($command->getDescription()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$line = $cron->job($this->buildCommand($command->getName(), $annotation, $options)); |
|
85
|
|
|
|
|
86
|
|
|
$configurator = new AnnotationLineConfigurator($line); |
|
87
|
|
|
$configurator->configureFrom($annotation); |
|
88
|
|
|
|
|
89
|
|
|
return $cron; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function getAnnotations(Command $command): array |
|
93
|
|
|
{ |
|
94
|
|
|
$reflectedClass = new \ReflectionClass($command); |
|
95
|
|
|
|
|
96
|
|
|
return $this->annotationsReader->getClassAnnotations($reflectedClass); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Build the Command to execute with parameters and environment. |
|
101
|
|
|
*/ |
|
102
|
|
|
private function buildCommand(string $commandName, CronAnnotation $annotation, array $options): string |
|
103
|
|
|
{ |
|
104
|
|
|
$executor = ''; |
|
105
|
|
|
|
|
106
|
|
|
if ($annotation->executor) { |
|
107
|
|
|
$executor = $annotation->executor; |
|
108
|
|
|
} elseif ($this->config['executor']) { |
|
109
|
|
|
$executor = $this->config['executor']; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$console = isset($this->config['console']) ? ' ' . $this->config['console'] : ''; |
|
113
|
|
|
$environment = isset($options['environment']) ? ' --env=' . $options['environment'] : ''; |
|
114
|
|
|
$params = $annotation->params ? ' ' . $annotation->params : ''; |
|
115
|
|
|
|
|
116
|
|
|
return $executor . $console . ' ' . $commandName . $params . $environment; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|