|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Bukashk0zzzLiipImagineSerializationBundle |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Denis Golubovskiy <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Bukashk0zzz\LiipImagineSerializationBundle\Tests\EventListener; |
|
12
|
|
|
|
|
13
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\EventListener\JmsPostSerializeListener; |
|
14
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\EventListener\JmsPreSerializeListener; |
|
15
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\EventSubscriber\Bukashk0zzzSerializationEventSubscriber; |
|
16
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Fixtures\User; |
|
17
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Fixtures\UserPictures; |
|
18
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
19
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
|
20
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
21
|
|
|
use JMS\Serializer\DeserializationContext; |
|
22
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
|
23
|
|
|
use JMS\Serializer\EventDispatcher\Events as JmsEvents; |
|
24
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
25
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
|
26
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyEventDispatcher; |
|
27
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
28
|
|
|
use Vich\UploaderBundle\Storage\StorageInterface; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* JmsSerializeEventsManager |
|
32
|
|
|
*/ |
|
33
|
|
|
class JmsSerializeEventsManager |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var EventDispatcher Dispatcher |
|
37
|
|
|
*/ |
|
38
|
|
|
private $dispatcher; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var CachedReader Cached annotation reader |
|
42
|
|
|
*/ |
|
43
|
|
|
private $annotationReader; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var SymfonyEventDispatcher |
|
47
|
|
|
*/ |
|
48
|
|
|
private $symfonyEventDispatcher; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* JmsSerializeEventsManager constructor. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache()); |
|
56
|
|
|
$this->symfonyEventDispatcher = new SymfonyEventDispatcher(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Add post & pre serialize event listeners |
|
61
|
|
|
*/ |
|
62
|
|
|
public function addEventListeners(RequestContext $requestContext, CacheManager $cacheManager, StorageInterface $vichStorage): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->dispatcher = new EventDispatcher(); |
|
65
|
|
|
$this->addEvents($this->dispatcher, $requestContext, $cacheManager, $vichStorage); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param User|UserPictures $user |
|
70
|
|
|
*/ |
|
71
|
|
|
public function dispatchEvents(DeserializationContext $context, $user): ObjectEvent |
|
72
|
|
|
{ |
|
73
|
|
|
/** @noinspection PhpParamsInspection */ |
|
74
|
|
|
$event = new ObjectEvent($context, $user, []); |
|
75
|
|
|
$this->dispatcher->dispatch(JmsEvents::PRE_SERIALIZE, User::class, $context->getFormat(), $event); |
|
76
|
|
|
$this->dispatcher->dispatch(JmsEvents::POST_SERIALIZE, User::class, $context->getFormat(), $event); |
|
77
|
|
|
|
|
78
|
|
|
return $event; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** @noinspection MoreThanThreeArgumentsInspection |
|
82
|
|
|
* Add post & pre serialize event to dispatcher |
|
83
|
|
|
* |
|
84
|
|
|
* @param mixed[] $config JMS serializer listner config |
|
85
|
|
|
*/ |
|
86
|
|
|
public function addEvents(EventDispatcher $dispatcher, RequestContext $requestContext, CacheManager $cacheManager, StorageInterface $vichStorage, array $config = []): void |
|
87
|
|
|
{ |
|
88
|
|
|
if (\count($config) === 0) { |
|
89
|
|
|
$config = [ |
|
90
|
|
|
'includeHost' => true, |
|
91
|
|
|
'vichUploaderSerialize' => true, |
|
92
|
|
|
'includeOriginal' => false, |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$preListener = new JmsPreSerializeListener($requestContext, $this->annotationReader, $cacheManager, $vichStorage, $this->symfonyEventDispatcher, $config); |
|
97
|
|
|
$postListener = new JmsPostSerializeListener($requestContext, $this->annotationReader, $cacheManager, $vichStorage, $this->symfonyEventDispatcher, $config); |
|
98
|
|
|
|
|
99
|
|
|
$dispatcher->addListener(JmsEvents::PRE_SERIALIZE, [$preListener, 'onPreSerialize']); |
|
100
|
|
|
$dispatcher->addListener(JmsEvents::POST_SERIALIZE, [$postListener, 'onPostSerialize']); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Add normalizer subscriber |
|
105
|
|
|
*/ |
|
106
|
|
|
public function addNormalizerSubscriber(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$this->symfonyEventDispatcher->addSubscriber(new Bukashk0zzzSerializationEventSubscriber()); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|