1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Discovery; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use Http\Client\HttpAsyncClient; |
7
|
|
|
use Http\Discovery\HttpClientDiscovery; |
8
|
|
|
use Http\Discovery\Strategy\DiscoveryStrategy; |
9
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
10
|
|
|
use Symfony\Component\EventDispatcher\Event; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
12
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A strategy that provide clients configured with HTTPlug bundle. With help from this strategy |
16
|
|
|
* we can use the web debug toolbar for clients found with the discovery. |
17
|
|
|
* |
18
|
|
|
* @author Tobias Nyholm <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var HttpClient |
24
|
|
|
*/ |
25
|
|
|
private static $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var HttpAsyncClient |
29
|
|
|
*/ |
30
|
|
|
private static $asyncClient; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param HttpClient $httpClient |
34
|
|
|
* @param HttpAsyncClient $asyncClient |
35
|
|
|
*/ |
36
|
|
|
public function __construct(HttpClient $httpClient = null, HttpAsyncClient $asyncClient = null) |
37
|
|
|
{ |
38
|
|
|
static::$client = $httpClient; |
|
|
|
|
39
|
|
|
static::$asyncClient = $asyncClient; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public static function getCandidates($type) |
46
|
|
|
{ |
47
|
|
|
if ($type === HttpClient::class && static::$client !== null) { |
|
|
|
|
48
|
|
|
return [['class' => function () { |
49
|
|
|
return static::$client; |
|
|
|
|
50
|
|
|
}]]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($type === HttpAsyncClient::class && static::$asyncClient !== null) { |
|
|
|
|
54
|
|
|
return [['class' => function () { |
55
|
|
|
return static::$asyncClient; |
|
|
|
|
56
|
|
|
}]]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Make sure to use our custom strategy. |
64
|
|
|
* |
65
|
|
|
* @param Event $e |
66
|
|
|
*/ |
67
|
|
|
public function onEvent(Event $e) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
HttpClientDiscovery::prependStrategy(self::class); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Whenever these events occur we make sure to add our strategy to the discovery. |
74
|
|
|
* |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public static function getSubscribedEvents() |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
|
|
KernelEvents::REQUEST => ['onEvent', 1024], |
81
|
|
|
ConsoleEvents::COMMAND => ['onEvent', 1024], |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: