Skip to content

add plugin client factory discovery #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
"require": {
"php": "^5.5 || ^7.0"
},
"repositories": {
"client-common": {
"type": "vcs",
"url": "git@github.com:fbourigault/client-common"
}
},
"require-dev": {
"php-http/client-common": "dev-plugin-client-factory",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just so we don't forget: we have to remove this before merging, when client-common is merged and released

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is in the to do list :)

"php-http/httplug": "^1.0",
"php-http/message-factory": "^1.0",
"puli/composer-plugin": "1.0.0-beta10",
Expand Down
41 changes: 41 additions & 0 deletions spec/PluginClientFactoryDiscoverySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace spec\Http\Discovery;

use Http\Client\Common\PluginClientFactoryInterface;
use Http\Discovery\ClassDiscovery;
use Http\Discovery\NotFoundException;
use Http\Discovery\Strategy\DiscoveryStrategy;
use PhpSpec\ObjectBehavior;
use spec\Http\Discovery\Helper\DiscoveryHelper;

class PluginClientFactoryDiscoverySpec extends ObjectBehavior
{
function let()
{
ClassDiscovery::setStrategies([DiscoveryHelper::class]);
DiscoveryHelper::clearClasses();
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Discovery\PluginClientFactoryDiscovery');
}

function it_is_a_class_discovery()
{
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
}

function it_finds_a_plugin_client_factory(DiscoveryStrategy $strategy) {

$candidate = ['class' => 'spec\Http\Discovery\Stub\PluginClientFactoryStub', 'condition' => true];
DiscoveryHelper::setClasses(PluginClientFactoryInterface::class, [$candidate]);

$this->find()->shouldImplement('Http\Client\Common\PluginClientFactoryInterface');
}

function it_throw_exception(DiscoveryStrategy $strategy) {
$this->shouldThrow(NotFoundException::class)->duringFind();
}
}
13 changes: 13 additions & 0 deletions spec/Stub/PluginClientFactoryStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace spec\Http\Discovery\Stub;

use Http\Client\Common\PluginClientFactoryInterface;

class PluginClientFactoryStub implements PluginClientFactoryInterface
{
public function createClient($client, array $plugins = [], array $options = [])
{

}
}
36 changes: 36 additions & 0 deletions src/PluginClientFactoryDiscovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Http\Discovery;

use Http\Client\Common\PluginClientFactoryInterface;
use Http\Discovery\Exception\DiscoveryFailedException;

/**
* Finds a PluginClientFactoryInterface implementation.
*
* @author Fabien Bourigault <bourigaultfabien@gmail.com>
*/
final class PluginClientFactoryDiscovery extends ClassDiscovery
{
/**
* Finds a PluginClientFactoryInterface implementation.
*
* @return PluginClientFactoryInterface
*
* @throws Exception\NotFoundException
*/
public static function find()
{
try {
$client = static::findOneByType(PluginClientFactoryInterface::class);
} catch (DiscoveryFailedException $e) {
throw new NotFoundException(
'No HTTPlug plugin clients found. Make sure to install "php-http/client-common".',
0,
$e
);
}

return static::instantiateClass($client);
}
}
4 changes: 4 additions & 0 deletions src/Strategy/CommonClassesStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Http\Discovery\Strategy;

use GuzzleHttp\Psr7\Request as GuzzleRequest;
use Http\Client\Common\PluginClientFactory;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use Http\Message\StreamFactory\GuzzleStreamFactory;
use Http\Message\UriFactory\GuzzleUriFactory;
Expand Down Expand Up @@ -60,6 +61,9 @@ final class CommonClassesStrategy implements DiscoveryStrategy
['class' => Buzz::class, 'condition' => Buzz::class],
['class' => React::class, 'condition' => React::class],
],
'Http\Client\Common\PluginClientFactoryInterface' => [
['class' => PluginClientFactory::class, 'condition' => PluginClientFactory::class],
],
];

/**
Expand Down