1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Adds fallback and discovery services. |
12
|
|
|
* |
13
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
final class DiscoveryPass implements CompilerPassInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Fallback services and classes. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $services = [ |
23
|
|
|
'client' => 'Http\Client\HttpClient', |
24
|
|
|
'message_factory' => 'Http\Message\MessageFactory', |
25
|
|
|
'uri_factory' => 'Http\Message\UriFactory', |
26
|
|
|
'stream_factory' => 'Http\Message\StreamFactory', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function process(ContainerBuilder $container) |
33
|
|
|
{ |
34
|
|
|
$useDiscovery = false; |
35
|
|
|
|
36
|
|
|
foreach ($this->services as $service => $class) { |
37
|
|
|
$serviceId = sprintf('httplug.%s.default', $service); |
38
|
|
|
|
39
|
|
|
if (false === $container->has($serviceId)) { |
40
|
|
|
// Register and create factory for the first time |
41
|
|
|
if (false === $useDiscovery) { |
42
|
|
|
$this->registerFactory($container); |
43
|
|
|
|
44
|
|
|
$factory = [ |
45
|
|
|
new Reference('httplug.factory'), |
46
|
|
|
'find', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
$useDiscovery = true; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$definition = $container->register($serviceId, $class); |
53
|
|
|
|
54
|
|
|
$definition->setFactory($factory); |
|
|
|
|
55
|
|
|
$definition->addArgument($class); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ContainerBuilder $container |
62
|
|
|
* |
63
|
|
|
* @throws RuntimeException |
64
|
|
|
*/ |
65
|
|
|
private function registerFactory(ContainerBuilder $container) |
66
|
|
|
{ |
67
|
|
|
if (false === $container->has('puli.discovery')) { |
68
|
|
|
throw new RuntimeException( |
69
|
|
|
'You need to install puli/symfony-bundle or add configuration at httplug.classes in order to use this bundle. Refer to http://some.doc' |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$definition = $container->register('httplug.factory', 'Http\HttplugBundle\Util\HttplugFactory'); |
74
|
|
|
|
75
|
|
|
$definition |
76
|
|
|
->addArgument(new Reference('puli.discovery')) |
77
|
|
|
->setPublic(false) |
78
|
|
|
; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: