Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Commit

Permalink
Implemented aliasing feature for autowiring (#222)
Browse files Browse the repository at this point in the history
* Implemented aliasing feature for autowiring
* Updated documentation
* Added info for default_client in bundle semantic configuration
  • Loading branch information
csarrazi committed Jan 9, 2018
1 parent de4d86f commit 097cf06
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 29 deletions.
44 changes: 21 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ CsaGuzzleBundle
Description
-----------

This bundle integrates Guzzle >=4.0 in Symfony. The `1.x` branch supports Guzzle 4 and 5, and the `master` branch supports Guzzle 6.
This bundle integrates Guzzle >=4.0 in Symfony.
Versions `2.x` and `3.x` support Guzzle 6 and versions `1.x` support Guzzle 4 and 5.

Currently, it supports the following features:
Currently, the bundle supports the following features:

* Integration with Symfony's debug tools (web debug toolbar, profiler, logger, timeline, ...)
* Configuring a Guzzle client simply using configuration
* Multiple middleware / subscribers (logger, profiler, timeline, cache, mock, and more to come)
* Service descriptions to describe your services is json format (only in the 1.3 branch, though)
* Service descriptions to describe your services in json format (only in the 1.3 branch, though)

![Web debug Toolbar](https://cloud.githubusercontent.com/assets/4208880/12815019/c49faeec-cb43-11e5-9de9-dc3423ea6c35.jpg)
![Profiler panel integration](https://cloud.githubusercontent.com/assets/4208880/12815021/c4a16746-cb43-11e5-9061-f1ea15b04e62.jpg)
Expand All @@ -39,28 +40,17 @@ Here are the upgrade notes between each version.

See [Upgrade.md](UPGRADE.md).

Documentation
-------------

### Documentation for stable (2.*)

* [Installation](../2.x/src/Resources/doc/install.md)
* [Creating clients](../2.x/src/Resources/doc/clients.md)
* [Registering new middleware](../2.x/src/Resources/doc/middleware.md)
* [Available middleware](../2.x/src/Resources/doc/available_middleware.md)
* [Streaming a guzzle response](../2.x/src/Resources/doc/response_streaming.md)
* [Configuration reference](../2.x/src/Resources/doc/configuration_reference.md)
Support
-------

### Documentation for legacy (1.3)
As Guzzle 4 and Guzzle 5 are no longer supported by its creator, you should aim to migrate to Guzzle 6.x as soon as
possible. Versions `1.x` of this bundle are no longer supported, and version `2.x` of will be supported until Symfony
2.8 EOL (November 2018).

* [Creating clients](../1.3/src/Resources/doc/clients.md)
* [Registering new event subscribers](../1.3/src/Resources/doc/event_subscribers.md)
* [Available event subscribers](../1.3/src/Resources/doc/available_subscribers.md)
* [Streaming a guzzle response](../1.3/src/Resources/doc/response_streaming.md)
* [Service descriptions](../1.3/src/Resources/doc/service_descriptions.md)
* [Configuration reference](../1.3/src/Resources/doc/configuration_reference.md)
Documentation
-------------

### Documentation for next version (master)
### Documentation for stable (3.x)

* [Installation](../master/src/Resources/doc/install.md)
* [Creating clients](../master/src/Resources/doc/clients.md)
Expand All @@ -69,6 +59,15 @@ Documentation
* [Streaming a guzzle response](../master/src/Resources/doc/response_streaming.md)
* [Configuration reference](../master/src/Resources/doc/configuration_reference.md)

### Documentation for legacy (2.x)

* [Installation](../2.x/src/Resources/doc/install.md)
* [Creating clients](../2.x/src/Resources/doc/clients.md)
* [Registering new middleware](../2.x/src/Resources/doc/middleware.md)
* [Available middleware](../2.x/src/Resources/doc/available_middleware.md)
* [Streaming a guzzle response](../2.x/src/Resources/doc/response_streaming.md)
* [Configuration reference](../2.x/src/Resources/doc/configuration_reference.md)

Contributing
------------

Expand All @@ -81,4 +80,3 @@ License
This library is under Apache License 2.0. For the full copyright and license
information, please view the [LICENSE](src/Resources/meta/LICENSE) file that was
distributed with this source code.

2 changes: 2 additions & 0 deletions src/DataCollector/GuzzleCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class GuzzleCollector extends DataCollector
const MAX_BODY_SIZE = 0x10000;

private $maxBodySize;

private $history;

private $curlFormatter = null;

/**
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/CompilerPass/MiddlewarePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
class MiddlewarePass implements CompilerPassInterface
{
const MIDDLEWARE_TAG = 'csa_guzzle.middleware';

const CLIENT_TAG = 'csa_guzzle.client';

public function process(ContainerBuilder $container)
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->scalarNode('default_client')->info('The first client defined is used if not set')->end()
->append($this->createCacheNode())
->append($this->createClientsNode())
->append($this->createMockNode())
Expand Down
12 changes: 12 additions & 0 deletions src/DependencyInjection/CsaGuzzleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Csa\Bundle\GuzzleBundle\DependencyInjection;

use Csa\Bundle\GuzzleBundle\DependencyInjection\CompilerPass\MiddlewarePass;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -122,6 +124,11 @@ private function processCacheConfiguration(array $config, ContainerBuilder $cont

private function processClientsConfiguration(array $config, ContainerBuilder $container, $debug)
{
if (empty($config['default_client'])) {
$keys = array_keys($config['clients']);
$config['default_client'] = reset($keys);
}

foreach ($config['clients'] as $name => $options) {
$client = new Definition($options['class']);
$client->setLazy($options['lazy']);
Expand Down Expand Up @@ -166,6 +173,11 @@ private function processClientsConfiguration(array $config, ContainerBuilder $co
if (isset($options['alias'])) {
$container->setAlias($options['alias'], $clientServiceId);
}

if ($config['default_client'] === $name) {
$container->setAlias(ClientInterface::class, $clientServiceId);
$container->setAlias(Client::class, $clientServiceId);
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/Resources/doc/configuration_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,25 @@ csa_guzzle:
service: ~
format: '{hostname} {req_header_User-Agent} - [{date_common_log}] "{method} {target} HTTP/{version}" {code} {res_header_Content-Length}'
level: debug
default_client: ~
cache:
enabled: false
adapter: ~
clients:

# Prototype
name:
class: GuzzleHttp\Client
config: ~
middleware: []
alias: ~
class: GuzzleHttp\Client
lazy: false
config: ~
middleware: []
alias: null
mock:
enabled: false
storage_path: ~ # Required
mode: replay
request_headers_blacklist: []
response_headers_blacklist: []
```

To log request/response body you can use `{req_body}` and `{res_body}` respectively in `format` setting.
Expand Down
59 changes: 57 additions & 2 deletions src/Resources/doc/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Add the required package using composer.
### Stable version

```bash
composer require csa/guzzle-bundle:^2.0
composer require csa/guzzle-bundle:^3.0
```

### Legacy version

```bash
composer require csa/guzzle-bundle:^1.3
composer require csa/guzzle-bundle:^2.0
```

### Bleeding-edge version
Expand Down Expand Up @@ -55,4 +55,59 @@ csa_guzzle:
logger: true
```

Autowiring
----------

If you rely on Symfony autowiring, you can choose to alias a specific service to the `GuzzleHttp\ClientInterface`
interface and `GuzzlHttp\Client` class.

```yml
csa_guzzle:
profiler: '%kernel.debug%'
logger: true
clients:
github_api:
config:
base_uri: 'https://api.github.com'
headers:
Accept: application/vnd.github.v3+json
jsonplaceholder:
config:
base_uri: 'https://jsonplaceholder.typicode.com'
headers:
Accept: application/json
default_client: github_api
```

Then, your github_api client will be automatically injected into your controller or service:

```php
<?php

namespace App\Controller;

use Twig\Environment;
use Symfony\Component\HttpFoundation\Response;
use GuzzleHttp\Client;

class DefaultController
{
private $twig;
private $client;

public function __construct(Environment $twig, Client $client)
{
$this->twig = $twig;
$this->client = $client;
}

public function index()
{
$this->client->get('/users');

return new Response($this->twig->render("base.html.twig"), 200, ['Content-Type' => 'text/html']);
}
}
```

Next section: [Creating clients](clients.md)
4 changes: 4 additions & 0 deletions tests/DependencyInjection/CsaGuzzleExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Csa\Bundle\GuzzleBundle\DependencyInjection\CompilerPass\MiddlewarePass;
use Csa\Bundle\GuzzleBundle\DependencyInjection\CsaGuzzleExtension;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\MessageFormatter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -49,6 +50,9 @@ public function testClientCreated()
'Config must be passed to client constructor.'
);

$defaultClient = $container->getAlias(ClientInterface::class);
$this->assertEquals('csa_guzzle.client.foo', $defaultClient);

$this->assertFalse($client->isLazy());
}

Expand Down

0 comments on commit 097cf06

Please sign in to comment.