[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/php-ai-client/src/Providers/Http/Abstracts/ -> AbstractClientDiscoveryStrategy.php (source)

   1  <?php
   2  
   3  declare (strict_types=1);
   4  namespace WordPress\AiClient\Providers\Http\Abstracts;
   5  
   6  use WordPress\AiClientDependencies\Http\Discovery\Psr18ClientDiscovery;
   7  use WordPress\AiClientDependencies\Http\Discovery\Strategy\DiscoveryStrategy;
   8  use WordPress\AiClientDependencies\Nyholm\Psr7\Factory\Psr17Factory;
   9  use WordPress\AiClientDependencies\Psr\Http\Client\ClientInterface;
  10  /**
  11   * Abstract discovery strategy for HTTP client implementations.
  12   *
  13   * Provides a base for registering custom HTTP client implementations
  14   * with HTTPlug's discovery mechanism. Subclasses must implement
  15   * the createClient() method to provide their specific PSR-18
  16   * HTTP client instance using the provided Psr17Factory.
  17   *
  18   * @since 1.1.0
  19   */
  20  abstract class AbstractClientDiscoveryStrategy implements DiscoveryStrategy
  21  {
  22      /**
  23       * Initializes and registers the discovery strategy.
  24       *
  25       * @since 1.1.0
  26       *
  27       * @return void
  28       */
  29      public static function init(): void
  30      {
  31          if (!class_exists('WordPress\AiClientDependencies\Http\Discovery\Psr18ClientDiscovery')) {
  32              return;
  33          }
  34          Psr18ClientDiscovery::prependStrategy(static::class);
  35      }
  36      /**
  37       * {@inheritDoc}
  38       *
  39       * @since 1.1.0
  40       *
  41       * @param string $type The type of discovery.
  42       * @return array<array<string, mixed>> The discovery candidates.
  43       */
  44      public static function getCandidates($type)
  45      {
  46          if (ClientInterface::class === $type) {
  47              return [['class' => static function () {
  48                  $psr17Factory = new Psr17Factory();
  49                  return static::createClient($psr17Factory);
  50              }]];
  51          }
  52          $psr17Factories = ['WordPress\AiClientDependencies\Psr\Http\Message\RequestFactoryInterface', 'WordPress\AiClientDependencies\Psr\Http\Message\ResponseFactoryInterface', 'WordPress\AiClientDependencies\Psr\Http\Message\ServerRequestFactoryInterface', 'WordPress\AiClientDependencies\Psr\Http\Message\StreamFactoryInterface', 'WordPress\AiClientDependencies\Psr\Http\Message\UploadedFileFactoryInterface', 'WordPress\AiClientDependencies\Psr\Http\Message\UriFactoryInterface'];
  53          if (in_array($type, $psr17Factories, \true)) {
  54              return [['class' => Psr17Factory::class]];
  55          }
  56          return [];
  57      }
  58      /**
  59       * Creates an instance of the HTTP client.
  60       *
  61       * Subclasses must implement this method to return their specific
  62       * PSR-18 HTTP client instance. The provided Psr17Factory implements
  63       * all PSR-17 interfaces (RequestFactory, ResponseFactory, StreamFactory,
  64       * etc.) and can be used to satisfy client constructor dependencies.
  65       *
  66       * @since 1.1.0
  67       *
  68       * @param Psr17Factory $psr17Factory The PSR-17 factory for creating HTTP messages.
  69       * @return ClientInterface The PSR-18 HTTP client.
  70       */
  71      abstract protected static function createClient(Psr17Factory $psr17Factory): ClientInterface;
  72  }


Generated : Sat Jun 13 09:38:55 2026 Cross-referenced by PHPXref