|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Symplify. |
|
7
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Symplify\NetteAdapterForSymfonyBundles; |
|
11
|
|
|
|
|
12
|
|
|
use Nette\DI\Container; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
|
16
|
|
|
use Symplify\NetteAdapterForSymfonyBundles\Exception\UnsupportedApiException; |
|
17
|
|
|
|
|
18
|
|
|
final class SymfonyContainerAdapter implements ContainerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private $symfonyToNetteServiceAliases; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Container |
|
27
|
|
|
*/ |
|
28
|
|
|
private $container; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string[] $symfonyToNetteServiceAliases |
|
32
|
|
|
* @param Container $container |
|
33
|
|
|
*/ |
|
34
|
8 |
|
public function __construct(array $symfonyToNetteServiceAliases, Container $container) |
|
35
|
|
|
{ |
|
36
|
8 |
|
$this->symfonyToNetteServiceAliases = $symfonyToNetteServiceAliases; |
|
37
|
8 |
|
$this->container = $container; |
|
38
|
8 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function set($id, $service) |
|
44
|
|
|
{ |
|
45
|
1 |
|
throw new UnsupportedApiException(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) |
|
52
|
|
|
{ |
|
53
|
3 |
|
if (isset($this->symfonyToNetteServiceAliases[$id])) { |
|
54
|
1 |
|
$id = $this->symfonyToNetteServiceAliases[$id]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
if ($this->has($id)) { |
|
58
|
2 |
|
return $this->container->getService($id); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
throw new ServiceNotFoundException( |
|
62
|
1 |
|
sprintf('Service "%s" was not found.', $id) |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
3 |
|
public function has($id) |
|
70
|
|
|
{ |
|
71
|
3 |
|
return $this->container->hasService($id); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function getParameter($name) |
|
78
|
|
|
{ |
|
79
|
3 |
|
if ($this->hasParameter($name)) { |
|
80
|
2 |
|
return $this->container->getParameters()[$name]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
throw new InvalidArgumentException( |
|
84
|
1 |
|
sprintf('Parameter "%s" was not found.', $name) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
4 |
|
public function hasParameter($name) |
|
92
|
|
|
{ |
|
93
|
4 |
|
return isset($this->container->getParameters()[$name]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function setParameter($name, $value) |
|
100
|
|
|
{ |
|
101
|
1 |
|
throw new UnsupportedApiException(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public function initialized($id) |
|
108
|
|
|
{ |
|
109
|
1 |
|
throw new UnsupportedApiException(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|