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

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