-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializerBundle.php
70 lines (59 loc) · 2.06 KB
/
SerializerBundle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace Pandawa\Bundle\SerializerBundle;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Support\DeferrableProvider;
use Pandawa\Bundle\DependencyInjectionBundle\Plugin\ImportServicesPlugin;
use Pandawa\Bundle\FoundationBundle\Plugin\ImportConfigurationPlugin;
use Pandawa\Component\Foundation\Bundle\Bundle;
use Pandawa\Contracts\Foundation\HasPluginInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @author Iqbal Maulana <iq.bluejack@gmail.com>
*/
class SerializerBundle extends Bundle implements HasPluginInterface, DeferrableProvider
{
protected array $deferred = [
SerializerInterface::class,
Serializer::class,
'serializer.default',
'serializer.factory',
'serializer.encoder.json',
'serializer.encoder.xml',
'serializer.encoder.yaml',
'serializer.encoder.csv',
'serializer.name_converter.camel_to_snake',
'serializer.normalizer.object',
'serializer.normalizer.date_time',
];
public function register(): void
{
$this->configurePlugin();
foreach ($this->getConfig('serializers') as $name => $config) {
$serviceName = sprintf('serializer.%s', $name);
$this->app->singleton($serviceName, function (Container $container) use ($config) {
return $container->get('serializer.factory')->create(
$config['normalizers'],
$config['encoders'],
);
});
$this->deferred[] = $serviceName;
}
$this->app->alias(
sprintf('serializer.%s', $this->getConfig('default')),
SerializerInterface::class
);
$this->app->alias(
sprintf('serializer.%s', $this->getConfig('default')),
Serializer::class
);
}
public function plugins(): array
{
return [
new ImportConfigurationPlugin(),
new ImportServicesPlugin(),
];
}
}