Skip to content

Commit 0689a04

Browse files
committed
Added support for PSR7 requests
1 parent b84ea02 commit 0689a04

File tree

8 files changed

+80
-107
lines changed

8 files changed

+80
-107
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
## [Unreleased]
1212

13+
### Added
14+
- Added support for PSR7 HTTP clients and requests for URL calls.
15+
1316
### Changed
1417
- Fixed issue with \ causing an infite loop.
1518

19+
### Removed
20+
- Removed curl interface and curl implementation.
21+
1622
## 2.2.0
1723

1824
### Added

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
"ext-mbstring": "*",
1818
"paquettg/string-encode": "~1.0.0",
1919
"ext-zlib": "*",
20-
"ext-curl": "*"
20+
"ext-curl": "*",
21+
"php-http/httplug": "^2.1",
22+
"php-http/guzzle6-adapter": "^2.0",
23+
"guzzlehttp/psr7": "^1.6"
2124
},
2225
"require-dev": {
2326
"phpunit/phpunit": "^7.5.1",

src/PHPHtmlParser/Curl.php

-54
This file was deleted.

src/PHPHtmlParser/CurlInterface.php

-20
This file was deleted.

src/PHPHtmlParser/Dom.php

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php declare(strict_types=1);
22
namespace PHPHtmlParser;
33

4+
use GuzzleHttp\Psr7\Request;
5+
use Http\Adapter\Guzzle6\Client;
6+
use Http\Discovery\HttpClientDiscovery;
7+
use Http\Discovery\MessageFactoryDiscovery;
8+
use Http\Discovery\Psr17FactoryDiscovery;
49
use PHPHtmlParser\Dom\AbstractNode;
510
use PHPHtmlParser\Dom\Collection;
611
use PHPHtmlParser\Dom\HtmlNode;
@@ -13,6 +18,8 @@
1318
use PHPHtmlParser\Exceptions\StrictException;
1419
use PHPHtmlParser\Exceptions\UnknownChildTypeException;
1520
use PHPHtmlParser\Exceptions\LogicalException;
21+
use Psr\Http\Client\ClientInterface;
22+
use Psr\Http\Message\RequestInterface;
1623
use stringEncode\Encode;
1724

1825
/**
@@ -182,22 +189,27 @@ public function loadFromFile(string $file, array $options = []): Dom
182189
/**
183190
* Use a curl interface implementation to attempt to load
184191
* the content from a url.
185-
* @param string $url
186-
* @param array $options
187-
* @param CurlInterface|null $curl
192+
* @param string $url
193+
* @param array $options
194+
* @param ClientInterface $client
195+
* @param RequestInterface|null $request
188196
* @return Dom
189197
* @throws ChildNotFoundException
190198
* @throws CircularException
191-
* @throws CurlException
192199
* @throws StrictException
200+
* @throws \Psr\Http\Client\ClientExceptionInterface
193201
*/
194-
public function loadFromUrl(string $url, array $options = [], CurlInterface $curl = null): Dom
202+
public function loadFromUrl(string $url, array $options = [], ClientInterface $client = null, RequestInterface $request = null): Dom
195203
{
196-
if (is_null($curl)) {
197-
// use the default curl interface
198-
$curl = new Curl;
204+
if (is_null($client)) {
205+
$client = new Client();
199206
}
200-
$content = $curl->get($url, $options);
207+
if (is_null($request)) {
208+
$request = new Request('GET', $url);
209+
}
210+
211+
$response = $client->sendRequest($request);
212+
$content = $response->getBody()->getContents();
201213

202214
return $this->loadStr($content, $options);
203215
}

src/PHPHtmlParser/StaticDom.php

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<?php declare(strict_types=1);
22
namespace PHPHtmlParser;
33

4+
use GuzzleHttp\Psr7\Request;
5+
use Http\Adapter\Guzzle6\Client;
6+
use Http\Discovery\HttpClientDiscovery;
7+
use Http\Discovery\Psr17FactoryDiscovery;
48
use PHPHtmlParser\Exceptions\ChildNotFoundException;
59
use PHPHtmlParser\Exceptions\CircularException;
610
use PHPHtmlParser\Exceptions\CurlException;
711
use PHPHtmlParser\Exceptions\NotLoadedException;
812
use PHPHtmlParser\Exceptions\StrictException;
13+
use Psr\Http\Client\ClientInterface;
14+
use Psr\Http\Message\RequestInterface;
915

1016
/**
1117
* Class StaticDom
@@ -82,6 +88,7 @@ public static function load(string $str): Dom
8288
* @throws ChildNotFoundException
8389
* @throws CircularException
8490
* @throws StrictException
91+
* @throws Exceptions\LogicalException
8592
*/
8693
public static function loadFromFile(string $file): Dom
8794
{
@@ -94,25 +101,29 @@ public static function loadFromFile(string $file): Dom
94101
/**
95102
* Creates a new dom object and calls loadFromUrl() on the
96103
* new object.
97-
* @param string $url
98-
* @param array $options
99-
* @param CurlInterface|null $curl
104+
* @param string $url
105+
* @param array $options
106+
* @param ClientInterface|null $client
107+
* @param RequestInterface|null $request
100108
* @return Dom
101109
* @throws ChildNotFoundException
102110
* @throws CircularException
103-
* @throws CurlException
104111
* @throws StrictException
112+
* @throws \Psr\Http\Client\ClientExceptionInterface
105113
*/
106-
public static function loadFromUrl(string $url, array $options = [], CurlInterface $curl = null): Dom
114+
public static function loadFromUrl(string $url, array $options = [], ClientInterface $client = null, RequestInterface $request = null): Dom
107115
{
108116
$dom = new Dom;
109117
self::$dom = $dom;
110-
if (is_null($curl)) {
111-
// use the default curl interface
112-
$curl = new Curl;
118+
119+
if (is_null($client)) {
120+
$client = new Client();
121+
}
122+
if (is_null($request)) {
123+
$request = new Request('GET', $url);
113124
}
114125

115-
return $dom->loadFromUrl($url, $options, $curl);
126+
return $dom->loadFromUrl($url, $options, $client, $request);
116127
}
117128

118129
/**

tests/DomTest.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,22 @@ public function testLoadFileBigTwicePreserveOption()
228228

229229
public function testLoadFromUrl()
230230
{
231-
$curl = Mockery::mock('PHPHtmlParser\CurlInterface');
232-
$curl->shouldReceive('get')
233-
->once()
234-
->with('http://google.com', [])
235-
->andReturn(file_get_contents('tests/data/files/small.html'));
236-
237-
$dom = new Dom;
238-
$dom->loadFromUrl('http://google.com', [], $curl);
231+
$streamMock = Mockery::mock(\Psr\Http\Message\StreamInterface::class);
232+
$streamMock->shouldReceive('getContents')
233+
->once()
234+
->andReturn(file_get_contents('tests/data/files/small.html'));
235+
$responseMock = Mockery::mock(\Psr\Http\Message\ResponseInterface::class);
236+
$responseMock->shouldReceive('getBody')
237+
->once()
238+
->andReturn($streamMock);
239+
$clientMock = Mockery::mock(\Psr\Http\Client\ClientInterface::class);
240+
$clientMock->shouldReceive('sendRequest')
241+
->once()
242+
->andReturn($responseMock);
243+
244+
245+
$dom = new Dom;
246+
$dom->loadFromUrl('http://google.com', [], $clientMock);
239247
$this->assertEquals('VonBurgermeister', $dom->find('.post-row div .post-user font', 0)->text);
240248
}
241249

tests/StaticDomTest.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,20 @@ public function testFindI()
5858

5959
public function testLoadFromUrl()
6060
{
61-
$curl = Mockery::mock('PHPHtmlParser\CurlInterface');
62-
$curl->shouldReceive('get')
63-
->once()
64-
->with('http://google.com', [])
65-
->andReturn(file_get_contents('tests/data/files/small.html'));
61+
$streamMock = Mockery::mock(\Psr\Http\Message\StreamInterface::class);
62+
$streamMock->shouldReceive('getContents')
63+
->once()
64+
->andReturn(file_get_contents('tests/data/files/small.html'));
65+
$responseMock = Mockery::mock(\Psr\Http\Message\ResponseInterface::class);
66+
$responseMock->shouldReceive('getBody')
67+
->once()
68+
->andReturn($streamMock);
69+
$clientMock = Mockery::mock(\Psr\Http\Client\ClientInterface::class);
70+
$clientMock->shouldReceive('sendRequest')
71+
->once()
72+
->andReturn($responseMock);
6673

67-
Dom::loadFromUrl('http://google.com', [], $curl);
74+
Dom::loadFromUrl('http://google.com', [], $clientMock);
6875
$this->assertEquals('VonBurgermeister', Dom::find('.post-row div .post-user font', 0)->text);
6976
}
7077

0 commit comments

Comments
 (0)