Skip to content
This repository was archived by the owner on Mar 9, 2021. It is now read-only.

Commit 9a5b6e3

Browse files
committed
Update tests and Phantomjs object for send and get request
1 parent 6247a51 commit 9a5b6e3

File tree

7 files changed

+178
-78
lines changed

7 files changed

+178
-78
lines changed

README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ The following illustrates how to make a basic GET request and output the page co
4848
// you can use Facade or app make function to use phantomjs
4949
// ex: app('phantomjs') or \PhantomJs
5050

51-
$response = \PhantomJs::get('http://google.com');
51+
$request = \PhantomJs::get('http://phantomjs.org/');
52+
53+
$response = \PhantomJs::send($request);
5254

5355
if($response->getStatus() === 200) {
5456

@@ -60,38 +62,39 @@ if($response->getStatus() === 200) {
6062
Saving a screen capture to local disk:
6163
```php
6264

63-
$width = 800;
64-
$height = 600;
65-
$top = 0;
66-
$left = 0;
67-
68-
$request = \PhantomJs::getMessageFactory()->createCaptureRequest('http://google.com', 'GET');
65+
$request = \PhantomJs::createImage('http://phantomjs.org/', 'GET');
6966

7067
$request->setOutputFile(public_path('file.jpg'));
7168

72-
$request->setViewportSize($width, $height);
69+
$request->setViewportSize(800, 600);
7370

74-
$request->setCaptureDimensions($width, $height, $top, $left);
71+
$request->setCaptureDimensions(800, 600, 0, 0);
7572

76-
$response = \PhantomJs::getMessageFactory()->createResponse();
73+
$response = \PhantomJs::send($request);
74+
75+
if($response->getStatus() === 200) {
7776

78-
\PhantomJs::send($request, $response);
77+
// Dump the requested page content
78+
echo $response->getContent();
79+
}
7980
```
8081

8182
Outputting a page as PDF:
8283

8384
```php
84-
use Josh\Component\PhantomJs\PhantomJs;
85-
86-
$request = PhantomJs::getMessageFactory()->createPdfRequest('http://google.com', 'GET');
85+
$request = \PhantomJs::createPdf('http://phantomjs.org/', 'GET');
8786
$request->setOutputFile(public_path('document.pdf'));
8887
$request->setFormat('A4');
8988
$request->setOrientation('landscape');
9089
$request->setMargin('1cm');
9190

92-
$response = PhantomJs::getMessageFactory()->createResponse();
91+
$response = \PhantomJs::send($request);
92+
93+
if($response->getStatus() === 200) {
9394

94-
PhantomJs::send($request, $response);
95+
// Dump the requested page content
96+
echo $response->getContent();
97+
}
9598
```
9699

97100
## License

src/Facade/PhantomJs.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use JonnyW\PhantomJs\Engine;
66
use Illuminate\Support\Facades\Facade;
7+
use JonnyW\PhantomJs\Http\CaptureRequest;
8+
use JonnyW\PhantomJs\Http\PdfRequest;
9+
use JonnyW\PhantomJs\Http\Request;
710
use JonnyW\PhantomJs\Http\RequestInterface;
811
use JonnyW\PhantomJs\Http\ResponseInterface;
912
use JonnyW\PhantomJs\Http\MessageFactoryInterface;
@@ -20,6 +23,13 @@
2023
* @method static ProcedureLoaderInterface getProcedureLoader
2124
* @method static ProcedureCompilerInterface getProcedureCompiler
2225
* @method static ResponseInterface send(RequestInterface $request, ResponseInterface $response)
26+
* @method static PdfRequest createPdf(string $url, string $method = 'GET', int $timeout = 5000, array $headers = [], array $parameters = [])
27+
* @method static CaptureRequest createImage(string $url, string $method = 'GET', int $timeout = 5000, array $headers = [], array $parameters = [])
28+
* @method static CaptureRequest request(string $url, string $method = 'GET', int $timeout = 5000, array $headers = [], array $parameters = [])
29+
* @method static Request get(string $url, array $headers = [], array $parameters = [])
30+
* @method static Request post(string $url, array $headers = [], array $parameters = [])
31+
* @method static Request put(string $url, array $headers = [], array $parameters = [])
32+
* @method static Request delete(string $url, array $headers = [], array $parameters = [])
2333
*/
2434
class PhantomJs extends Facade {
2535

src/PhantomJs.php

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
use Illuminate\Support\Arr;
66
use JonnyW\PhantomJs\Client;
77
use JonnyW\PhantomJs\Engine;
8+
use JonnyW\PhantomJs\Http\Request;
9+
use JonnyW\PhantomJs\Http\Response;
10+
use JonnyW\PhantomJs\Http\PdfRequest;
11+
use JonnyW\PhantomJs\Http\CaptureRequest;
812
use JonnyW\PhantomJs\Http\RequestInterface;
13+
use JonnyW\PhantomJs\Http\ResponseInterface;
914

1015
class PhantomJs
1116
{
@@ -31,7 +36,7 @@ class PhantomJs
3136
*/
3237
public function __construct($options = [])
3338
{
34-
$this->engine = new Engine();
39+
$this->engine = new Engine;
3540
$this->setOptions($options);
3641
$this->container = $this->getContainer();
3742
}
@@ -42,7 +47,7 @@ public function __construct($options = [])
4247
* @param $path
4348
* @return $this
4449
*/
45-
public function setBinaryPath($path)
50+
public function setBinaryPath($path) : PhantomJs
4651
{
4752
$this->engine->setPath($path);
4853

@@ -55,7 +60,7 @@ public function setBinaryPath($path)
5560
* @param $debug
5661
* @return $this
5762
*/
58-
public function setDebug($debug)
63+
public function setDebug($debug) : PhantomJs
5964
{
6065
$this->engine->debug($debug);
6166

@@ -68,7 +73,7 @@ public function setDebug($debug)
6873
* @param $cache
6974
* @return $this
7075
*/
71-
public function setCache($cache)
76+
public function setCache($cache) : PhantomJs
7277
{
7378
$this->engine->cache($cache);
7479

@@ -78,9 +83,9 @@ public function setCache($cache)
7883
/**
7984
* Get engine object of phantomjs client
8085
*
81-
* @return \Illuminate\Foundation\Application|mixed
86+
* @return Engine
8287
*/
83-
public function getEngine()
88+
public function getEngine() : Engine
8489
{
8590
return $this->engine;
8691
}
@@ -90,7 +95,7 @@ public function getEngine()
9095
*
9196
* @return PhantomJsServiceContainer
9297
*/
93-
public function getContainer()
98+
public function getContainer() : PhantomJsServiceContainer
9499
{
95100
$serviceContainer = PhantomJsServiceContainer::getInstance();
96101

@@ -104,7 +109,7 @@ public function getContainer()
104109
*
105110
* @return \Exception|Client
106111
*/
107-
public function getClient()
112+
public function getClient() : Client
108113
{
109114
try {
110115

@@ -147,37 +152,66 @@ public function setOptions(array $options)
147152
}
148153

149154
/**
150-
* Create request
155+
* send request
151156
*
152157
* @param $url
153-
* @param string $method
158+
* @param $method
159+
* @param $timeout
154160
* @param array $headers
155-
* @param array $data
161+
* @param array $parameters
162+
* @return Request
163+
*/
164+
public function request(string $url, string $method = RequestInterface::METHOD_GET, int $timeout = 5000, array $headers = [], array $parameters = []) : Request
165+
{
166+
$request = new Request($url, $method, $timeout);
167+
168+
$request->setHeaders($headers);
169+
170+
$request->setRequestData($parameters);
171+
172+
return $request;
173+
}
174+
175+
/**
176+
* Create pdf request
177+
*
178+
* @param $url
179+
* @param $method
156180
* @param int $timeout
157-
* @return RequestInterface
181+
* @param array $headers
182+
* @param array $parameters
183+
* @return PdfRequest
158184
*/
159-
public function createRequest($url, $method = RequestInterface::METHOD_GET,$headers = [],$data = [], $timeout = 5000)
185+
public function createPdf(string $url, string $method = RequestInterface::METHOD_GET, int $timeout = 5000, array $headers = [], array $parameters = []) : PdfRequest
160186
{
161-
$request = $this->getClient()->getMessageFactory()->createRequest($url, $method, $timeout);
187+
$request = new PdfRequest($url, $method, $timeout);
162188

163189
$request->setHeaders($headers);
164190

165-
$request->setRequestData($data);
191+
$request->setRequestData($parameters);
166192

167193
return $request;
168194
}
169195

170196
/**
171-
* Get response of request
197+
* Create pdf request
172198
*
173-
* @param $request
174-
* @return \JonnyW\PhantomJs\Http\ResponseInterface
199+
* @param $url
200+
* @param $method
201+
* @param int $timeout
202+
* @param array $headers
203+
* @param array $parameters
204+
* @return CaptureRequest
175205
*/
176-
public function createResponse($request)
206+
public function createImage(string $url, string $method = RequestInterface::METHOD_GET, int $timeout = 5000, array $headers = [], array $parameters = []) : CaptureRequest
177207
{
178-
$response = $this->getClient()->getMessageFactory()->createResponse();
208+
$request = new CaptureRequest($url, $method, $timeout);
209+
210+
$request->setHeaders($headers);
211+
212+
$request->setRequestData($parameters);
179213

180-
return $this->getClient()->send($request, $response);
214+
return $request;
181215
}
182216

183217
/**
@@ -186,13 +220,11 @@ public function createResponse($request)
186220
* @param $url
187221
* @param array $headers
188222
* @param array $parameters
189-
* @return \JonnyW\PhantomJs\Http\ResponseInterface
223+
* @return Request
190224
*/
191-
public function get($url,$headers = [],$parameters = [])
225+
public function get(string $url, array $headers = [], array $parameters = []) : Request
192226
{
193-
$request = $this->createRequest($url, 'GET', $headers, $parameters);
194-
195-
return $this->createResponse($request);
227+
return $this->request($url, RequestInterface::METHOD_GET, 5000, $headers, $parameters);
196228
}
197229

198230
/**
@@ -201,13 +233,11 @@ public function get($url,$headers = [],$parameters = [])
201233
* @param $url
202234
* @param array $headers
203235
* @param array $parameters
204-
* @return \JonnyW\PhantomJs\Http\ResponseInterface
236+
* @return Request
205237
*/
206-
public function post($url, $headers = [], $parameters = [])
238+
public function post(string $url, array $headers = [], array $parameters = []) : Request
207239
{
208-
$request = $this->createRequest($url, 'POST', $headers, $parameters);
209-
210-
return $this->createResponse($request);
240+
return $this->request($url, RequestInterface::METHOD_POST, 5000, $headers, $parameters);
211241
}
212242

213243
/**
@@ -216,13 +246,11 @@ public function post($url, $headers = [], $parameters = [])
216246
* @param $url
217247
* @param array $headers
218248
* @param array $parameters
219-
* @return \JonnyW\PhantomJs\Http\ResponseInterface
249+
* @return Request
220250
*/
221-
public function put($url, $headers = [], $parameters = [])
251+
public function put(string $url, array $headers = [], array $parameters = []) : Request
222252
{
223-
$request = $this->createRequest($url, 'PUT', $headers, $parameters);
224-
225-
return $this->createResponse($request);
253+
return $this->request($url, RequestInterface::METHOD_PUT, 5000, $headers, $parameters);
226254
}
227255

228256
/**
@@ -231,12 +259,22 @@ public function put($url, $headers = [], $parameters = [])
231259
* @param $url
232260
* @param array $headers
233261
* @param array $parameters
234-
* @return \JonnyW\PhantomJs\Http\ResponseInterface
262+
* @return Request
235263
*/
236-
public function delete($url, $headers = [], $parameters = [])
264+
public function delete(string $url, array $headers = [], array $parameters = []) : Request
237265
{
238-
$request = $this->createRequest($url, 'PUT', $headers, $parameters);
266+
return $this->request($url, RequestInterface::METHOD_DELETE, 5000, $headers, $parameters);
267+
}
239268

240-
return $this->createResponse($request);
269+
/**
270+
* Send request with response
271+
*
272+
* @param RequestInterface $request
273+
* @param ResponseInterface $response
274+
* @return ResponseInterface
275+
*/
276+
public function send(RequestInterface $request, ResponseInterface $response = null) : ResponseInterface
277+
{
278+
return $this->getClient()->send($request, ( is_null($response) ? new Response : $response ) );
241279
}
242280
}

src/PhantomJsServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PhantomJsServiceProvider extends ServiceProvider
1414
* @since 7 May 2017
1515
* @return void
1616
*/
17-
public function boot()
17+
public function boot() : void
1818
{
1919
$this->publishes([ __DIR__ . '/../config.php' => config_path( 'phantomjs.php' ) ]);
2020
}
@@ -26,7 +26,7 @@ public function boot()
2626
* @since 7 May 2017
2727
* @return void
2828
*/
29-
public function register()
29+
public function register() : void
3030
{
3131
$this->app->singleton('phantomjs', function(){
3232

@@ -41,9 +41,9 @@ public function register()
4141
* @since 8 May 2017
4242
* @return PhantomJs
4343
*/
44-
protected function getClient()
44+
protected function getClient() : PhantomJs
4545
{
46-
$client = app(PhantomJs::class);
46+
$client = new PhantomJs;
4747

4848
if(file_exists(config_path('phantomjs.php'))){
4949
$config = config('phantomjs');

tests/LaravelPhantomJsTest.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)