1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\EventListener\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventSubscriber; |
6
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform; |
7
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
8
|
|
|
use Doctrine\ORM\UnitOfWork; |
9
|
|
|
use Enqueue\Client\TraceableProducer; |
|
|
|
|
10
|
|
|
use Liip\ImagineBundle\Async\Commands; |
11
|
|
|
use Liip\ImagineBundle\Async\ResolveCache; |
12
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
13
|
|
|
use Liip\ImagineBundle\Service\FilterService; |
14
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\FileInterface; |
15
|
|
|
use Silverback\ApiComponentBundle\Entity\SortableInterface; |
16
|
|
|
use Silverback\ApiComponentBundle\Serializer\ApiNormalizer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class FileEntitySubscriber |
20
|
|
|
* @package Silverback\ApiComponentBundle\EventListener |
21
|
|
|
* @author Daniel West <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class EntitySubscriber implements EventSubscriber |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var CacheManager |
27
|
|
|
*/ |
28
|
|
|
private $imagineCacheManager; |
29
|
|
|
/** |
30
|
|
|
* @var FilterService |
31
|
|
|
*/ |
32
|
|
|
private $filterService; |
33
|
|
|
/** |
34
|
|
|
* @var ApiNormalizer |
35
|
|
|
*/ |
36
|
|
|
private $fileNormalizer; |
37
|
|
|
/** |
38
|
|
|
* @var TraceableProducer |
39
|
|
|
*/ |
40
|
|
|
// private $producer; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* FileListener constructor. |
44
|
|
|
* @param CacheManager $imagineCacheManager |
45
|
|
|
* @param FilterService $filterService |
46
|
|
|
* @param ApiNormalizer $fileNormalizer |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
CacheManager $imagineCacheManager, |
50
|
|
|
FilterService $filterService, |
51
|
|
|
ApiNormalizer $fileNormalizer //, |
52
|
|
|
// TraceableProducer $producer |
53
|
|
|
) { |
54
|
|
|
$this->imagineCacheManager = $imagineCacheManager; |
55
|
|
|
$this->fileNormalizer = $fileNormalizer; |
56
|
|
|
$this->filterService = $filterService; |
57
|
|
|
// $this->producer = $producer; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function getSubscribedEvents(): array |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
'onFlush' |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param OnFlushEventArgs $eventArgs |
72
|
|
|
* @throws \Enqueue\Rpc\TimeoutException |
73
|
|
|
* @throws \Doctrine\DBAL\DBALException |
74
|
|
|
*/ |
75
|
|
|
public function onFlush(OnFlushEventArgs $eventArgs): void |
76
|
|
|
{ |
77
|
|
|
$entityManager = $eventArgs->getEntityManager(); |
78
|
|
|
if ($entityManager->getConnection()->getDatabasePlatform() instanceof SqlitePlatform) { |
79
|
|
|
$entityManager->getConnection()->exec('PRAGMA foreign_keys = ON;'); |
80
|
|
|
} |
81
|
|
|
$unitOfWork = $entityManager->getUnitOfWork(); |
82
|
|
|
$this->processNewEntities($unitOfWork); |
83
|
|
|
$this->processUpdatedEntities($unitOfWork); |
84
|
|
|
$this->processDeletedEntities($unitOfWork); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param UnitOfWork $unitOfWork |
89
|
|
|
* @throws \Enqueue\Rpc\TimeoutException |
90
|
|
|
*/ |
91
|
|
|
private function processNewEntities(UnitOfWork $unitOfWork): void |
92
|
|
|
{ |
93
|
|
|
$newEntities = $unitOfWork->getScheduledEntityInsertions(); |
94
|
|
|
foreach ($newEntities as $entity) { |
95
|
|
|
if ($entity instanceof SortableInterface) { |
96
|
|
|
try { |
97
|
|
|
$entity->getSort(); |
98
|
|
|
} catch (\TypeError $e) { |
99
|
|
|
$entity->setSort($entity->calculateSort(true)); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
if ( |
103
|
|
|
$entity instanceof FileInterface && |
104
|
|
|
$this->fileNormalizer->isImagineSupportedFile($entity->getFilePath()) |
105
|
|
|
) { |
106
|
|
|
$this->sendCommand($entity); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param UnitOfWork $unitOfWork |
113
|
|
|
* @throws \Enqueue\Rpc\TimeoutException |
114
|
|
|
*/ |
115
|
|
|
private function processUpdatedEntities(UnitOfWork $unitOfWork): void |
116
|
|
|
{ |
117
|
|
|
$updatedEntities = $unitOfWork->getScheduledEntityUpdates(); |
118
|
|
|
foreach ($updatedEntities as $entity) { |
119
|
|
|
if ($entity instanceof FileInterface) { |
120
|
|
|
$changes = $unitOfWork->getEntityChangeSet($entity); |
121
|
|
|
if (!\is_array($changes)) { |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
if (array_key_exists('filePath', $changes)) { |
125
|
|
|
$fpChanges = $changes['filePath']; |
126
|
|
|
$previousValueForField = $fpChanges[0] ?? null; |
127
|
|
|
$newValueForField = $fpChanges[1] ?? null; |
128
|
|
|
if ($previousValueForField !== $newValueForField) { |
129
|
|
|
if ($this->fileNormalizer->isImagineSupportedFile($previousValueForField)) { |
130
|
|
|
$this->imagineCacheManager->remove($previousValueForField); |
131
|
|
|
} |
132
|
|
|
if ($this->fileNormalizer->isImagineSupportedFile($newValueForField)) { |
133
|
|
|
$this->createFilteredImages($entity); |
134
|
|
|
// $this->sendCommand($entity); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param UnitOfWork $unitOfWork |
144
|
|
|
*/ |
145
|
|
|
private function processDeletedEntities(UnitOfWork $unitOfWork): void |
146
|
|
|
{ |
147
|
|
|
$deletedEntities = $unitOfWork->getScheduledEntityDeletions(); |
148
|
|
|
foreach ($deletedEntities as $entity) { |
149
|
|
|
if ( |
150
|
|
|
$entity instanceof FileInterface && |
151
|
|
|
$this->fileNormalizer->isImagineSupportedFile($entity->getFilePath()) |
152
|
|
|
) { |
153
|
|
|
$this->imagineCacheManager->remove($entity->getFilePath()); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function createFilteredImages(FileInterface $file): void |
159
|
|
|
{ |
160
|
|
|
$filters = $file::getImagineFilters(); |
161
|
|
|
foreach ($filters as $filter) { |
162
|
|
|
$this->filterService->getUrlOfFilteredImage($file->getFilePath(), $filter); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param FileInterface $file |
168
|
|
|
* @throws \Enqueue\Rpc\TimeoutException |
169
|
|
|
*/ |
170
|
|
|
private function sendCommand(FileInterface $file): void |
171
|
|
|
{ |
172
|
|
|
$this->producer |
|
|
|
|
173
|
|
|
->sendCommand( |
174
|
|
|
Commands::RESOLVE_CACHE, |
175
|
|
|
new ResolveCache($file->getFilePath(), $file::getImagineFilters()), |
176
|
|
|
true |
177
|
|
|
) |
178
|
|
|
->receive(20000) |
179
|
|
|
; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths