From c46491d5da461edad4c96736d73c13a20b40a79d Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Wed, 14 Oct 2020 10:47:10 +0200 Subject: [PATCH] Split listener from ConfiguredClientsStrategy The listener only executes a static call. This avoids initializing the HttpClient on each request/command call. --- CHANGELOG.md | 8 +++++ src/Discovery/ConfiguredClientsStrategy.php | 33 +---------------- .../ConfiguredClientsStrategyListener.php | 35 +++++++++++++++++++ src/Resources/config/services.xml | 3 ++ tests/Functional/DiscoveredClientsTest.php | 10 ++---- 5 files changed, 50 insertions(+), 39 deletions(-) create mode 100644 src/Discovery/ConfiguredClientsStrategyListener.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 47251c4d..51e53321 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release. +## 1.19.0 - unreleased + +### Changed + +- `ConfiguredClientsStrategy` no longer implements `EventSubscriberInterface`, + this has been moved to `ConfiguredClientsStrategyListener` to avoid initializing + the strategy on every request. + ## 1.18.0 - 2020-03-30 ### Added diff --git a/src/Discovery/ConfiguredClientsStrategy.php b/src/Discovery/ConfiguredClientsStrategy.php index 67336fd8..1739d7b6 100644 --- a/src/Discovery/ConfiguredClientsStrategy.php +++ b/src/Discovery/ConfiguredClientsStrategy.php @@ -8,16 +8,6 @@ use Http\Client\HttpClient; use Http\Discovery\HttpClientDiscovery; use Http\Discovery\Strategy\DiscoveryStrategy; -use Symfony\Component\EventDispatcher\Event as LegacyEvent; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Symfony\Component\HttpKernel\Kernel; -use Symfony\Contracts\EventDispatcher\Event; - -if (Kernel::MAJOR_VERSION >= 5) { - \class_alias(Event::class, 'Http\HttplugBundle\Discovery\ConfiguredClientsStrategyEventClass'); -} else { - \class_alias(LegacyEvent::class, 'Http\HttplugBundle\Discovery\ConfiguredClientsStrategyEventClass'); -} /** * A strategy that provide clients configured with HTTPlug bundle. With help from this strategy @@ -25,7 +15,7 @@ * * @author Tobias Nyholm */ -class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInterface +class ConfiguredClientsStrategy implements DiscoveryStrategy { /** * @var HttpClient @@ -67,25 +57,4 @@ public static function getCandidates($type) return []; } - - /** - * Make sure to use our custom strategy. - */ - public function onEvent(ConfiguredClientsStrategyEventClass $e) - { - HttpClientDiscovery::prependStrategy(self::class); - } - - /** - * Whenever these events occur we make sure to add our strategy to the discovery. - * - * {@inheritdoc} - */ - public static function getSubscribedEvents() - { - return [ - 'kernel.request' => ['onEvent', 1024], - 'console.command' => ['onEvent', 1024], - ]; - } } diff --git a/src/Discovery/ConfiguredClientsStrategyListener.php b/src/Discovery/ConfiguredClientsStrategyListener.php new file mode 100644 index 00000000..621c03c9 --- /dev/null +++ b/src/Discovery/ConfiguredClientsStrategyListener.php @@ -0,0 +1,35 @@ + + */ +class ConfiguredClientsStrategyListener implements EventSubscriberInterface +{ + /** + * Make sure to use the custom strategy. + */ + public function onEvent() + { + HttpClientDiscovery::prependStrategy(ConfiguredClientsStrategy::class); + } + + /** + * Whenever these events occur we make sure to add the custom strategy to the discovery. + * + * {@inheritdoc} + */ + public static function getSubscribedEvents() + { + return [ + 'kernel.request' => ['onEvent', 1024], + 'console.command' => ['onEvent', 1024], + ]; + } +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index a7d6929b..67c828bf 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -7,6 +7,9 @@ + + + diff --git a/tests/Functional/DiscoveredClientsTest.php b/tests/Functional/DiscoveredClientsTest.php index 4525ef01..fe019960 100644 --- a/tests/Functional/DiscoveredClientsTest.php +++ b/tests/Functional/DiscoveredClientsTest.php @@ -10,12 +10,9 @@ use Http\Discovery\HttpClientDiscovery; use Http\Discovery\Strategy\CommonClassesStrategy; use Http\HttplugBundle\Collector\ProfileClient; -use Http\HttplugBundle\Discovery\ConfiguredClientsStrategy; +use Http\HttplugBundle\Discovery\ConfiguredClientsStrategyListener; use Nyholm\NSA; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; -use Symfony\Component\EventDispatcher\Event as LegacyEvent; -use Symfony\Component\HttpKernel\Kernel; -use Symfony\Contracts\EventDispatcher\Event; class DiscoveredClientsTest extends WebTestCase { @@ -128,9 +125,8 @@ protected function setUp(): void parent::setUp(); // Reset values - $strategy = new ConfiguredClientsStrategy(null, null); + $strategy = new ConfiguredClientsStrategyListener(null, null); HttpClientDiscovery::setStrategies([CommonClassesStrategy::class]); - $class = (Kernel::MAJOR_VERSION >= 5) ? Event::class : LegacyEvent::class; - $strategy->onEvent(new $class()); + $strategy->onEvent(); } }