Skip to content

Commit

Permalink
Add handlers and subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Mar 26, 2024
1 parent f305206 commit f5130d3
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace PewPew\Hydrator\JMS;

use JMS\Serializer\EventDispatcher\EventDispatcher;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\Handler\HandlerRegistry;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder;
Expand All @@ -29,18 +33,35 @@ public function __construct(SerializerBuilder $builder)
/**
* @param array<class-string, non-empty-string> $configs Sets a map of
* namespace prefixes to directories.
* @param iterable<array-key, SubscribingHandlerInterface> $handlers List
* of subscribing handlers.
* @param iterable<array-key, EventSubscriberInterface> $subscribers List
* of event subscribers.
*
* @psalm-suppress InternalClass
* @psalm-suppress TooManyArguments
*/
public static function create(
array $configs = [],
bool $debug = false,
iterable $handlers = [],
iterable $subscribers = [],
CacheItemPoolInterface $cache = null,
): self {
$builder = SerializerBuilder::create()
->setDebug($debug)
->setMetadataDirs($configs);
->addDefaultHandlers()
->setMetadataDirs($configs)
->configureHandlers(static function (HandlerRegistry $registry) use ($handlers): void {
foreach ($handlers as $handler) {
$registry->registerSubscribingHandler($handler);
}
})
->configureListeners(static function (EventDispatcher $dispatcher) use ($subscribers): void {
foreach ($subscribers as $subscriber) {
$dispatcher->addSubscriber($subscriber);
}
});

if ($cache !== null) {
$builder->setMetadataCache(new MetadataPsrCacheAdapter('jms_hydrator', $cache));
Expand Down

0 comments on commit f5130d3

Please sign in to comment.