|
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
|
1 |
|
public function __construct(EntityManagerInterface $em, EventDispatcherInterface $dispatcher) |
|
18
|
|
|
{ |
|
19
|
1 |
|
$this->em = $em; |
|
20
|
1 |
|
$this->dispatcher = $dispatcher; |
|
21
|
1 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $cacheDir |
|
25
|
|
|
* @throws \ReflectionException |
|
26
|
|
|
*/ |
|
27
|
1 |
|
public function clear($cacheDir = null): void |
|
28
|
|
|
{ |
|
29
|
|
|
try { |
|
30
|
1 |
|
$repo = $this->em->getRepository(Form::class); |
|
31
|
1 |
|
$forms = $repo->findAll(); |
|
32
|
|
|
} catch (\Exception $exception) { |
|
33
|
|
|
$this->dispatcher->dispatch( |
|
34
|
|
|
self::FORM_CACHE_EVENT_NAME, |
|
35
|
|
|
new CommandNotifyEvent(sprintf('<error>Could not clear form cache: %s</error>', $exception->getMessage())) |
|
36
|
|
|
); |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
1 |
|
if (!\count($forms)) { |
|
40
|
|
|
$this->dispatcher->dispatch( |
|
41
|
|
|
self::FORM_CACHE_EVENT_NAME, |
|
42
|
|
|
new CommandNotifyEvent('<info>Skipping form component cache clear / timestamp updates - No forms components found</info>') |
|
43
|
|
|
); |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
1 |
|
foreach ($forms as $form) { |
|
47
|
1 |
|
$this->updateFormTimestamp($form); |
|
48
|
|
|
} |
|
49
|
1 |
|
$this->em->flush(); |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Form $form |
|
54
|
|
|
* @throws \ReflectionException |
|
55
|
|
|
*/ |
|
56
|
1 |
|
private function updateFormTimestamp(Form $form): void |
|
57
|
|
|
{ |
|
58
|
1 |
|
$formClass = $form->getFormType(); |
|
59
|
1 |
|
$reflector = new \ReflectionClass($formClass); |
|
60
|
1 |
|
$dateTime = new \DateTime(); |
|
61
|
1 |
|
$timestamp = filemtime($reflector->getFileName()); |
|
62
|
|
|
|
|
63
|
1 |
|
$this->dispatcher->dispatch( |
|
64
|
1 |
|
self::FORM_CACHE_EVENT_NAME, |
|
65
|
1 |
|
new CommandNotifyEvent(sprintf('<info>Checking timestamp for %s</info>', $formClass)) |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
1 |
|
if (!$form->getLastModified() || $timestamp !== $form->getLastModified()->getTimestamp()) { |
|
69
|
1 |
|
$dateTime->setTimestamp($timestamp); |
|
70
|
1 |
|
$form->setLastModified($dateTime); |
|
71
|
1 |
|
$this->dispatcher->dispatch( |
|
72
|
1 |
|
self::FORM_CACHE_EVENT_NAME, |
|
73
|
1 |
|
new CommandNotifyEvent('<comment>Updated timestamp</comment>') |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
1 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|