1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Silverback\ApiComponentBundle\Form\Cache; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use Silverback\ApiComponentBundle\ApiComponentBundleEvents; |
11
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\Form; |
12
|
|
|
use Silverback\ApiComponentBundle\Event\CommandLogEvent; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface; |
15
|
|
|
|
16
|
|
|
class FormCachePurger implements CacheClearerInterface |
17
|
|
|
{ |
18
|
|
|
private EntityManagerInterface $em; |
19
|
|
|
private EventDispatcherInterface $dispatcher; |
20
|
|
|
|
21
|
|
|
public function __construct(EntityManagerInterface $em, EventDispatcherInterface $dispatcher) |
22
|
|
|
{ |
23
|
|
|
$this->em = $em; |
24
|
|
|
$this->dispatcher = $dispatcher; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $cacheDir |
29
|
|
|
*/ |
30
|
|
|
public function clear($cacheDir = null): void |
31
|
|
|
{ |
32
|
|
|
try { |
33
|
|
|
$repo = $this->em->getRepository(Form::class); |
34
|
|
|
/** @var Form[] $forms */ |
35
|
|
|
$forms = $repo->findAll(); |
36
|
|
|
} catch (\Exception $exception) { |
37
|
|
|
$this->dispatcher->dispatch( |
38
|
|
|
new CommandLogEvent(sprintf('<error>Could not clear form cache: %s</error>', $exception->getMessage())), |
39
|
|
|
ApiComponentBundleEvents::COMMAND_LOG |
|
|
|
|
40
|
|
|
); |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
if (!\count($forms)) { |
44
|
|
|
$this->dispatcher->dispatch( |
45
|
|
|
new CommandLogEvent('<info>Skipping form component cache clear / timestamp updates - No forms components found</info>'), |
46
|
|
|
ApiComponentBundleEvents::COMMAND_LOG |
47
|
|
|
); |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
foreach ($forms as $form) { |
51
|
|
|
$this->updateFormTimestamp($form); |
52
|
|
|
} |
53
|
|
|
$this->em->flush(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function updateFormTimestamp(Form $form): void |
57
|
|
|
{ |
58
|
|
|
$formClass = $form->formType; |
59
|
|
|
$reflector = new ReflectionClass($formClass); |
60
|
|
|
$dateTime = new DateTime(); |
61
|
|
|
$timestamp = filemtime($reflector->getFileName()); |
62
|
|
|
|
63
|
|
|
$this->dispatcher->dispatch( |
64
|
|
|
new CommandLogEvent(sprintf('<info>Checking timestamp for %s</info>', $formClass)), |
65
|
|
|
ApiComponentBundleEvents::COMMAND_LOG |
|
|
|
|
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
if (!$form->modified || $timestamp !== $form->modified->getTimestamp()) { |
69
|
|
|
$dateTime->setTimestamp($timestamp); |
70
|
|
|
$form->modified = $dateTime; |
71
|
|
|
$this->dispatcher->dispatch( |
72
|
|
|
new CommandLogEvent('<comment>Updated timestamp</comment>'), |
73
|
|
|
ApiComponentBundleEvents::COMMAND_LOG |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.