Jonny Wenmoth 8 éve
szülő
commit
9916e29e97

+ 1 - 3
src/Engine.php

@@ -18,8 +18,6 @@ use JonnyW\PhantomJs\Exception\InvalidExecutableException;
  */
 class Engine
 {
-    const VERSION = 4;
-
     /**
      * Executable path.
      *
@@ -60,7 +58,7 @@ class Engine
      */
     public function __construct()
     {
-        $this->path = 'bin/phantomjs';
+        $this->path = 'bin'.DIRECTORY_SEPARATOR.'phantomjs';
         $this->options = array();
 
         $this->debug = false;

+ 0 - 33
src/Http/StreamResponse.php

@@ -1,33 +0,0 @@
-<?php
-
-/*
- * This file is part of the php-phantomjs.
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace JonnyW\PhantomJs\Http;
-
-use GuzzleHttp\Psr7\Response as BaseResponse;
-
-/**
- * PHP PhantomJs.
- *
- * @author Jon Wenmoth <contact@jonnyw.me>
- */
-class StreamResponse extends BaseResponse implements OutputInterface
-{
-    use \GuzzleHttp\Psr7\MessageTrait;
-    use \JonnyW\PhantomJs\IO\Output\OutputTrait;
-
-    /**
-     * Get input type.
-     *
-     * @return string
-     */
-    public function getType()
-    {
-        return 'http_stream';
-    }
-}

+ 72 - 0
src/IO/BufferOutput.php

@@ -0,0 +1,72 @@
+<?php
+
+/*
+ * This file is part of the php-phantomjs.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace JonnyW\PhantomJs\IO;
+
+/**
+ * PHP PhantomJs.
+ *
+ * @author Jon Wenmoth <contact@jonnyw.me>
+ */
+class BufferOutput implements OutputInterface
+{
+    use \GuzzleHttp\Psr7\MessageTrait;
+    use \JonnyW\PhantomJs\IO\OutputTrait;
+
+    /**
+     * Format.
+     *
+     * @var string
+     */
+    private $format;
+
+    /**
+     * Internal constructor.
+     *
+     * @param string $format (default: 'PNG')
+     *
+     * @throws \InvalidArgumentException
+     */
+    public function __construct($format = 'PNG')
+    {
+        $formats = array(
+            'PNG',
+            'GIF',
+            'JPEG',
+            'PDF',
+        );
+
+        if (!in_array(strtoupper($format), $formats)) {
+            throw new \InvalidArgumentException('Output format must be PNG, GIF, JPEG or PDF');
+        }
+
+        $this->format = strtoupper($format);
+    }
+    
+    /**
+     * Get format.
+     * 
+     * @access public
+     * @return string
+     */
+    public function getFormat()
+    {
+        return $this->format;
+    }
+
+    /**
+     * Get output type.
+     *
+     * @return string
+     */
+    public function getType()
+    {
+        return 'buffer';
+    }
+}

+ 2 - 40
src/IO/InputInterface.php

@@ -42,29 +42,9 @@ interface InputInterface extends MessageInterface
     /**
      * Get custom setting.
      *
-     * @param string $name
-     *
-     * @return mixed
-     */
-    public function getCustom($name);
-    
-    /**
-     * Has cookie.
-     *
-     * @param string $cookie
-     *
-     * @return bool
-     */
-    public function hasCookie($cookie);
-
-    /**
-     * Get single added cookie.
-     *
-     * @param string $cookie
-     *
-     * @return \JonnyW\PhantomJs\Page\Cookie|null
+     * @return array
      */
-    public function getCookie($cookie);
+    public function getCustom();
 
     /**
      * Get all added cookies.
@@ -93,24 +73,6 @@ interface InputInterface extends MessageInterface
      */
     public function withoutCookie($cookie);
 
-    /**
-     * Has setting.
-     *
-     * @param string $setting
-     *
-     * @return bool
-     */
-    public function hasSetting($setting);
-
-    /**
-     * Get single setting.
-     *
-     * @param string $setting
-     *
-     * @return mixed
-     */
-    public function getSetting($setting);
-
     /**
      * Get all settings.
      *

+ 3 - 65
src/IO/InputTrait.php

@@ -83,45 +83,11 @@ trait InputTrait
     /**
      * Get custom setting.
      *
-     * @param string $name
-     *
-     * @return mixed
-     */
-    public function getCustom($name)
-    {
-        if (isset($this->custom[$name])) {
-            return $this->custom[$name];
-        }
-
-        return '';
-    }
-
-    /**
-     * Has cookie.
-     *
-     * @param string $cookie
-     *
-     * @return bool
-     */
-    public function hasCookie($cookie)
-    {
-        return isset($this->cookies['add'][$cookie]);
-    }
-
-    /**
-     * Get single added cookie.
-     *
-     * @param string $cookie
-     *
-     * @return \JonnyW\PhantomJs\Page\Cookie|null
+     * @return array
      */
-    public function getCookie($cookie)
+    public function getCustom()
     {
-        if (isset($this->cookies['add'][$cookie])) {
-            return $this->cookies['add'][$cookie];
-        }
-
-        return null;
+        return $this->custom;
     }
 
     /**
@@ -168,34 +134,6 @@ trait InputTrait
         return $new;
     }
 
-    /**
-     * Has setting.
-     *
-     * @param string $setting
-     *
-     * @return bool
-     */
-    public function hasSetting($setting)
-    {
-        return isset($this->settings[$setting]);
-    }
-
-    /**
-     * Get single setting.
-     *
-     * @param string $setting
-     *
-     * @return mixed
-     */
-    public function getSetting($setting)
-    {
-        if (isset($this->settings[$setting])) {
-            return $this->settings[$setting];
-        }
-
-        return null;
-    }
-
     /**
      * Get all settings.
      *

+ 16 - 16
src/IO/OutputTrait.php

@@ -28,11 +28,11 @@ trait OutputTrait
     private $logs = [];
 
     /**
-     * Settings.
+     * Page setup.
      *
      * @var array
      */
-    private $settings = [];
+    private $page = [];
 
     /**
      * Create new output instance
@@ -45,7 +45,7 @@ trait OutputTrait
     public function withViewportSize(ViewportSize $size)
     {
         $new = clone $this;
-        $new->settings['page.viewportSize'] = $size;
+        $new->page['viewportSize'] = $size;
 
         return $new;
     }
@@ -60,7 +60,7 @@ trait OutputTrait
     {
         $new = clone $this;
 
-        unset($new->settings['page.viewportSize']);
+        unset($new->page['viewportSize']);
 
         return $new;
     }
@@ -72,7 +72,7 @@ trait OutputTrait
      */
     public function getViewportSize()
     {
-        return $this->settings['page.viewportSize'];
+        return $this->page['viewportSize'];
     }
 
     /**
@@ -86,7 +86,7 @@ trait OutputTrait
     public function withPaperSize(PaperSize $size)
     {
         $new = clone $this;
-        $new->settings['page.paperSize'] = $size;
+        $new->page['paperSize'] = $size;
 
         return $new;
     }
@@ -101,7 +101,7 @@ trait OutputTrait
     {
         $new = clone $this;
 
-        unset($new->settings['page.paperSize']);
+        unset($new->page['paperSize']);
 
         return $new;
     }
@@ -113,7 +113,7 @@ trait OutputTrait
      */
     public function getPaperSize()
     {
-        return $this->settings['page.paperSize'];
+        return $this->page['paperSize'];
     }
 
     /**
@@ -127,7 +127,7 @@ trait OutputTrait
     public function withZoomFactor(ZoomFactor $zoom)
     {
         $new = clone $this;
-        $new->settings['page.zoomFactor'] = $zoom;
+        $new->page['zoomFactor'] = $zoom;
 
         return $new;
     }
@@ -142,7 +142,7 @@ trait OutputTrait
     {
         $new = clone $this;
 
-        unset($new->settings['page.zoomFactor']);
+        unset($new->page['zoomFactor']);
 
         return $new;
     }
@@ -154,21 +154,21 @@ trait OutputTrait
      */
     public function getZoomFactor()
     {
-        return $this->settings['page.zoomFactor'];
+        return $this->page['zoomFactor'];
     }
 
     /**
      * Create new output instance
      * with clip rect set.
      *
-     * @param \JonnyW\PhantomJs\Page\ClipRect $zoom
+     * @param \JonnyW\PhantomJs\Page\ClipRect $clipRect
      *
      * @return \JonnyW\PhantomJs\IO\OutputInterface
      */
-    public function withClipRect(ClipRect $zoom)
+    public function withClipRect(ClipRect $clipRect)
     {
         $new = clone $this;
-        $new->settings['page.clipRect'] = $zoom;
+        $new->page['clipRect'] = $clipRect;
 
         return $new;
     }
@@ -183,7 +183,7 @@ trait OutputTrait
     {
         $new = clone $this;
 
-        unset($new->settings['page.clipRect']);
+        unset($new->page['clipRect']);
 
         return $new;
     }
@@ -195,7 +195,7 @@ trait OutputTrait
      */
     public function getClipRect()
     {
-        return $this->settings['page.clipRect'];
+        return $this->page['clipRect'];
     }
 
     /**

+ 0 - 41
src/IO/StreamOutput.php

@@ -1,41 +0,0 @@
-<?php
-
-/*
- * This file is part of the php-phantomjs.
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace JonnyW\PhantomJs\IO;
-
-/**
- * PHP PhantomJs.
- *
- * @author Jon Wenmoth <contact@jonnyw.me>
- */
-class StreamOutput implements OutputInterface
-{
-    use \GuzzleHttp\Psr7\MessageTrait;
-    use \JonnyW\PhantomJs\IO\OutputTrait;
-
-    /**
-     * Internal constructor.
-     *
-     * @param string|null|resource|StreamInterface $body
-     */
-    public function __construct($body)
-    {
-        $this->stream = \GuzzleHttp\Psr7\stream_for($body);
-    }
-
-    /**
-     * Get output type.
-     *
-     * @return string
-     */
-    public function getType()
-    {
-        return 'stream';
-    }
-}

+ 77 - 0
src/Page/ClipRect.php

@@ -0,0 +1,77 @@
+<?php
+
+/*
+ * This file is part of the php-phantomjs.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace JonnyW\PhantomJs\Page;
+
+/**
+ * PHP PhantomJs.
+ *
+ * @author Jon Wenmoth <contact@jonnyw.me>
+ */
+class ClipRect implements \JsonSerializable
+{
+    /**
+     * Width.
+     *
+     * @var int
+     */
+    private $width;
+
+    /**
+     * Height.
+     *
+     * @var int
+     */
+    private $height;
+
+    /**
+     * Top.
+     *
+     * @var int
+     */
+    private $top;
+
+    /**
+     * Left.
+     *
+     * @var int
+     */
+    private $left;
+
+    /**
+     * Internal constructor.
+     *
+     * @param int $width
+     * @param int $height
+     * @param int $top    (default: 0)
+     * @param int $left   (default: 0)
+     */
+    public function __construct($width, $height, $top = 0, $left = 0)
+    {
+        $this->width = (int) $width;
+        $this->height = (int) $height;
+        $this->top = (int) $top;
+        $this->left = (int) $left;
+    }
+
+    /**
+     * Format data for JSON serialization.
+     *
+     * @return array
+     */
+    public function jsonSerialize()
+    {
+        return array(
+            'top' => $this->top,
+            'left' => $this->left,
+            'width' => $this->width,
+            'height' => $this->height,
+        );
+    }
+}

+ 107 - 0
src/Page/Cookie.php

@@ -0,0 +1,107 @@
+<?php
+
+/*
+ * This file is part of the php-phantomjs.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace JonnyW\PhantomJs\Page;
+
+/**
+ * PHP PhantomJs.
+ *
+ * @author Jon Wenmoth <contact@jonnyw.me>
+ */
+class Cookie implements \JsonSerializable
+{
+    /**
+     * Name.
+     *
+     * @var string
+     */
+    private $name;
+
+    /**
+     * Value.
+     *
+     * @var mixed
+     */
+    private $value;
+
+    /**
+     * Path.
+     *
+     * @var string
+     */
+    private $path;
+
+    /**
+     * Domain.
+     *
+     * @var string
+     */
+    private $domain;
+
+    /**
+     * HTTP Only.
+     *
+     * @var bool
+     */
+    private $httpOnly;
+
+    /**
+     * Secure.
+     *
+     * @var bool
+     */
+    private $secure;
+
+    /**
+     * Expires.
+     *
+     * @var int
+     */
+    private $expires;
+
+    /**
+     * Internal constructor.
+     *
+     * @param string $name
+     * @param mixed  $value
+     * @param string $path
+     * @param string $domain
+     * @param bool   $httpOnly (default: true)
+     * @param bool   $secure   (default: false)
+     * @param int    $expires  (default: null)
+     */
+    public function __construct($name, $value, $path, $domain, $httpOnly = true, $secure = false, $expires = null)
+    {
+        $this->name = $name;
+        $this->value = $value;
+        $this->path = $path;
+        $this->domain = $domain;
+        $this->httpOnly = $httpOnly;
+        $this->secure = $secure;
+        $this->expires = $expires;
+    }
+
+    /**
+     * Format data for JSON serialization.
+     *
+     * @return array
+     */
+    public function jsonSerialize()
+    {
+        return array_filter(array(
+            'name' => $this->name,
+            'value' => $this->value,
+            'path' => $this->path,
+            'domain' => $this->domain,
+            'httponly' => $this->httpOnly,
+            'secure' => $this->secure,
+            'expires' => $this->expires,
+        ));
+    }
+}

+ 77 - 0
src/Page/Margin.php

@@ -0,0 +1,77 @@
+<?php
+
+/*
+ * This file is part of the php-phantomjs.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace JonnyW\PhantomJs\Page;
+
+/**
+ * PHP PhantomJs.
+ *
+ * @author Jon Wenmoth <contact@jonnyw.me>
+ */
+class Margin implements \JsonSerializable
+{
+    /**
+     * Top.
+     *
+     * @var int
+     */
+    private $top;
+
+    /**
+     * Left.
+     *
+     * @var int
+     */
+    private $left;
+
+    /**
+     * Bottom.
+     *
+     * @var int
+     */
+    private $bottom;
+
+    /**
+     * Right.
+     *
+     * @var int
+     */
+    private $right;
+
+    /**
+     * Internal constructor.
+     *
+     * @param int $top    (default: 0)
+     * @param int $left   (default: 0)
+     * @param int $bottom (default: 0)
+     * @param int $right  (default: 0)
+     */
+    public function __construct($top = 0, $left = 0, $bottom = 0, $right = 0)
+    {
+        $this->top = (int) $top;
+        $this->left = (int) $left;
+        $this->bottom = (int) $bottom;
+        $this->right = (int) $right;
+    }
+
+    /**
+     * Format data for JSON serialization.
+     *
+     * @return array
+     */
+    public function jsonSerialize()
+    {
+        return array(
+            'top' => $this->top,
+            'left' => $this->left,
+            'bottom' => $this->bottom,
+            'right' => $this->right,
+        );
+    }
+}

+ 62 - 0
src/Page/PaperFormat.php

@@ -0,0 +1,62 @@
+<?php
+
+/*
+ * This file is part of the php-phantomjs.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace JonnyW\PhantomJs\Page;
+
+/**
+ * PHP PhantomJs.
+ *
+ * @author Jon Wenmoth <contact@jonnyw.me>
+ */
+class PaperFormat extends PaperSize
+{
+    /**
+     * Format.
+     *
+     * @var string
+     */
+    private $format;
+
+    /**
+     * Orientation.
+     *
+     * @var string
+     */
+    private $orientation;
+
+    /**
+     * Internal constructor.
+     *
+     * @param string                           $format
+     * @param string                           $orientation (default: 'portrait')
+     * @param int|JonnyW\PhantomJs\Page\Margin $margin      (default: 0)
+     */
+    public function __construct($format, $orientation = 'portrait', $margin = 0)
+    {
+        $this->format = $format;
+        $this->orientation = $orientation;
+        $this->margin = $margin;
+    }
+
+    /**
+     * Format data for JSON serialization.
+     *
+     * @return array
+     */
+    public function jsonSerialize()
+    {
+        return array_filter(array(
+            'format' => $this->format,
+            'orientation' => $this->orientation,
+            'margin' => $this->margin,
+            'header' => $this.header,
+            'footer' => $this->footer,
+        ));
+    }
+}

+ 111 - 0
src/Page/PaperSize.php

@@ -0,0 +1,111 @@
+<?php
+
+/*
+ * This file is part of the php-phantomjs.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace JonnyW\PhantomJs\Page;
+
+/**
+ * PHP PhantomJs.
+ *
+ * @author Jon Wenmoth <contact@jonnyw.me>
+ */
+class PaperSize implements \JsonSerializable
+{
+    /**
+     * Width.
+     *
+     * @var int
+     */
+    private $width;
+
+    /**
+     * Height.
+     *
+     * @var int
+     */
+    private $height;
+
+    /**
+     * Margin.
+     *
+     * @var int|JonnyW\PhantomJs\Page\Margin
+     */
+    protected $margin;
+
+    /**
+     * Header.
+     *
+     * @var \JonnyW\PhantomJs\Page\PaperBlock
+     */
+    protected $header;
+
+    /**
+     * Footer.
+     *
+     * @var \JonnyW\PhantomJs\Page\PaperBlock
+     */
+    protected $footer;
+
+    /**
+     * Internal constructor.
+     *
+     * @param int                              $width
+     * @param int                              $height
+     * @param int|JonnyW\PhantomJs\Page\Margin $margin (default: 0)
+     */
+    public function __construct($width, $height, $margin = 0)
+    {
+        $this->width = (int) $width;
+        $this->height = (int) $height;
+        $this->margin = $margin;
+    }
+
+    /**
+     * Set header.
+     *
+     * @param \JonnyW\PhantomJs\Page\PaperBlock $header
+     *
+     * @return |JonnyW\PhantomJs\Page\PaperSize
+     */
+    public function setHeader(PaperBlock $header)
+    {
+        $this->header = $header;
+
+        return $this;
+    }
+
+    /**
+     * Set footer.
+     *
+     * @param \JonnyW\PhantomJs\Page\PaperBlock $footer
+     *
+     * @return |JonnyW\PhantomJs\Page\PaperSize
+     */
+    public function setFooter(PaperBlock $footer)
+    {
+        $this->footer = $footer;
+
+        return $this;
+    }
+
+    /**
+     * Format data for JSON serialization.
+     *
+     * @return array
+     */
+    public function jsonSerialize()
+    {
+        return array_filter(array(
+            'width' => $this->width,
+            'height' => $this->height,
+            'margin' => $this->margin,
+            'header' => $this.header,
+            'footer' => $this->footer,
+        ));
+    }
+}

+ 57 - 0
src/Page/ViewportSize.php

@@ -0,0 +1,57 @@
+<?php
+
+/*
+ * This file is part of the php-phantomjs.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace JonnyW\PhantomJs\Page;
+
+/**
+ * PHP PhantomJs.
+ *
+ * @author Jon Wenmoth <contact@jonnyw.me>
+ */
+class ViewportSize implements \JsonSerializable
+{
+    /**
+     * Width.
+     *
+     * @var int
+     */
+    private $width;
+
+    /**
+     * Height.
+     *
+     * @var int
+     */
+    private $height;
+
+    /**
+     * Internal constructor.
+     *
+     * @param int $width
+     * @param int $height
+     */
+    public function __construct($width, $height)
+    {
+        $this->width = (int) $width;
+        $this->height = (int) $height;
+    }
+
+    /**
+     * Format data for JSON serialization.
+     *
+     * @return array
+     */
+    public function jsonSerialize()
+    {
+        return array(
+            'top' => $this->top,
+            'left' => $this->left,
+        );
+    }
+}

+ 47 - 0
src/Page/ZoomFactor.php

@@ -0,0 +1,47 @@
+<?php
+
+/*
+ * This file is part of the php-phantomjs.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace JonnyW\PhantomJs\Page;
+
+/**
+ * PHP PhantomJs.
+ *
+ * @author Jon Wenmoth <contact@jonnyw.me>
+ */
+class ZoomFactor implements \JsonSerializable
+{
+    /**
+     * Zoom.
+     *
+     * @var int
+     */
+    private $zoom;
+
+    /**
+     * Internal constructor.
+     *
+     * @param int $zoom
+     */
+    public function __construct($zoom)
+    {
+        $this->zoom = (int) $zoom;
+    }
+
+    /**
+     * Format data for JSON serialization.
+     *
+     * @return array
+     */
+    public function jsonSerialize()
+    {
+        return array(
+            'zoom' => $this->zoom,
+        );
+    }
+}

+ 3 - 3
src/Resources/procedures/http_default.proc

@@ -68,7 +68,7 @@ page.onResourceRequested = function (req) {
     
     [[ engine.load('page_on_resource_requested') ]]
     
-    {% if output.lazy %}
+    {% if input.lazy %}
         resources++;
         window.clearTimeout(timeout);
     {% endif %}
@@ -81,7 +81,7 @@ page.onResourceReceived = function (res) {
     
     [[ engine.load('page_on_resource_received') ]]
     
-    {% if output.lazy %}
+    {% if input.lazy %}
 
         if(!res.stage || res.stage === 'end') {
             
@@ -126,7 +126,7 @@ page.open('{{ input.requestTarget }}', '{{ input.method }}', '{{ input.body }}',
     
     [[ engine.load('page_body_styles') ]]
     
-    {% if output.lazy %}
+    {% if input.lazy %}
 
         debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Delaying page render until resources have loaded');
         

+ 6 - 5
src/Resources/procedures/procedure_stream.partial → src/Resources/procedures/procedure_buffer.partial

@@ -2,11 +2,12 @@
 if (status === 'success') {
 
     try {
-
-        response.content = page.renderBuffer({
-            format: '{{ output.format }}',
-            quality: {{ output.quality }},
-        });
+        
+        if(output.format == 'PDF') {
+            page.render(fs.workingDirectory + fs.separator + 'output.pdf', { format: '{{ output.format }}', quality: -1 });
+        } else {
+            response.body = page.renderBase64('{{ output.format }}');
+        }
         
         page.close();
 

+ 1 - 1
src/Resources/procedures/procedure_http.partial

@@ -3,7 +3,7 @@ if (status === 'success') {
     
     try {
     
-        response.content = page.evaluate(function () {
+        response.body = page.evaluate(function () {
             return document.getElementsByTagName('html')[0].innerHTML
         });
     

+ 3 - 3
src/Resources/procedures/stream_default.proc

@@ -68,7 +68,7 @@ page.onResourceRequested = function (req) {
     
     [[ engine.load('page_on_resource_requested') ]]
     
-    {% if output.lazy %}
+    {% if input.lazy %}
         resources++;
         window.clearTimeout(timeout);
     {% endif %}
@@ -81,7 +81,7 @@ page.onResourceReceived = function (res) {
     
     [[ engine.load('page_on_resource_received') ]]
     
-    {% if output.lazy %}
+    {% if input.lazy %}
 
         if(!res.stage || res.stage === 'end') {
             
@@ -119,7 +119,7 @@ page.onLoadFinished = function(status) {
     
     [[ engine.load('page_body_styles') ]]
     
-    {% if output.lazy %}
+    {% if input.lazy %}
 
         debug.push(new Date().toISOString().slice(0, -5) + ' [INFO] PhantomJS - Delaying page render until resources have loaded');