1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Helper\Form; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Silverback\ApiComponentsBundle\Entity\Component\Form; |
18
|
|
|
use Silverback\ApiComponentsBundle\Event\CommandLogEvent; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
20
|
|
|
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Daniel West <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class FormCachePurger implements CacheClearerInterface |
26
|
|
|
{ |
27
|
|
|
private EntityManagerInterface $em; |
28
|
|
|
private EventDispatcherInterface $dispatcher; |
29
|
|
|
|
30
|
|
|
public function __construct(EntityManagerInterface $em, EventDispatcherInterface $dispatcher) |
31
|
|
|
{ |
32
|
|
|
$this->em = $em; |
33
|
|
|
$this->dispatcher = $dispatcher; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $cacheDir |
38
|
|
|
*/ |
39
|
|
|
public function clear($cacheDir = null): void |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
|
|
$repo = $this->em->getRepository(Form::class); |
43
|
|
|
/** @var Form[] $forms */ |
44
|
|
|
$forms = $repo->findAll(); |
45
|
|
|
} catch (\Exception $exception) { |
46
|
|
|
$this->dispatcher->dispatch(new CommandLogEvent(sprintf('<error>Could not clear form cache: %s</error>', $exception->getMessage()))); |
47
|
|
|
|
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
if (!\count($forms)) { |
51
|
|
|
$this->dispatcher->dispatch(new CommandLogEvent('<info>Skipping form component cache clear / timestamp updates - No forms components found</info>')); |
52
|
|
|
|
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
foreach ($forms as $form) { |
56
|
|
|
$this->updateFormTimestamp($form); |
57
|
|
|
} |
58
|
|
|
$this->em->flush(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function updateFormTimestamp(Form $form): void |
62
|
|
|
{ |
63
|
|
|
$formClass = $form->formType; |
64
|
|
|
$reflector = new \ReflectionClass($formClass); |
65
|
|
|
$dateTime = new \DateTime(); |
66
|
|
|
$timestamp = filemtime($reflector->getFileName()); |
67
|
|
|
|
68
|
|
|
$this->dispatcher->dispatch(new CommandLogEvent(sprintf('<info>Checking timestamp for %s</info>', $formClass))); |
69
|
|
|
|
70
|
|
|
if (!$form->modifiedAt || $timestamp !== $form->modifiedAt->getTimestamp()) { |
71
|
|
|
$dateTime->setTimestamp($timestamp); |
72
|
|
|
$form->modifiedAt = $dateTime; |
73
|
|
|
$this->dispatcher->dispatch(new CommandLogEvent('<comment>Updated timestamp</comment>')); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|