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