1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Kévin Dunglas <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This source file is subject to the MIT license that is bundled |
7
|
|
|
* with this source code in the file LICENSE. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Dunglas\DoctrineJsonOdm\Bundle\DependencyInjection; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Config\FileLocator; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
15
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
17
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
18
|
|
|
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Kévin Dunglas <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class DunglasDoctrineJsonOdmExtension extends Extension implements PrependExtensionInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function prepend(ContainerBuilder $container) |
29
|
|
|
{ |
30
|
|
|
if (empty($frameworkConfiguration = $container->getExtensionConfig('framework'))) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
if (!isset($frameworkConfiguration['serializer']) || !isset($frameworkConfiguration['serializer']['enabled'])) { |
|
|
|
|
35
|
|
|
$container->prependExtensionConfig('framework', ['serializer' => ['enabled' => true]]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
if (!isset($frameworkConfiguration['property_info']) || !isset($frameworkConfiguration['property_info']['enabled'])) { |
|
|
|
|
39
|
|
|
$container->prependExtensionConfig('framework', ['property_info' => ['enabled' => true]]); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function load(array $configs, ContainerBuilder $container) |
47
|
|
|
{ |
48
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
49
|
|
|
$loader->load('services.xml'); |
50
|
|
|
|
51
|
|
|
$this->defineArraySerializerIfRequired($container); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* BC layer for Symfony Framework Bundle <= 3.1.6. |
56
|
|
|
* |
57
|
|
|
* @param ContainerBuilder $container |
58
|
|
|
*/ |
59
|
|
|
private function defineArraySerializerIfRequired(ContainerBuilder $container): void |
60
|
|
|
{ |
61
|
|
|
if (!$container->hasDefinition('serializer.denormalizer.array')) { |
62
|
|
|
$container->setDefinition('serializer.denormalizer.array', new Definition(ArrayDenormalizer::class)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.