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\Serializer; |
13
|
|
|
|
14
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
15
|
|
|
use JMS\Serializer\Handler\HandlerRegistryInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Search in the class parents to find an adapted handler. |
19
|
|
|
* |
20
|
|
|
* @author Ener-Getick <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @internal do not depend on this class directly |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
final class JMSHandlerRegistryV2 implements HandlerRegistryInterface |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
private $registry; |
27
|
|
|
|
28
|
7 |
|
public function __construct(HandlerRegistryInterface $registry) |
29
|
|
|
{ |
30
|
7 |
|
$this->registry = $registry; |
31
|
7 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function registerSubscribingHandler(SubscribingHandlerInterface $handler): void |
37
|
|
|
{ |
38
|
|
|
$this->registry->registerSubscribingHandler($handler); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function registerHandler(int $direction, string $typeName, string $format, $handler): void |
45
|
|
|
{ |
46
|
|
|
$this->registry->registerHandler($direction, $typeName, $format, $handler); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
6 |
|
public function getHandler(int $direction, string $typeName, string $format) |
53
|
|
|
{ |
54
|
6 |
|
$first = true; |
55
|
|
|
do { |
56
|
6 |
|
$handler = $this->registry->getHandler($direction, $typeName, $format); |
57
|
6 |
|
if (null !== $handler) { |
58
|
6 |
|
if (!$first && \Error::class !== $typeName && \Exception::class !== $typeName) { |
59
|
|
|
@trigger_error(sprintf('The behavior of %s is deprecated since FOSRestBundle 2.8. In version 3.0, it will only force JMS Serializer to look for Exceptions and Errors parent handlers. You should not rely on it for the parent type "%s".', __CLASS__, $typeName), E_USER_DEPRECATED); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
6 |
|
return $handler; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$first = false; |
66
|
|
|
} while ($typeName = get_parent_class($typeName)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
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.