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
|
|
|
final class DiscoveryPass implements CompilerPassInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Fallback services and classes. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $services = [ |
18
|
|
|
'client' => 'Http\Client\HttpClient', |
19
|
|
|
'message_factory' => 'Http\Message\MessageFactory', |
20
|
|
|
'uri_factory' => 'Http\Message\UriFactory', |
21
|
|
|
'stream_factory' => 'Http\Message\StreamFactory', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function process(ContainerBuilder $container) |
28
|
|
|
{ |
29
|
|
|
$useDiscovery = false; |
30
|
|
|
|
31
|
|
|
foreach ($this->services as $service => $class) { |
32
|
|
|
$serviceId = sprintf('httplug.%s.default', $service); |
33
|
|
|
|
34
|
|
|
if (false === $container->has($serviceId)) { |
35
|
|
|
// Register and create factory for the first time |
36
|
|
|
if (false === $useDiscovery) { |
37
|
|
|
$this->registerFactory($container); |
38
|
|
|
|
39
|
|
|
$factory = [ |
40
|
|
|
new Reference('httplug.factory'), |
41
|
|
|
'find', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
$useDiscovery = true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$definition = $container->register($serviceId, $class); |
48
|
|
|
|
49
|
|
|
$definition->setFactory($factory); |
|
|
|
|
50
|
|
|
$definition->addArgument($class); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ContainerBuilder $container |
57
|
|
|
* |
58
|
|
|
* @throws RuntimeException |
59
|
|
|
*/ |
60
|
|
|
private function registerFactory(ContainerBuilder $container) |
61
|
|
|
{ |
62
|
|
|
if (false === $container->has('puli.discovery')) { |
63
|
|
|
throw new RuntimeException( |
64
|
|
|
'You need to install puli/symfony-bundle or add configuration at httplug.classes in order to use this bundle. Refer to http://some.doc' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$definition = $container->register('httplug.factory', 'Http\HttplugBundle\Util\HttplugFactory'); |
69
|
|
|
|
70
|
|
|
$definition |
71
|
|
|
->addArgument(new Reference('puli.discovery')) |
72
|
|
|
->setPublic(false) |
73
|
|
|
; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
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: