Skip to content

Commit f77c48d

Browse files
committed
Use HTTPlug
1 parent a27b677 commit f77c48d

File tree

7 files changed

+159
-159
lines changed

7 files changed

+159
-159
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ composer require scriptotek/php-sru-client
2424

2525
in your project directory to get the latest stable version of the package.
2626

27+
**You also need a HTTP library**. If you're not already using one in your project,
28+
just add Guzzle:
29+
30+
```bash
31+
composer require php-http/guzzle6-adapter
32+
```
33+
34+
[HTTPlug discovery](http://php-http.readthedocs.io/en/latest/discovery.html) is
35+
used in order to not depend on a specific library.
36+
2737
## Configuring the client
2838

2939
```php
@@ -113,17 +123,15 @@ You can view it at [scriptotek.github.io/php-sru-client](//scriptotek.github.io/
113123

114124
## Laravel 5 integration
115125

116-
In the $providers array add the service providers for this package:
126+
Add the service provider to the `'providers'` array in `config/app.php`:
117127

118128
Scriptotek\Sru\Providers\SruServiceProvider::class,
119129

120-
Add the facade of this package to the `$aliases` array:
130+
Optionally, add the facade to the `'aliases'` array in the same file:
121131

122-
'SruClient' => Scriptotek\Sru\Facades\SruClient::class,
132+
'SruClient' => Scriptotek\Sru\Facades\SruClient::class,
123133

124-
Publish configuration in Laravel 5:
134+
To create the configuration file `config/sru.php`:
125135

126136
$ php artisan vendor:publish --provider="Scriptotek\Sru\Providers\SruServiceProvider"
127137

128-
The configuration file is copied to `config/sru.php`.
129-

composer.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@
55
"homepage": "http://github.com/scriptotek/sru-client",
66
"keywords": ["sru"],
77
"require": {
8-
"php" : "^5.5 || ^7.0",
9-
"guzzlehttp/guzzle": "~6.0",
10-
"danmichaelo/quitesimplexmlelement": ">=0.4.2"
8+
"php" : "^5.6 || ^7.0",
9+
"danmichaelo/quitesimplexmlelement": ">=0.4.2",
10+
"php-http/client-implementation": "^1.0",
11+
"php-http/httplug": "^1.1",
12+
"php-http/message-factory": "^1.0",
13+
"php-http/discovery": "^1.0",
14+
"php-http/client-common": "^1.5"
1115
},
1216
"require-dev": {
1317
"phpunit/phpunit": "^4.8|^5.5",
1418
"sami/sami": "^3.3",
15-
"mockery/mockery": "^0.9"
19+
"php-http/mock-client": "^1.0",
20+
"php-http/message": "^1.0",
21+
"guzzlehttp/psr7": "^1.0",
22+
"php-http/guzzle6-adapter": "^1.1"
1623
},
1724
"license": "MIT",
1825
"authors": [

src/Client.php

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<?php namespace Scriptotek\Sru;
22

3-
use GuzzleHttp\Client as HttpClient;
3+
use Http\Client\Common\Plugin\AuthenticationPlugin;
4+
use Http\Client\Common\PluginClient;
5+
use Http\Client\HttpClient;
6+
use Http\Discovery\HttpClientDiscovery;
7+
use Http\Client\Common\Plugin\ErrorPlugin;
8+
use Http\Discovery\MessageFactoryDiscovery;
9+
use Http\Message\Authentication\BasicAuth;
10+
use Http\Message\MessageFactory;
411

512
/**
613
* SRU client
@@ -10,6 +17,9 @@ class Client
1017
/** @var HttpClient */
1118
protected $httpClient;
1219

20+
/** @var MessageFactory */
21+
protected $messageFactory;
22+
1323
/** @var string SRU service base URL */
1424
protected $url;
1525

@@ -38,15 +48,22 @@ class Client
3848
/**
3949
* Create a new client
4050
*
41-
* @param string $url Base URL to the SRU service
42-
* @param array $options Associative array of options
43-
* @param HttpClient $httpClient
51+
* @param string $url Base URL to the SRU service
52+
* @param array $options Associative array of options
53+
* @param HttpClient $httpClient
54+
* @param MessageFactory|null $messageFactory
55+
* @throws \ErrorException
4456
*/
45-
public function __construct($url, $options = null, $httpClient = null)
46-
{
57+
public function __construct(
58+
$url,
59+
$options = null,
60+
HttpClient $httpClient = null,
61+
MessageFactory $messageFactory = null
62+
) {
4763
$this->url = $url;
4864
$options = $options ?: array();
49-
$this->httpClient = $httpClient ?: new HttpClient;
65+
66+
$plugins = [new ErrorPlugin()];
5067

5168
$this->schema = isset($options['schema'])
5269
? $options['schema']
@@ -60,13 +77,17 @@ public function __construct($url, $options = null, $httpClient = null)
6077
? $options['user-agent']
6178
: null;
6279

63-
$this->credentials = isset($options['credentials'])
64-
? $options['credentials']
65-
: null;
80+
if (isset($options['credentials'])) {
81+
$authentication = new BasicAuth($options['credentials'][0], $options['credentials'][1]);
82+
$plugins[] = new AuthenticationPlugin($authentication);
83+
}
6684

67-
$this->proxy = isset($options['proxy'])
68-
? $options['proxy']
69-
: null;
85+
if (isset($options['proxy'])) {
86+
throw new\ErrorException('Not supported');
87+
}
88+
89+
$this->httpClient = new PluginClient($httpClient ?: HttpClientDiscovery::find(), $plugins);
90+
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find();
7091
}
7192

7293
/**
@@ -106,24 +127,16 @@ public function urlTo($cql, $start = 1, $count = 10, $extraParams = array())
106127
*
107128
* @return array
108129
*/
109-
public function getHttpOptions()
130+
public function getHttpHeaders()
110131
{
111132
$headers = array(
112133
'Accept' => 'application/xml'
113134
);
114135
if ($this->userAgent) {
115136
$headers['User-Agent'] = $this->userAgent;
116137
}
117-
$options = array(
118-
'headers' => $headers
119-
);
120-
if ($this->credentials) {
121-
$options['auth'] = $this->credentials;
122-
}
123-
if ($this->proxy) {
124-
$options['proxy'] = $this->proxy;
125-
}
126-
return $options;
138+
139+
return $headers;
127140
}
128141

129142
/**
@@ -139,10 +152,7 @@ public function getHttpOptions()
139152
public function search($cql, $start = 1, $count = 10, $extraParams = array())
140153
{
141154
$url = $this->urlTo($cql, $start, $count, $extraParams);
142-
$options = $this->getHttpOptions();
143-
144-
$response = $this->httpClient->get($url, $options);
145-
$body = (string) $response->getBody();
155+
$body = $this->request('GET', $url);
146156

147157
return new SearchRetrieveResponse($body, $this);
148158
}
@@ -200,11 +210,22 @@ public function explain()
200210
'operation' => 'explain',
201211
'version' => $this->version,
202212
));
203-
$options = $this->getHttpOptions();
204213

205-
$response = $this->httpClient->get($url, $options);
206-
$body = (string) $response->getBody();
214+
$body = $this->request('GET', $url);
207215

208216
return new ExplainResponse($body, $this);
209217
}
218+
219+
/**
220+
* @param string $method
221+
* @param string $url
222+
* @return string
223+
*/
224+
public function request($method, $url)
225+
{
226+
$request = $this->messageFactory->createRequest($method, $url, $this->getHttpHeaders());
227+
$response = $this->httpClient->sendRequest($request);
228+
229+
return (string) $response->getBody();
230+
}
210231
}

src/Records.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php namespace Scriptotek\Sru;
22

3-
use GuzzleHttp\Client as HttpClient;
4-
53
/**
64
* When iterating, methods are called in the following order:
75
*
@@ -20,9 +18,6 @@
2018
*/
2119
class Records implements \Iterator
2220
{
23-
/** @var HttpClient */
24-
protected $httpClient;
25-
2621
private $position;
2722
private $count;
2823
private $extraParams;
@@ -39,15 +34,13 @@ class Records implements \Iterator
3934
* @param Client $client SRU client reference (optional)
4035
* @param int $count Number of records to request per request
4136
* @param array $extraParams Extra GET parameters
42-
* @param mixed $httpClient A http client
4337
*/
44-
public function __construct($cql, Client $client, $count = 10, $extraParams = array(), $httpClient = null)
38+
public function __construct($cql, Client $client, $count = 10, $extraParams = array())
4539
{
4640
$this->position = 1;
4741
$this->count = $count; // number of records per request
4842
$this->extraParams = $extraParams;
4943
$this->cql = $cql;
50-
$this->httpClient = $httpClient ?: new HttpClient;
5144
$this->client = $client;
5245
$this->fetchMore();
5346
}
@@ -68,10 +61,7 @@ public function numberOfRecords()
6861
private function fetchMore()
6962
{
7063
$url = $this->client->urlTo($this->cql, $this->position, $this->count, $this->extraParams);
71-
$options = $this->client->getHttpOptions();
72-
73-
$response = $this->httpClient->get($url, $options);
74-
$body = (string) $response->getBody();
64+
$body = $this->client->request('GET', $url);
7565
$this->lastResponse = new SearchRetrieveResponse($body);
7666
$this->data = $this->lastResponse->records;
7767

0 commit comments

Comments
 (0)