4
4
5
5
namespace PewPew \Hydrator \JMS ;
6
6
7
+ use JMS \Serializer \EventDispatcher \EventDispatcher ;
8
+ use JMS \Serializer \EventDispatcher \EventSubscriberInterface ;
9
+ use JMS \Serializer \Handler \HandlerRegistry ;
10
+ use JMS \Serializer \Handler \SubscribingHandlerInterface ;
7
11
use JMS \Serializer \Naming \IdenticalPropertyNamingStrategy ;
8
12
use JMS \Serializer \Serializer ;
9
13
use JMS \Serializer \SerializerBuilder ;
@@ -29,18 +33,35 @@ public function __construct(SerializerBuilder $builder)
29
33
/**
30
34
* @param array<class-string, non-empty-string> $configs Sets a map of
31
35
* namespace prefixes to directories.
36
+ * @param iterable<array-key, SubscribingHandlerInterface> $handlers List
37
+ * of subscribing handlers.
38
+ * @param iterable<array-key, EventSubscriberInterface> $subscribers List
39
+ * of event subscribers.
32
40
*
33
41
* @psalm-suppress InternalClass
34
42
* @psalm-suppress TooManyArguments
35
43
*/
36
44
public static function create (
37
45
array $ configs = [],
38
46
bool $ debug = false ,
47
+ iterable $ handlers = [],
48
+ iterable $ subscribers = [],
39
49
CacheItemPoolInterface $ cache = null ,
40
50
): self {
41
51
$ builder = SerializerBuilder::create ()
42
52
->setDebug ($ debug )
43
- ->setMetadataDirs ($ configs );
53
+ ->addDefaultHandlers ()
54
+ ->setMetadataDirs ($ configs )
55
+ ->configureHandlers (static function (HandlerRegistry $ registry ) use ($ handlers ): void {
56
+ foreach ($ handlers as $ handler ) {
57
+ $ registry ->registerSubscribingHandler ($ handler );
58
+ }
59
+ })
60
+ ->configureListeners (static function (EventDispatcher $ dispatcher ) use ($ subscribers ): void {
61
+ foreach ($ subscribers as $ subscriber ) {
62
+ $ dispatcher ->addSubscriber ($ subscriber );
63
+ }
64
+ });
44
65
45
66
if ($ cache !== null ) {
46
67
$ builder ->setMetadataCache (new MetadataPsrCacheAdapter ('jms_hydrator ' , $ cache ));
0 commit comments