vendor/php-http/httplug-bundle/src/Discovery/ConfiguredClientsStrategy.php line 74

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Http\HttplugBundle\Discovery;
  4. use Http\Client\HttpAsyncClient;
  5. use Http\Client\HttpClient;
  6. use Http\Discovery\HttpClientDiscovery;
  7. use Http\Discovery\Strategy\DiscoveryStrategy;
  8. use Symfony\Component\EventDispatcher\Event as LegacyEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Kernel;
  11. use Symfony\Contracts\EventDispatcher\Event;
  12. if (Kernel::MAJOR_VERSION >= 5) {
  13.     \class_alias(Event::class, 'Http\HttplugBundle\Discovery\ConfiguredClientsStrategyEventClass');
  14. } else {
  15.     \class_alias(LegacyEvent::class, 'Http\HttplugBundle\Discovery\ConfiguredClientsStrategyEventClass');
  16. }
  17. /**
  18.  * A strategy that provide clients configured with HTTPlug bundle. With help from this strategy
  19.  * we can use the web debug toolbar for clients found with the discovery.
  20.  *
  21.  * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  22.  */
  23. class ConfiguredClientsStrategy implements DiscoveryStrategyEventSubscriberInterface
  24. {
  25.     /**
  26.      * @var HttpClient
  27.      */
  28.     private static $client;
  29.     /**
  30.      * @var HttpAsyncClient
  31.      */
  32.     private static $asyncClient;
  33.     /**
  34.      * @param HttpClient      $httpClient
  35.      * @param HttpAsyncClient $asyncClient
  36.      */
  37.     public function __construct(HttpClient $httpClient nullHttpAsyncClient $asyncClient null)
  38.     {
  39.         self::$client $httpClient;
  40.         self::$asyncClient $asyncClient;
  41.         HttpClientDiscovery::clearCache();
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public static function getCandidates($type)
  47.     {
  48.         if (HttpClient::class === $type && null !== self::$client) {
  49.             return [['class' => function () {
  50.                 return self::$client;
  51.             }]];
  52.         }
  53.         if (HttpAsyncClient::class === $type && null !== self::$asyncClient) {
  54.             return [['class' => function () {
  55.                 return self::$asyncClient;
  56.             }]];
  57.         }
  58.         return [];
  59.     }
  60.     /**
  61.      * Make sure to use our custom strategy.
  62.      */
  63.     public function onEvent(ConfiguredClientsStrategyEventClass $e)
  64.     {
  65.         HttpClientDiscovery::prependStrategy(self::class);
  66.     }
  67.     /**
  68.      * Whenever these events occur we make sure to add our strategy to the discovery.
  69.      *
  70.      * {@inheritdoc}
  71.      */
  72.     public static function getSubscribedEvents()
  73.     {
  74.         return [
  75.             'kernel.request' => ['onEvent'1024],
  76.             'console.command' => ['onEvent'1024],
  77.         ];
  78.     }
  79. }