1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use Http\HttplugBundle\Util\HttplugFactory; |
7
|
|
|
use Http\Message\MessageFactory; |
8
|
|
|
use Http\Message\StreamFactory; |
9
|
|
|
use Http\Message\UriFactory; |
10
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
12
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
13
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Adds fallback and discovery services. |
17
|
|
|
* |
18
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class DiscoveryPass implements CompilerPassInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Fallback services and classes. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $services = [ |
28
|
|
|
'client' => HttpClient::class, |
29
|
|
|
'message_factory' => MessageFactory::class, |
30
|
|
|
'uri_factory' => UriFactory::class, |
31
|
|
|
'stream_factory' => StreamFactory::class, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function process(ContainerBuilder $container) |
38
|
|
|
{ |
39
|
|
|
$useDiscovery = false; |
40
|
|
|
|
41
|
|
|
foreach ($this->services as $service => $class) { |
42
|
|
|
$serviceId = sprintf('httplug.%s.default', $service); |
43
|
|
|
|
44
|
|
|
if (false === $container->has($serviceId)) { |
45
|
|
|
// Register and create factory for the first time |
46
|
|
|
if (false === $useDiscovery) { |
47
|
|
|
$this->registerFactory($container); |
48
|
|
|
|
49
|
|
|
$factory = [ |
50
|
|
|
new Reference('httplug.factory'), |
51
|
|
|
'find', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
$useDiscovery = true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$definition = $container->register($serviceId, $class); |
58
|
|
|
|
59
|
|
|
$definition->setFactory($factory); |
|
|
|
|
60
|
|
|
$definition->addArgument($class); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param ContainerBuilder $container |
67
|
|
|
* |
68
|
|
|
* @throws RuntimeException |
69
|
|
|
*/ |
70
|
|
|
private function registerFactory(ContainerBuilder $container) |
71
|
|
|
{ |
72
|
|
|
if (false === $container->has('puli.discovery')) { |
73
|
|
|
throw new RuntimeException( |
74
|
|
|
'You need to install puli/symfony-bundle or add configuration at httplug.classes in order to use this bundle. Refer to http://docs.php-http/en/latest/integrations/index.html' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$definition = $container->register('httplug.factory', HttplugFactory::class); |
79
|
|
|
|
80
|
|
|
$definition |
81
|
|
|
->addArgument(new Reference('puli.discovery')) |
82
|
|
|
->setPublic(false) |
83
|
|
|
; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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: