|
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\Tests\Fixtures\UserPictures; |
|
15
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Tests\Fixtures\User; |
|
16
|
|
|
use JMS\Serializer\DeserializationContext; |
|
17
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
18
|
|
|
use JMS\Serializer\EventDispatcher\Events as JmsEvents; |
|
19
|
|
|
use JMS\Serializer\EventDispatcher\EventDispatcher; |
|
20
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\EventListener\JmsSerializeListener; |
|
21
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
|
22
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
23
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
24
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
25
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
|
26
|
|
|
use Vich\UploaderBundle\Storage\StorageInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* JmsSerializeEventsManager |
|
30
|
|
|
* |
|
31
|
|
|
* @author Denis Golubovskiy <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
class JmsSerializeEventsManager |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var EventDispatcher $dispatcher Dispatcher |
|
37
|
|
|
*/ |
|
38
|
|
|
private $dispatcher; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var CachedReader $annotationReader Cached annotation reader |
|
42
|
|
|
*/ |
|
43
|
|
|
private $annotationReader; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* JmsSerializeEventsManager constructor. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct() |
|
49
|
|
|
{ |
|
50
|
|
|
$this->annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Add post & pre serialize event listeners |
|
55
|
|
|
* |
|
56
|
|
|
* @param RequestContext $requestContext |
|
57
|
|
|
* @param CacheManager $cacheManager |
|
58
|
|
|
* @param StorageInterface $vichStorage |
|
59
|
|
|
*/ |
|
60
|
|
|
public function addEventListeners(RequestContext $requestContext, CacheManager $cacheManager, StorageInterface $vichStorage) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->dispatcher = new EventDispatcher(); |
|
63
|
|
|
$this->addEvents($this->dispatcher, $requestContext, $cacheManager, $vichStorage, []); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param DeserializationContext $context |
|
68
|
|
|
* @param User|UserPictures $user |
|
69
|
|
|
* @return ObjectEvent |
|
70
|
|
|
*/ |
|
71
|
|
|
public function dispatchEvents($context, $user) |
|
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
|
|
|
* |
|
83
|
|
|
* Add post & pre serialize event to dispatcher |
|
84
|
|
|
* |
|
85
|
|
|
* @param EventDispatcher $dispatcher |
|
86
|
|
|
* @param RequestContext $requestContext |
|
87
|
|
|
* @param CacheManager $cacheManager |
|
88
|
|
|
* @param StorageInterface $vichStorage |
|
89
|
|
|
* @param array $config JMS serializer listner config |
|
90
|
|
|
*/ |
|
91
|
|
|
public function addEvents(EventDispatcher $dispatcher, RequestContext $requestContext, CacheManager $cacheManager, StorageInterface $vichStorage, array $config = []) |
|
92
|
|
|
{ |
|
93
|
|
|
if (count($config) === 0) { |
|
94
|
|
|
$config = [ |
|
95
|
|
|
'includeHost' => true, |
|
96
|
|
|
'vichUploaderSerialize' => true, |
|
97
|
|
|
'includeOriginal' => false, |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$listener = new JmsSerializeListener($requestContext, $this->annotationReader, $cacheManager, $vichStorage, $config); |
|
102
|
|
|
|
|
103
|
|
|
$dispatcher->addListener(JmsEvents::PRE_SERIALIZE, [$listener, 'onPreSerialize']); |
|
104
|
|
|
$dispatcher->addListener(JmsEvents::POST_SERIALIZE, [$listener, 'onPostSerialize']); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|