Skip to content

Commit bcd1e38

Browse files
authored
Split listener from ConfiguredClientsStrategy (#388)
The listener only executes a static call. This avoids initializing the HttpClient on each request/command call.
1 parent 010fb44 commit bcd1e38

File tree

5 files changed

+50
-39
lines changed

5 files changed

+50
-39
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5+
## 1.19.0 - unreleased
6+
7+
### Changed
8+
9+
- `ConfiguredClientsStrategy` no longer implements `EventSubscriberInterface`,
10+
this has been moved to `ConfiguredClientsStrategyListener` to avoid initializing
11+
the strategy on every request.
12+
513
## 1.18.0 - 2020-03-30
614

715
### Added

src/Discovery/ConfiguredClientsStrategy.php

+1-32
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,14 @@
88
use Http\Client\HttpClient;
99
use Http\Discovery\HttpClientDiscovery;
1010
use Http\Discovery\Strategy\DiscoveryStrategy;
11-
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
12-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13-
use Symfony\Component\HttpKernel\Kernel;
14-
use Symfony\Contracts\EventDispatcher\Event;
15-
16-
if (Kernel::MAJOR_VERSION >= 5) {
17-
\class_alias(Event::class, 'Http\HttplugBundle\Discovery\ConfiguredClientsStrategyEventClass');
18-
} else {
19-
\class_alias(LegacyEvent::class, 'Http\HttplugBundle\Discovery\ConfiguredClientsStrategyEventClass');
20-
}
2111

2212
/**
2313
* A strategy that provide clients configured with HTTPlug bundle. With help from this strategy
2414
* we can use the web debug toolbar for clients found with the discovery.
2515
*
2616
* @author Tobias Nyholm <[email protected]>
2717
*/
28-
class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInterface
18+
class ConfiguredClientsStrategy implements DiscoveryStrategy
2919
{
3020
/**
3121
* @var HttpClient
@@ -67,25 +57,4 @@ public static function getCandidates($type)
6757

6858
return [];
6959
}
70-
71-
/**
72-
* Make sure to use our custom strategy.
73-
*/
74-
public function onEvent(ConfiguredClientsStrategyEventClass $e)
75-
{
76-
HttpClientDiscovery::prependStrategy(self::class);
77-
}
78-
79-
/**
80-
* Whenever these events occur we make sure to add our strategy to the discovery.
81-
*
82-
* {@inheritdoc}
83-
*/
84-
public static function getSubscribedEvents()
85-
{
86-
return [
87-
'kernel.request' => ['onEvent', 1024],
88-
'console.command' => ['onEvent', 1024],
89-
];
90-
}
9160
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Http\HttplugBundle\Discovery;
6+
7+
use Http\Discovery\HttpClientDiscovery;
8+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
10+
/**
11+
* @author Wouter de Jong <[email protected]>
12+
*/
13+
class ConfiguredClientsStrategyListener implements EventSubscriberInterface
14+
{
15+
/**
16+
* Make sure to use the custom strategy.
17+
*/
18+
public function onEvent()
19+
{
20+
HttpClientDiscovery::prependStrategy(ConfiguredClientsStrategy::class);
21+
}
22+
23+
/**
24+
* Whenever these events occur we make sure to add the custom strategy to the discovery.
25+
*
26+
* {@inheritdoc}
27+
*/
28+
public static function getSubscribedEvents()
29+
{
30+
return [
31+
'kernel.request' => ['onEvent', 1024],
32+
'console.command' => ['onEvent', 1024],
33+
];
34+
}
35+
}

src/Resources/config/services.xml

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
<service id="httplug.strategy" class="Http\HttplugBundle\Discovery\ConfiguredClientsStrategy">
88
<argument type="service" id="httplug.auto_discovery.auto_discovered_client" on-invalid="null"/>
99
<argument type="service" id="httplug.auto_discovery.auto_discovered_async" on-invalid="null"/>
10+
</service>
11+
12+
<service id="httplug.strategy_listener" class="Http\HttplugBundle\Discovery\ConfiguredClientsStrategyListener">
1013
<tag name="kernel.event_subscriber"/>
1114
</service>
1215

tests/Functional/DiscoveredClientsTest.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
use Http\Discovery\HttpClientDiscovery;
1111
use Http\Discovery\Strategy\CommonClassesStrategy;
1212
use Http\HttplugBundle\Collector\ProfileClient;
13-
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategy;
13+
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategyListener;
1414
use Nyholm\NSA;
1515
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
16-
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
17-
use Symfony\Component\HttpKernel\Kernel;
18-
use Symfony\Contracts\EventDispatcher\Event;
1916

2017
class DiscoveredClientsTest extends WebTestCase
2118
{
@@ -128,9 +125,8 @@ protected function setUp(): void
128125
parent::setUp();
129126

130127
// Reset values
131-
$strategy = new ConfiguredClientsStrategy(null, null);
128+
$strategy = new ConfiguredClientsStrategyListener(null, null);
132129
HttpClientDiscovery::setStrategies([CommonClassesStrategy::class]);
133-
$class = (Kernel::MAJOR_VERSION >= 5) ? Event::class : LegacyEvent::class;
134-
$strategy->onEvent(new $class());
130+
$strategy->onEvent();
135131
}
136132
}

0 commit comments

Comments
 (0)