1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
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 FOS\RestBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use FOS\RestBundle\Serializer\Serializer; |
15
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Checks if a serializer is either set or can be auto-configured. |
21
|
|
|
* |
22
|
|
|
* @author Christian Flothmann <[email protected]> |
23
|
|
|
* @author Florian Voutzinos <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @internal |
26
|
|
|
*/ |
27
|
|
|
final class SerializerConfigurationPass implements CompilerPassInterface |
28
|
|
|
{ |
29
|
19 |
|
public function process(ContainerBuilder $container): void |
30
|
|
|
{ |
31
|
19 |
|
if ($container->has('fos_rest.serializer')) { |
32
|
1 |
|
$class = $container->getParameterBag()->resolveValue( |
33
|
1 |
|
$container->findDefinition('fos_rest.serializer')->getClass() |
34
|
|
|
); |
35
|
1 |
|
if (!is_subclass_of($class, Serializer::class)) { |
|
|
|
|
36
|
|
|
throw new \InvalidArgumentException(sprintf('"fos_rest.serializer" must implement %s (instance of "%s" given).', Serializer::class, $class)); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
18 |
|
if (!$container->has('serializer') && !$container->has('jms_serializer.serializer')) { |
43
|
1 |
|
throw new \InvalidArgumentException('Neither a service called "jms_serializer.serializer" nor "serializer" is available and no serializer is explicitly configured. You must either enable the JMSSerializerBundle, enable the FrameworkBundle serializer or configure a custom serializer.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
17 |
|
if ($container->has('jms_serializer.serializer')) { |
47
|
7 |
|
$container->setAlias('fos_rest.serializer', 'fos_rest.serializer.jms'); |
48
|
|
|
|
49
|
7 |
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// As there is no `jms_serializer.serializer` service, there is a `serializer` service |
53
|
10 |
|
$class = $container->getParameterBag()->resolveValue( |
54
|
10 |
|
$container->findDefinition('serializer')->getClass() |
55
|
|
|
); |
56
|
|
|
|
57
|
10 |
|
if (is_subclass_of($class, SerializerInterface::class)) { |
|
|
|
|
58
|
10 |
|
$container->setAlias('fos_rest.serializer', 'fos_rest.serializer.symfony'); |
59
|
|
|
} elseif (is_subclass_of($class, Serializer::class)) { |
|
|
|
|
60
|
|
|
$container->setAlias('fos_rest.serializer', 'serializer'); |
61
|
|
|
} else { |
62
|
|
|
throw new \InvalidArgumentException(sprintf('The class of the "serializer" service in use is not supported (instance of "%s" given). Please make it implement %s or configure the service "fos_rest.serializer" with a class implementing %s.', $class, Serializer::class, Serializer::class)); |
63
|
|
|
} |
64
|
10 |
|
} |
65
|
|
|
} |
66
|
|
|
|