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

Commit

Permalink
Update tests and Phantomjs object for send and get request
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjosh committed Feb 12, 2018
1 parent 6247a51 commit 9a5b6e3
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 78 deletions.
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ The following illustrates how to make a basic GET request and output the page co
// you can use Facade or app make function to use phantomjs
// ex: app('phantomjs') or \PhantomJs
$response = \PhantomJs::get('http://google.com');
$request = \PhantomJs::get('http://phantomjs.org/');
$response = \PhantomJs::send($request);
if($response->getStatus() === 200) {
Expand All @@ -60,38 +62,39 @@ if($response->getStatus() === 200) {
Saving a screen capture to local disk:
```php
$width = 800;
$height = 600;
$top = 0;
$left = 0;
$request = \PhantomJs::getMessageFactory()->createCaptureRequest('http://google.com', 'GET');
$request = \PhantomJs::createImage('http://phantomjs.org/', 'GET');
$request->setOutputFile(public_path('file.jpg'));
$request->setViewportSize($width, $height);
$request->setViewportSize(800, 600);
$request->setCaptureDimensions($width, $height, $top, $left);
$request->setCaptureDimensions(800, 600, 0, 0);
$response = \PhantomJs::getMessageFactory()->createResponse();
$response = \PhantomJs::send($request);
if($response->getStatus() === 200) {
\PhantomJs::send($request, $response);
// Dump the requested page content
echo $response->getContent();
}
```

Outputting a page as PDF:

```php
use Josh\Component\PhantomJs\PhantomJs;
$request = PhantomJs::getMessageFactory()->createPdfRequest('http://google.com', 'GET');
$request = \PhantomJs::createPdf('http://phantomjs.org/', 'GET');
$request->setOutputFile(public_path('document.pdf'));
$request->setFormat('A4');
$request->setOrientation('landscape');
$request->setMargin('1cm');
$response = PhantomJs::getMessageFactory()->createResponse();
$response = \PhantomJs::send($request);
if($response->getStatus() === 200) {
PhantomJs::send($request, $response);
// Dump the requested page content
echo $response->getContent();
}
```

## License
Expand Down
10 changes: 10 additions & 0 deletions src/Facade/PhantomJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use JonnyW\PhantomJs\Engine;
use Illuminate\Support\Facades\Facade;
use JonnyW\PhantomJs\Http\CaptureRequest;
use JonnyW\PhantomJs\Http\PdfRequest;
use JonnyW\PhantomJs\Http\Request;
use JonnyW\PhantomJs\Http\RequestInterface;
use JonnyW\PhantomJs\Http\ResponseInterface;
use JonnyW\PhantomJs\Http\MessageFactoryInterface;
Expand All @@ -20,6 +23,13 @@
* @method static ProcedureLoaderInterface getProcedureLoader
* @method static ProcedureCompilerInterface getProcedureCompiler
* @method static ResponseInterface send(RequestInterface $request, ResponseInterface $response)
* @method static PdfRequest createPdf(string $url, string $method = 'GET', int $timeout = 5000, array $headers = [], array $parameters = [])
* @method static CaptureRequest createImage(string $url, string $method = 'GET', int $timeout = 5000, array $headers = [], array $parameters = [])
* @method static CaptureRequest request(string $url, string $method = 'GET', int $timeout = 5000, array $headers = [], array $parameters = [])
* @method static Request get(string $url, array $headers = [], array $parameters = [])
* @method static Request post(string $url, array $headers = [], array $parameters = [])
* @method static Request put(string $url, array $headers = [], array $parameters = [])
* @method static Request delete(string $url, array $headers = [], array $parameters = [])
*/
class PhantomJs extends Facade {

Expand Down
118 changes: 78 additions & 40 deletions src/PhantomJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
use Illuminate\Support\Arr;
use JonnyW\PhantomJs\Client;
use JonnyW\PhantomJs\Engine;
use JonnyW\PhantomJs\Http\Request;
use JonnyW\PhantomJs\Http\Response;
use JonnyW\PhantomJs\Http\PdfRequest;
use JonnyW\PhantomJs\Http\CaptureRequest;
use JonnyW\PhantomJs\Http\RequestInterface;
use JonnyW\PhantomJs\Http\ResponseInterface;

class PhantomJs
{
Expand All @@ -31,7 +36,7 @@ class PhantomJs
*/
public function __construct($options = [])
{
$this->engine = new Engine();
$this->engine = new Engine;
$this->setOptions($options);
$this->container = $this->getContainer();
}
Expand All @@ -42,7 +47,7 @@ public function __construct($options = [])
* @param $path
* @return $this
*/
public function setBinaryPath($path)
public function setBinaryPath($path) : PhantomJs
{
$this->engine->setPath($path);

Expand All @@ -55,7 +60,7 @@ public function setBinaryPath($path)
* @param $debug
* @return $this
*/
public function setDebug($debug)
public function setDebug($debug) : PhantomJs
{
$this->engine->debug($debug);

Expand All @@ -68,7 +73,7 @@ public function setDebug($debug)
* @param $cache
* @return $this
*/
public function setCache($cache)
public function setCache($cache) : PhantomJs
{
$this->engine->cache($cache);

Expand All @@ -78,9 +83,9 @@ public function setCache($cache)
/**
* Get engine object of phantomjs client
*
* @return \Illuminate\Foundation\Application|mixed
* @return Engine
*/
public function getEngine()
public function getEngine() : Engine
{
return $this->engine;
}
Expand All @@ -90,7 +95,7 @@ public function getEngine()
*
* @return PhantomJsServiceContainer
*/
public function getContainer()
public function getContainer() : PhantomJsServiceContainer
{
$serviceContainer = PhantomJsServiceContainer::getInstance();

Expand All @@ -104,7 +109,7 @@ public function getContainer()
*
* @return \Exception|Client
*/
public function getClient()
public function getClient() : Client
{
try {

Expand Down Expand Up @@ -147,37 +152,66 @@ public function setOptions(array $options)
}

/**
* Create request
* send request
*
* @param $url
* @param string $method
* @param $method
* @param $timeout
* @param array $headers
* @param array $data
* @param array $parameters
* @return Request
*/
public function request(string $url, string $method = RequestInterface::METHOD_GET, int $timeout = 5000, array $headers = [], array $parameters = []) : Request
{
$request = new Request($url, $method, $timeout);

$request->setHeaders($headers);

$request->setRequestData($parameters);

return $request;
}

/**
* Create pdf request
*
* @param $url
* @param $method
* @param int $timeout
* @return RequestInterface
* @param array $headers
* @param array $parameters
* @return PdfRequest
*/
public function createRequest($url, $method = RequestInterface::METHOD_GET,$headers = [],$data = [], $timeout = 5000)
public function createPdf(string $url, string $method = RequestInterface::METHOD_GET, int $timeout = 5000, array $headers = [], array $parameters = []) : PdfRequest
{
$request = $this->getClient()->getMessageFactory()->createRequest($url, $method, $timeout);
$request = new PdfRequest($url, $method, $timeout);

$request->setHeaders($headers);

$request->setRequestData($data);
$request->setRequestData($parameters);

return $request;
}

/**
* Get response of request
* Create pdf request
*
* @param $request
* @return \JonnyW\PhantomJs\Http\ResponseInterface
* @param $url
* @param $method
* @param int $timeout
* @param array $headers
* @param array $parameters
* @return CaptureRequest
*/
public function createResponse($request)
public function createImage(string $url, string $method = RequestInterface::METHOD_GET, int $timeout = 5000, array $headers = [], array $parameters = []) : CaptureRequest
{
$response = $this->getClient()->getMessageFactory()->createResponse();
$request = new CaptureRequest($url, $method, $timeout);

$request->setHeaders($headers);

$request->setRequestData($parameters);

return $this->getClient()->send($request, $response);
return $request;
}

/**
Expand All @@ -186,13 +220,11 @@ public function createResponse($request)
* @param $url
* @param array $headers
* @param array $parameters
* @return \JonnyW\PhantomJs\Http\ResponseInterface
* @return Request
*/
public function get($url,$headers = [],$parameters = [])
public function get(string $url, array $headers = [], array $parameters = []) : Request
{
$request = $this->createRequest($url, 'GET', $headers, $parameters);

return $this->createResponse($request);
return $this->request($url, RequestInterface::METHOD_GET, 5000, $headers, $parameters);
}

/**
Expand All @@ -201,13 +233,11 @@ public function get($url,$headers = [],$parameters = [])
* @param $url
* @param array $headers
* @param array $parameters
* @return \JonnyW\PhantomJs\Http\ResponseInterface
* @return Request
*/
public function post($url, $headers = [], $parameters = [])
public function post(string $url, array $headers = [], array $parameters = []) : Request
{
$request = $this->createRequest($url, 'POST', $headers, $parameters);

return $this->createResponse($request);
return $this->request($url, RequestInterface::METHOD_POST, 5000, $headers, $parameters);
}

/**
Expand All @@ -216,13 +246,11 @@ public function post($url, $headers = [], $parameters = [])
* @param $url
* @param array $headers
* @param array $parameters
* @return \JonnyW\PhantomJs\Http\ResponseInterface
* @return Request
*/
public function put($url, $headers = [], $parameters = [])
public function put(string $url, array $headers = [], array $parameters = []) : Request
{
$request = $this->createRequest($url, 'PUT', $headers, $parameters);

return $this->createResponse($request);
return $this->request($url, RequestInterface::METHOD_PUT, 5000, $headers, $parameters);
}

/**
Expand All @@ -231,12 +259,22 @@ public function put($url, $headers = [], $parameters = [])
* @param $url
* @param array $headers
* @param array $parameters
* @return \JonnyW\PhantomJs\Http\ResponseInterface
* @return Request
*/
public function delete($url, $headers = [], $parameters = [])
public function delete(string $url, array $headers = [], array $parameters = []) : Request
{
$request = $this->createRequest($url, 'PUT', $headers, $parameters);
return $this->request($url, RequestInterface::METHOD_DELETE, 5000, $headers, $parameters);
}

return $this->createResponse($request);
/**
* Send request with response
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function send(RequestInterface $request, ResponseInterface $response = null) : ResponseInterface
{
return $this->getClient()->send($request, ( is_null($response) ? new Response : $response ) );
}
}
8 changes: 4 additions & 4 deletions src/PhantomJsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PhantomJsServiceProvider extends ServiceProvider
* @since 7 May 2017
* @return void
*/
public function boot()
public function boot() : void
{
$this->publishes([ __DIR__ . '/../config.php' => config_path( 'phantomjs.php' ) ]);
}
Expand All @@ -26,7 +26,7 @@ public function boot()
* @since 7 May 2017
* @return void
*/
public function register()
public function register() : void
{
$this->app->singleton('phantomjs', function(){

Expand All @@ -41,9 +41,9 @@ public function register()
* @since 8 May 2017
* @return PhantomJs
*/
protected function getClient()
protected function getClient() : PhantomJs
{
$client = app(PhantomJs::class);
$client = new PhantomJs;

if(file_exists(config_path('phantomjs.php'))){
$config = config('phantomjs');
Expand Down
17 changes: 0 additions & 17 deletions tests/LaravelPhantomJsTest.php

This file was deleted.

Loading

0 comments on commit 9a5b6e3

Please sign in to comment.