1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CronBundle\Command; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\CronBundle\Annotation\CronJob as CronJobAnno; |
6
|
|
|
use Alpixel\Bundle\CronBundle\Entity\CronJob; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class CronScanCommand extends ContainerAwareCommand |
15
|
|
|
{ |
16
|
|
|
protected function configure() |
17
|
|
|
{ |
18
|
|
|
$this->setName('cron:scan') |
19
|
|
|
->setDescription('Scans for any new or deleted cron jobs') |
20
|
|
|
->addOption('keep-deleted', 'k', InputOption::VALUE_NONE, 'If set, deleted cron jobs will not be removed') |
21
|
|
|
->addOption('default-disabled', 'd', InputOption::VALUE_NONE, 'If set, new jobs will be disabled by default'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
25
|
|
|
{ |
26
|
|
|
$keepDeleted = $input->getOption('keep-deleted'); |
27
|
|
|
$defaultDisabled = $input->getOption('default-disabled'); |
28
|
|
|
$em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
29
|
|
|
|
30
|
|
|
// Enumerate the known jobs |
31
|
|
|
$jobRepo = $em->getRepository('CronBundle:CronJob'); |
32
|
|
|
$knownJobs = $jobRepo->getKnownJobs(); |
33
|
|
|
$knownJobs = array_fill_keys($knownJobs, true); |
34
|
|
|
|
35
|
|
|
// Enumerate all the jobs currently loaded |
36
|
|
|
$reader = $this->getContainer()->get('annotation_reader'); |
37
|
|
|
|
38
|
|
|
foreach ($this->getApplication()->all() as $command) { |
39
|
|
|
// Check for an @CronJob annotation |
40
|
|
|
$reflClass = new \ReflectionClass($command); |
41
|
|
|
foreach ($reader->getClassAnnotations($reflClass) as $anno) { |
42
|
|
|
if ($anno instanceof CronJobAnno) { |
43
|
|
|
$job = $command->getName(); |
44
|
|
|
if (array_key_exists($job, $knownJobs)) { |
45
|
|
|
// Clear it from the known jobs so that we don't try to delete it |
46
|
|
|
unset($knownJobs[$job]); |
47
|
|
|
|
48
|
|
|
// Update the job if necessary |
49
|
|
|
$currentJob = $jobRepo->findOneByCommand($job); |
50
|
|
|
$currentJob->setDescription($command->getDescription()); |
51
|
|
|
if ($currentJob->getInterval() != $anno->value) { |
52
|
|
|
$newTime = new \DateTime(); |
53
|
|
|
$newTime = $newTime->add(new \DateInterval($anno->value)); |
54
|
|
|
|
55
|
|
|
$currentJob->setInterval($anno->value); |
56
|
|
|
$currentJob->setNextRun($newTime); |
57
|
|
|
$output->writeln("Updated interval for $job to {$anno->value}"); |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
|
|
$this->newJobFound($em, $output, $command, $anno, $defaultDisabled); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Clear any jobs that weren't found |
67
|
|
|
if (!$keepDeleted) { |
68
|
|
|
foreach (array_keys($knownJobs) as $deletedJob) { |
69
|
|
|
$output->writeln("Deleting job: $deletedJob"); |
70
|
|
|
$jobToDelete = $jobRepo->findOneByCommand($deletedJob); |
71
|
|
|
$em->remove($jobToDelete); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$em->flush(); |
76
|
|
|
$output->writeln('Finished scanning for cron jobs'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function newJobFound(EntityManager $em, OutputInterface $output, Command $command, CronJobAnno $anno, $defaultDisabled = false) |
80
|
|
|
{ |
81
|
|
|
$newJob = new CronJob(); |
82
|
|
|
$newJob->setCommand($command->getName()); |
83
|
|
|
$newJob->setDescription($command->getDescription()); |
84
|
|
|
$newJob->setInterval($anno->value); |
85
|
|
|
$newJob->setNextRun(new \DateTime()); |
|
|
|
|
86
|
|
|
$newJob->setEnabled(!$defaultDisabled); |
87
|
|
|
|
88
|
|
|
$output->writeln('Added the job '.$newJob->getCommand().' with interval '.$newJob->getInterval()); |
89
|
|
|
$em->persist($newJob); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: