1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MyBuilder\Bundle\CronosBundle\Command; |
4
|
|
|
|
5
|
|
|
use MyBuilder\Bundle\CronosBundle\Exporter\AnnotationCronExporter; |
6
|
|
|
use MyBuilder\Cronos\Formatter\Cron; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
|
15
|
|
|
class CommandBase extends Command implements ContainerAwareInterface |
16
|
|
|
{ |
17
|
|
|
use ContainerAwareTrait; |
18
|
|
|
|
19
|
|
|
protected function addServerOption(): void |
20
|
|
|
{ |
21
|
|
|
$this |
22
|
|
|
->addOption('server', null, InputOption::VALUE_REQUIRED, 'Only include cron jobs for the specified server', AnnotationCronExporter::ALL_SERVERS); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function configureCronExport(InputInterface $input, OutputInterface $output): Cron |
26
|
|
|
{ |
27
|
|
|
$options = [ |
28
|
|
|
'serverName' => $input->getOption('server'), |
29
|
|
|
'environment' => $input->getOption('env'), |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
$output->writeln(sprintf('Server <comment>%s</comment>', $options['serverName'])); |
33
|
|
|
$cron = $this->exportCron($options); |
34
|
|
|
$output->writeln(sprintf('<comment>Found %d lines</comment>', $cron->countLines())); |
35
|
|
|
|
36
|
|
|
return $cron; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @throws \LogicException */ |
40
|
|
|
protected function getContainer(): ContainerInterface |
41
|
|
|
{ |
42
|
|
|
if (null === $this->container) { |
43
|
|
|
$application = $this->getApplication(); |
44
|
|
|
|
45
|
|
|
if (null === $application) { |
46
|
|
|
throw new \LogicException('The container cannot be retrieved as the application instance is not yet set.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->container = $application->getKernel()->getContainer(); |
50
|
|
|
|
51
|
|
|
if (null === $this->container) { |
52
|
|
|
throw new \LogicException('The container cannot be retrieved as the kernel has shut down.'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->container; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function exportCron($options): Cron |
60
|
|
|
{ |
61
|
|
|
$commands = $this->getApplication()->all(); |
62
|
|
|
/** @var AnnotationCronExporter $exporter */ |
63
|
|
|
$exporter = $this->getContainer()->get('mybuilder.cronos_bundle.annotation_cron_exporter'); |
64
|
|
|
|
65
|
|
|
return $exporter->export($commands, $options); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|