Skip to content

Add a discovery strategy #85

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

Merged
merged 5 commits into from
Jul 15, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
43 changes: 43 additions & 0 deletions ConfiguredClientsStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Http\HttplugBundle;
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we put this in a sub-namespace "Discovery"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.


use Http\Client\HttpClient;
use Http\Discovery\Exception\StrategyUnavailableException;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Strategy\DiscoveryStrategy;

/**
* A strategy that provide clients configured with HTTPlug bundle.
*
* @author Tobias Nyholm <[email protected]>
*/
class ConfiguredClientsStrategy implements DiscoveryStrategy
{
/**
* @var HttpClient
*/
private static $client;

/**
* @param HttpClient $httpClient
*/
public function __construct(HttpClient $httpClient)
{
static::$client = $httpClient;

HttpClientDiscovery::prependStrategy(self::class);
}

/**
* {@inheritdoc}
*/
public static function getCandidates($type)
{
if (static::$client !== null && $type == 'Http\Client\HttpClient') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we use HttpClient::class or do we need to support too old php versions?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could, We don't do it in CommonClassesStrategy. But I don't know why i didn't use the ::class constant there.

I should probably update here anyhow. Thank you.

return [['class' => function() { return static::$client; }]];
}

return [];
}
}
4 changes: 4 additions & 0 deletions DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function load(array $configs, ContainerBuilder $container)
}
}

// Set main aliases
foreach ($config['main_alias'] as $type => $id) {
$container->setAlias(sprintf('httplug.%s', $type), $id);
}
Expand All @@ -69,9 +70,12 @@ public function load(array $configs, ContainerBuilder $container)
*/
private function configureClients(ContainerBuilder $container, array $config)
{
// If we have a client named 'default'
$first = isset($config['clients']['default']) ? 'default' : null;

foreach ($config['clients'] as $name => $arguments) {
if ($first === null) {
// Save the name of the first configurated client.
$first = $name;
}

Expand Down
3 changes: 3 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="httplug.strategy" class="Http\HttplugBundle\ConfiguredClientsStrategy" public="true">
<argument type="service" id="httplug.client"/>
</service>

<!-- ClientFactories -->
<service id="httplug.factory.buzz" class="Http\HttplugBundle\ClientFactory\BuzzFactory" public="false">
Expand Down