1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Cache; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\Component\Form\Form; |
7
|
|
|
use Silverback\ApiComponentBundle\Event\CommandNotifyEvent; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
9
|
|
|
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface; |
10
|
|
|
|
11
|
|
|
class FormCacheClearer implements CacheClearerInterface |
12
|
|
|
{ |
13
|
|
|
public const FORM_CACHE_EVENT_NAME = 'api_component_bundle.form_cache_clear_event'; |
14
|
|
|
private $em; |
15
|
|
|
private $dispatcher; |
16
|
|
|
|
17
|
|
|
public function __construct(EntityManagerInterface $em, EventDispatcherInterface $dispatcher) |
18
|
|
|
{ |
19
|
|
|
$this->em = $em; |
20
|
|
|
$this->dispatcher = $dispatcher; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $cacheDir |
25
|
|
|
* @throws \ReflectionException |
26
|
|
|
*/ |
27
|
|
|
public function clear($cacheDir = null): void |
28
|
|
|
{ |
29
|
|
|
$repo = $this->em->getRepository(Form::class); |
30
|
|
|
$forms = $repo->findAll(); |
31
|
|
|
if (!\count($forms)) { |
32
|
|
|
$this->dispatcher->dispatch( |
33
|
|
|
self::FORM_CACHE_EVENT_NAME, |
34
|
|
|
new CommandNotifyEvent('<info>Skipping form component cache clear / timestamp updates - No forms components found</info>') |
35
|
|
|
); |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
foreach ($forms as $form) { |
39
|
|
|
$this->updateFormTimestamp($form); |
40
|
|
|
} |
41
|
|
|
$this->em->flush(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Form $form |
46
|
|
|
* @throws \ReflectionException |
47
|
|
|
*/ |
48
|
|
|
private function updateFormTimestamp(Form $form): void |
49
|
|
|
{ |
50
|
|
|
$formClass = $form->getFormType(); |
51
|
|
|
$reflector = new \ReflectionClass($formClass); |
52
|
|
|
$dateTime = new \DateTime(); |
53
|
|
|
$timestamp = filemtime($reflector->getFileName()); |
54
|
|
|
|
55
|
|
|
$this->dispatcher->dispatch( |
56
|
|
|
self::FORM_CACHE_EVENT_NAME, |
57
|
|
|
new CommandNotifyEvent(sprintf('<info>Checking timestamp for %s</info>', $formClass)) |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
if (!$form->getLastModified() || $timestamp !== $form->getLastModified()->getTimestamp()) { |
61
|
|
|
$dateTime->setTimestamp($timestamp); |
62
|
|
|
$form->setLastModified($dateTime); |
63
|
|
|
$this->dispatcher->dispatch( |
64
|
|
|
self::FORM_CACHE_EVENT_NAME, |
65
|
|
|
new CommandNotifyEvent('<comment>Updated timestamp</comment>') |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |