Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
remove auto-discovery of php-http adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Feb 1, 2016
1 parent 3e3770f commit 827c832
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 69 deletions.
25 changes: 5 additions & 20 deletions README.md
Expand Up @@ -10,8 +10,7 @@
Provides a httpcache warmup command for Symfony2. The command simply executes a `GET` request on a list of urls.
One or more url providers must be registered. This bundle requires an implementation of
[php-http/httplug](https://packagist.org/packages/php-http/httplug) and
[php-http/message-factory](https://packagist.org/packages/php-http/message-factory). The bundle will try and
auto-discover these if not configured directly.
[php-http/message-factory](https://packagist.org/packages/php-http/message-factory).

## Installation

Expand Down Expand Up @@ -47,20 +46,6 @@ zenstruck_cache:
message_factory: Acme\MyMessageFactory # or a service (acme.my_message_factory)
```

If left blank, the bundle will try and auto-discover these classes. The following HTTP clients and
message factories are currently auto-discoverable:

* [guzzle6-adapter](https://packagist.org/packages/php-http/guzzle6-adapter) (Provides HttpClient
and allows discovery of MessageFactory)

composer require php-http/guzzle6-adapter


* [guzzle5-adapter](https://packagist.org/packages/php-http/guzzle5-adapter) (Provides HttpClient)
and [guzzlehttp/psr7](https://packagist.org/packages/guzzlehttp/psr7) (allows discovery of MessageFactory)

composer require php-http/guzzle5-adapter guzzlephp/psr7

## HttpCache Warmup Command

Usage:
Expand Down Expand Up @@ -128,11 +113,11 @@ zenstruck_cache:

```yaml
zenstruck_cache:
# Either a class or a service that implements Http\Client\HttpClient. Leave blank to attempt auto discovery.
http_client: ~
# Either a class or a service that implements Http\Client\HttpClient.
http_client: ~ # Required

# Either a class or a service that implements Http\Message\MessageFactory. Leave blank to attempt auto discovery.
message_factory: ~
# Either a class or a service that implements Http\Message\MessageFactory.
message_factory: ~ # Required

sitemap_provider:
enabled: false
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Expand Up @@ -14,14 +14,15 @@
"require": {
"symfony/framework-bundle": "^2.5|^3.0",
"symfony/console": "^2.5|^3.0",
"php-http/httplug": "^1.0@dev",
"php-http/httplug": "^1.0",
"php-http/message-factory": "^1.0",
"php-http/client-implementation": "^1.0"
},
"require-dev": {
"symfony/dom-crawler": "^2.5|^3.0",
"symfony/css-selector": "^2.5|^3.0",
"matthiasnoback/symfony-dependency-injection-test": "^0.7.4",
"php-http/guzzle5-adapter": "^0.2@dev",
"php-http/guzzle5-adapter": "^0.4@dev",
"guzzlehttp/psr7": "^1.0",
"sllh/php-cs-fixer-styleci-bridge": "^1.4"
},
Expand Down
8 changes: 4 additions & 4 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -18,12 +18,12 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->scalarNode('http_client')
->info('Either a class or a service that implements Http\Client\HttpClient. Leave blank to attempt auto discovery.')
->defaultNull()
->info('Either a class or a service that implements Http\Client\HttpClient.')
->isRequired()
->end()
->scalarNode('message_factory')
->info('Either a class or a service that implements Http\Message\MessageFactory. Leave blank to attempt auto discovery.')
->defaultNull()
->info('Either a class or a service that implements Http\Message\MessageFactory.')
->isRequired()
->end()
->arrayNode('sitemap_provider')
->canBeEnabled()
Expand Down
35 changes: 2 additions & 33 deletions src/DependencyInjection/ZenstruckCacheExtension.php
Expand Up @@ -2,9 +2,6 @@

namespace Zenstruck\CacheBundle\DependencyInjection;

use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\NotFoundException;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -35,13 +32,11 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
}

/**
* @param string|null $httpClient
* @param string $httpClient
* @param ContainerBuilder $container
*/
private function configureHttpClient($httpClient, ContainerBuilder $container)
{
$httpClient = $httpClient ?: $this->autoDiscoverHttpClient();

if (!class_exists($httpClient)) {
// is a service
$container->setAlias('zenstruck_cache.http_client', $httpClient);
Expand Down Expand Up @@ -69,13 +64,11 @@ private function configureHttpClient($httpClient, ContainerBuilder $container)
}

/**
* @param string|null $messageFactory
* @param string $messageFactory
* @param ContainerBuilder $container
*/
private function configureMessageFactory($messageFactory, ContainerBuilder $container)
{
$messageFactory = $messageFactory ?: $this->autoDiscoverMessageFactory();

if (!class_exists($messageFactory)) {
// is a service
$container->setAlias('zenstruck_cache.message_factory', $messageFactory);
Expand All @@ -101,28 +94,4 @@ private function configureMessageFactory($messageFactory, ContainerBuilder $cont
$messageFactory->setPublic(false);
$container->setDefinition('zenstruck_cache.message_factory', $messageFactory);
}

/**
* @return string
*/
private function autoDiscoverHttpClient()
{
try {
return get_class(HttpClientDiscovery::find());
} catch (NotFoundException $e) {
throw new InvalidConfigurationException('A HttpClient was not found, please define one in your configuration.', 0, $e);
}
}

/**
* @return string
*/
private function autoDiscoverMessageFactory()
{
try {
return get_class(MessageFactoryDiscovery::find());
} catch (NotFoundException $e) {
throw new InvalidConfigurationException('A MessageFactory was not found, please define one in your configuration.', 0, $e);
}
}
}
20 changes: 10 additions & 10 deletions tests/DependencyInjection/ZenstruckCacheExtensionTest.php
Expand Up @@ -13,24 +13,24 @@
*/
class ZenstruckCacheExtensionTest extends AbstractExtensionTestCase
{
public function testAutoDiscoverHttpClient()
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage The child node "http_client" at path "zenstruck_cache" must be configured.
*/
public function testThrowsExceptionWhenMissingHttpClient()
{
$this->markTestIncomplete();

$this->load(['message_factory' => 'bar']);
$this->compile();

$this->assertContainerBuilderHasService('zenstruck_cache.http_client', 'Http\Adapter\Guzzle5HttpAdapter');
}

public function testAutoDiscoverMessageFactory()
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage The child node "message_factory" at path "zenstruck_cache" must be configured.
*/
public function testThrowsExceptionWhenMissingMessageFactory()
{
$this->markTestIncomplete();

$this->load(['http_client' => 'foo']);
$this->compile();

$this->assertContainerBuilderHasService('zenstruck_cache.message_factory', 'Http\Discovery\MessageFactory\GuzzleFactory');
}

public function testClientAndFactoryAsService()
Expand Down

0 comments on commit 827c832

Please sign in to comment.