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

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