1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Discovery; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use Http\Discovery\Exception\StrategyUnavailableException; |
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
|
|
|
* @param HttpClient $httpClient |
29
|
|
|
*/ |
30
|
|
|
public function __construct(HttpClient $httpClient) |
31
|
|
|
{ |
32
|
|
|
static::$client = $httpClient; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public static function getCandidates($type) |
39
|
|
|
{ |
40
|
|
|
if (static::$client !== null && $type == HttpClient::class) { |
|
|
|
|
41
|
|
|
return [['class' => function() { return static::$client; }]]; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return []; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Make sure to use our custom strategy |
49
|
|
|
* @param Event $e |
50
|
|
|
*/ |
51
|
|
|
public function onEvent(Event $e) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
HttpClientDiscovery::prependStrategy(self::class); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Whenever these events occur we make sure to add our strategy to the discovery. |
58
|
|
|
* |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public static function getSubscribedEvents() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
KernelEvents::REQUEST => ['onEvent', 1024], |
65
|
|
|
ConsoleEvents::COMMAND => ['onEvent', 1024], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
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: