Skip to content

Include a DiscoveryStategy for php-http/discovery #4

@eclipxe13

Description

@eclipxe13

Could you consider to add a Http\Discovery\Strategy\DiscoveryStrategy implementation so it would be easy to register and to use this library when working with php-http/discovery?

I just see that php-http/discovery knows about a very limited set of PSR-17, PSR-18 implementations. But has a way to add your classes to the discover process:

// register class so `php-http/discovery` can find them
Psr18ClientDiscovery::appendStrategy(ChillerlanStrategy::class);
Psr17FactoryDiscovery::appendStrategy(hillerlanStrategy::class);


// on your library:
namespace chillerlan\HTTP;

use Http\Discovery\Strategy\DiscoveryStrategy;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Client\ClientInterface;

final class ChillerlanStrategy implements DiscoveryStrategy
{
    /**
     * {@inheritdoc}
     */
    public static function getCandidates($type)
    {
        $classes = [
            RequestFactoryInterface::class => ['chillerlan\HTTP\Psr17\RequestFactory'],
            ResponseFactoryInterface::class => ['chillerlan\HTTP\Psr17\ResponseFactory'],
            ServerRequestFactoryInterface::class => ['chillerlan\HTTP\Psr17\ServerRequestFactory'],
            StreamFactoryInterface::class => ['chillerlan\HTTP\Psr17\StreamFactory'],
            UploadedFileFactoryInterface::class => ['chillerlan\HTTP\Psr17\UploadedFileFactory'],
            UriFactoryInterface::class => ['chillerlan\HTTP\Psr17\UriFactory'],
            ClientInterface::class => ['chillerlan\HTTP\Psr18\StreamClient', 'chillerlan\HTTP\Psr18\CurlClient'],
        ];
        $candidates = [];
        if (isset($classes[$type])) {
            foreach ($classes[$type] as $class) {
                $candidates[] = ['class' => $class, 'condition' => [$class]];
            }
        }

        return $candidates;
    }
}

This is a POC that I made and works once #3 is merged.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions