|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright 2016 Johannes M. Schmitt <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace JMS\Serializer\Handler; |
|
20
|
|
|
|
|
21
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
23
|
|
|
|
|
24
|
|
|
class LazyHandlerRegistry extends HandlerRegistry |
|
25
|
|
|
{ |
|
26
|
|
|
private $container; |
|
27
|
|
|
private $initializedHandlers = array(); |
|
28
|
|
|
|
|
29
|
4 |
|
public function __construct($container, array $handlers = array()) |
|
30
|
|
|
{ |
|
31
|
4 |
|
if (!$container instanceof PsrContainerInterface && !$container instanceof ContainerInterface) { |
|
32
|
|
|
throw new \InvalidArgumentException(sprintf('The container must be an instance of %s or %s (%s given).', PsrContainerInterface::class, ContainerInterface::class, is_object($container) ? get_class($container) : gettype($container))); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
4 |
|
parent::__construct($handlers); |
|
36
|
4 |
|
$this->container = $container; |
|
37
|
4 |
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
public function registerHandler($direction, $typeName, $format, $handler) |
|
40
|
|
|
{ |
|
41
|
4 |
|
parent::registerHandler($direction, $typeName, $format, $handler); |
|
42
|
4 |
|
unset($this->initializedHandlers[$direction][$typeName][$format]); |
|
43
|
4 |
|
} |
|
44
|
|
|
|
|
45
|
4 |
|
public function getHandler($direction, $typeName, $format) |
|
46
|
|
|
{ |
|
47
|
4 |
|
if (isset($this->initializedHandlers[$direction][$typeName][$format])) { |
|
48
|
|
|
return $this->initializedHandlers[$direction][$typeName][$format]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
4 |
|
if (!isset($this->handlers[$direction][$typeName][$format])) { |
|
52
|
|
|
return null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
4 |
|
$handler = $this->handlers[$direction][$typeName][$format]; |
|
56
|
4 |
|
if (is_array($handler) && is_string($handler[0]) && $this->container->has($handler[0])) { |
|
57
|
2 |
|
$handler[0] = $this->container->get($handler[0]); |
|
58
|
2 |
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
return $this->initializedHandlers[$direction][$typeName][$format] = $handler; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|