From eda2045648e1e038107370554cabb577e96fa6e7 Mon Sep 17 00:00:00 2001 From: DaanDeSmedt Date: Fri, 17 Nov 2017 00:12:43 +0100 Subject: [PATCH 1/4] Initial commit --- composer.json | 25 + composer.lock | 1172 +++++++++++++++++++++++++++++ examples/example.php | 7 + phpunit.xml | 19 + src/HeadlessChrome.php | 38 + tests/HeadlessChromeTestSuite.php | 19 + tests/bootstrap.php | 3 + 7 files changed, 1283 insertions(+) create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 examples/example.php create mode 100644 phpunit.xml create mode 100644 src/HeadlessChrome.php create mode 100644 tests/HeadlessChromeTestSuite.php create mode 100644 tests/bootstrap.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4eb969e --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "daandesmedt/phpheadlesschrome", + "description": "A PHP wrapper for using Google Chrome Headless mode. Convert URL or HTML to a PDF / screenshot. Easy to use and OOP interfaced.", + "type": "librbrary", + "require": { + "mikehaertl/php-shellcommand": "^1.2" + }, + "license": "MIT", + "keywords": ["html","headless","php","pdf","chrome","screenshots"], + "authors": [ + { + "name": "Daan De Smedt", + "email": "daan.de.smedt@hotmail.com" + } + ], + "minimum-stability": "stable", + "autoload": { + "psr-4": { + "daandesmedt\\PHPHeadlessChrome\\": "src/" + } + }, + "require-dev": { + "phpunit/phpunit": "*" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..4bdc7fa --- /dev/null +++ b/composer.lock @@ -0,0 +1,1172 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "1b71ac19bfa924a3a420ff74479ddab6", + "content-hash": "380fdd92ce7b92529c4ef253414c7345", + "packages": [ + { + "name": "mikehaertl/php-shellcommand", + "version": "1.2.5", + "source": { + "type": "git", + "url": "https://github.com/mikehaertl/php-shellcommand.git", + "reference": "35a81f344bd771b0b1c1ae3f4dc986dddfd234b9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mikehaertl/php-shellcommand/zipball/35a81f344bd771b0b1c1ae3f4dc986dddfd234b9", + "reference": "35a81f344bd771b0b1c1ae3f4dc986dddfd234b9", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "mikehaertl\\shellcommand\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Härtl", + "email": "haertl.mike@gmail.com" + } + ], + "description": "An object oriented interface to shell commands", + "keywords": [ + "shell" + ], + "time": "2017-06-30 06:21:01" + } + ], + "packages-dev": [ + { + "name": "doctrine/instantiator", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/doctrine/instantiator.git", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/8e884e78f9f0eb1329e445619e04456e64d8051d", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d", + "shasum": "" + }, + "require": { + "php": ">=5.3,<8.0-DEV" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "ext-pdo": "*", + "ext-phar": "*", + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://github.com/doctrine/instantiator", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2015-06-14 21:17:01" + }, + { + "name": "phpdocumentor/reflection-common", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionCommon.git", + "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", + "reference": "21bdeb5f65d7ebf9f43b1b25d404f87deab5bfb6", + "shasum": "" + }, + "require": { + "php": ">=5.5" + }, + "require-dev": { + "phpunit/phpunit": "^4.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jaap van Otterdijk", + "email": "opensource@ijaap.nl" + } + ], + "description": "Common reflection classes used by phpdocumentor to reflect the code structure", + "homepage": "http://www.phpdoc.org", + "keywords": [ + "FQSEN", + "phpDocumentor", + "phpdoc", + "reflection", + "static analysis" + ], + "time": "2017-09-11 18:02:19" + }, + { + "name": "phpdocumentor/reflection-docblock", + "version": "3.2.2", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", + "reference": "4aada1f93c72c35e22fb1383b47fee43b8f1d157" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/4aada1f93c72c35e22fb1383b47fee43b8f1d157", + "reference": "4aada1f93c72c35e22fb1383b47fee43b8f1d157", + "shasum": "" + }, + "require": { + "php": ">=5.5", + "phpdocumentor/reflection-common": "^1.0@dev", + "phpdocumentor/type-resolver": "^0.3.0", + "webmozart/assert": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^4.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", + "time": "2017-08-08 06:39:58" + }, + { + "name": "phpdocumentor/type-resolver", + "version": "0.3.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/TypeResolver.git", + "reference": "fb3933512008d8162b3cdf9e18dba9309b7c3773" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/fb3933512008d8162b3cdf9e18dba9309b7c3773", + "reference": "fb3933512008d8162b3cdf9e18dba9309b7c3773", + "shasum": "" + }, + "require": { + "php": "^5.5 || ^7.0", + "phpdocumentor/reflection-common": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^0.9.4", + "phpunit/phpunit": "^5.2||^4.8.24" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "time": "2017-06-03 08:32:36" + }, + { + "name": "phpspec/prophecy", + "version": "v1.7.2", + "source": { + "type": "git", + "url": "https://github.com/phpspec/prophecy.git", + "reference": "c9b8c6088acd19d769d4cc0ffa60a9fe34344bd6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/c9b8c6088acd19d769d4cc0ffa60a9fe34344bd6", + "reference": "c9b8c6088acd19d769d4cc0ffa60a9fe34344bd6", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "php": "^5.3|^7.0", + "phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0", + "sebastian/comparator": "^1.1|^2.0", + "sebastian/recursion-context": "^1.0|^2.0|^3.0" + }, + "require-dev": { + "phpspec/phpspec": "^2.5|^3.2", + "phpunit/phpunit": "^4.8 || ^5.6.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.7.x-dev" + } + }, + "autoload": { + "psr-0": { + "Prophecy\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + }, + { + "name": "Marcello Duarte", + "email": "marcello.duarte@gmail.com" + } + ], + "description": "Highly opinionated mocking framework for PHP 5.3+", + "homepage": "https://github.com/phpspec/prophecy", + "keywords": [ + "Double", + "Dummy", + "fake", + "mock", + "spy", + "stub" + ], + "time": "2017-09-04 11:05:03" + }, + { + "name": "phpunit/php-code-coverage", + "version": "2.2.4", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "eabf68b476ac7d0f73793aada060f1c1a9bf8979" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/eabf68b476ac7d0f73793aada060f1c1a9bf8979", + "reference": "eabf68b476ac7d0f73793aada060f1c1a9bf8979", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "phpunit/php-file-iterator": "~1.3", + "phpunit/php-text-template": "~1.2", + "phpunit/php-token-stream": "~1.3", + "sebastian/environment": "^1.3.2", + "sebastian/version": "~1.0" + }, + "require-dev": { + "ext-xdebug": ">=2.1.4", + "phpunit/phpunit": "~4" + }, + "suggest": { + "ext-dom": "*", + "ext-xdebug": ">=2.2.1", + "ext-xmlwriter": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.2.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "time": "2015-10-06 15:47:00" + }, + { + "name": "phpunit/php-file-iterator", + "version": "1.4.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "3cc8f69b3028d0f96a9078e6295d86e9bf019be5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/3cc8f69b3028d0f96a9078e6295d86e9bf019be5", + "reference": "3cc8f69b3028d0f96a9078e6295d86e9bf019be5", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "time": "2016-10-03 07:40:28" + }, + { + "name": "phpunit/php-text-template", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "time": "2015-06-21 13:50:34" + }, + { + "name": "phpunit/php-timer", + "version": "1.0.9", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "3dcf38ca72b158baf0bc245e9184d3fdffa9c46f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/3dcf38ca72b158baf0bc245e9184d3fdffa9c46f", + "reference": "3dcf38ca72b158baf0bc245e9184d3fdffa9c46f", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "time": "2017-02-26 11:10:40" + }, + { + "name": "phpunit/php-token-stream", + "version": "1.4.11", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-token-stream.git", + "reference": "e03f8f67534427a787e21a385a67ec3ca6978ea7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/e03f8f67534427a787e21a385a67ec3ca6978ea7", + "reference": "e03f8f67534427a787e21a385a67ec3ca6978ea7", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Wrapper around PHP's tokenizer extension.", + "homepage": "https://github.com/sebastianbergmann/php-token-stream/", + "keywords": [ + "tokenizer" + ], + "time": "2017-02-27 10:12:30" + }, + { + "name": "phpunit/phpunit", + "version": "4.8.36", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "46023de9a91eec7dfb06cc56cb4e260017298517" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/46023de9a91eec7dfb06cc56cb4e260017298517", + "reference": "46023de9a91eec7dfb06cc56cb4e260017298517", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-json": "*", + "ext-pcre": "*", + "ext-reflection": "*", + "ext-spl": "*", + "php": ">=5.3.3", + "phpspec/prophecy": "^1.3.1", + "phpunit/php-code-coverage": "~2.1", + "phpunit/php-file-iterator": "~1.4", + "phpunit/php-text-template": "~1.2", + "phpunit/php-timer": "^1.0.6", + "phpunit/phpunit-mock-objects": "~2.3", + "sebastian/comparator": "~1.2.2", + "sebastian/diff": "~1.2", + "sebastian/environment": "~1.3", + "sebastian/exporter": "~1.2", + "sebastian/global-state": "~1.0", + "sebastian/version": "~1.0", + "symfony/yaml": "~2.1|~3.0" + }, + "suggest": { + "phpunit/php-invoker": "~1.1" + }, + "bin": [ + "phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.8.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "https://phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "time": "2017-06-21 08:07:12" + }, + { + "name": "phpunit/phpunit-mock-objects", + "version": "2.3.8", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", + "reference": "ac8e7a3db35738d56ee9a76e78a4e03d97628983" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/ac8e7a3db35738d56ee9a76e78a4e03d97628983", + "reference": "ac8e7a3db35738d56ee9a76e78a4e03d97628983", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "php": ">=5.3.3", + "phpunit/php-text-template": "~1.2", + "sebastian/exporter": "~1.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "suggest": { + "ext-soap": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.3.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Mock Object library for PHPUnit", + "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", + "keywords": [ + "mock", + "xunit" + ], + "time": "2015-10-02 06:51:40" + }, + { + "name": "sebastian/comparator", + "version": "1.2.4", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "2b7424b55f5047b47ac6e5ccb20b2aea4011d9be" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/2b7424b55f5047b47ac6e5ccb20b2aea4011d9be", + "reference": "2b7424b55f5047b47ac6e5ccb20b2aea4011d9be", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/diff": "~1.2", + "sebastian/exporter": "~1.2 || ~2.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "http://www.github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "time": "2017-01-29 09:50:25" + }, + { + "name": "sebastian/diff", + "version": "1.4.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "7f066a26a962dbe58ddea9f72a4e82874a3975a4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/7f066a26a962dbe58ddea9f72a4e82874a3975a4", + "reference": "7f066a26a962dbe58ddea9f72a4e82874a3975a4", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Diff implementation", + "homepage": "https://github.com/sebastianbergmann/diff", + "keywords": [ + "diff" + ], + "time": "2017-05-22 07:24:03" + }, + { + "name": "sebastian/environment", + "version": "1.3.8", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/environment.git", + "reference": "be2c607e43ce4c89ecd60e75c6a85c126e754aea" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/be2c607e43ce4c89ecd60e75c6a85c126e754aea", + "reference": "be2c607e43ce4c89ecd60e75c6a85c126e754aea", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8 || ^5.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides functionality to handle HHVM/PHP environments", + "homepage": "http://www.github.com/sebastianbergmann/environment", + "keywords": [ + "Xdebug", + "environment", + "hhvm" + ], + "time": "2016-08-18 05:49:44" + }, + { + "name": "sebastian/exporter", + "version": "1.2.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/42c4c2eec485ee3e159ec9884f95b431287edde4", + "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/recursion-context": "~1.0" + }, + "require-dev": { + "ext-mbstring": "*", + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "http://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "time": "2016-06-17 09:04:28" + }, + { + "name": "sebastian/global-state", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/global-state.git", + "reference": "bc37d50fea7d017d3d340f230811c9f1d7280af4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/bc37d50fea7d017d3d340f230811c9f1d7280af4", + "reference": "bc37d50fea7d017d3d340f230811c9f1d7280af4", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "suggest": { + "ext-uopz": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Snapshotting of global state", + "homepage": "http://www.github.com/sebastianbergmann/global-state", + "keywords": [ + "global state" + ], + "time": "2015-10-12 03:26:01" + }, + { + "name": "sebastian/recursion-context", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "b19cc3298482a335a95f3016d2f8a6950f0fbcd7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/b19cc3298482a335a95f3016d2f8a6950f0fbcd7", + "reference": "b19cc3298482a335a95f3016d2f8a6950f0fbcd7", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "time": "2016-10-03 07:41:43" + }, + { + "name": "sebastian/version", + "version": "1.0.6", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/version.git", + "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", + "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", + "shasum": "" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that helps with managing the version number of Git-hosted PHP projects", + "homepage": "https://github.com/sebastianbergmann/version", + "time": "2015-06-21 13:59:46" + }, + { + "name": "symfony/yaml", + "version": "v3.3.13", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "0938408c4faa518d95230deabb5f595bf0de31b9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/0938408c4faa518d95230deabb5f595bf0de31b9", + "reference": "0938408c4faa518d95230deabb5f595bf0de31b9", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8" + }, + "require-dev": { + "symfony/console": "~2.8|~3.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2017-11-10 18:26:04" + }, + { + "name": "webmozart/assert", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/webmozart/assert.git", + "reference": "2db61e59ff05fe5126d152bd0655c9ea113e550f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozart/assert/zipball/2db61e59ff05fe5126d152bd0655c9ea113e550f", + "reference": "2db61e59ff05fe5126d152bd0655c9ea113e550f", + "shasum": "" + }, + "require": { + "php": "^5.3.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.6", + "sebastian/version": "^1.0.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "time": "2016-11-23 20:04:58" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/examples/example.php b/examples/example.php new file mode 100644 index 0000000..baeaaab --- /dev/null +++ b/examples/example.php @@ -0,0 +1,7 @@ + + + + + + tests/ + + + \ No newline at end of file diff --git a/src/HeadlessChrome.php b/src/HeadlessChrome.php new file mode 100644 index 0000000..2bf3b58 --- /dev/null +++ b/src/HeadlessChrome.php @@ -0,0 +1,38 @@ +binaryPath = $binaryPath; + } + +} \ No newline at end of file diff --git a/tests/HeadlessChromeTestSuite.php b/tests/HeadlessChromeTestSuite.php new file mode 100644 index 0000000..bfb31a2 --- /dev/null +++ b/tests/HeadlessChromeTestSuite.php @@ -0,0 +1,19 @@ +assertEquals( + true, + true + ); + } + +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..7aea9dc --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,3 @@ + Date: Fri, 17 Nov 2017 00:25:16 +0100 Subject: [PATCH 2/4] Initial README.md --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e096045 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +PHP ChromeToPdf +=============== + +Headless Chrome is shipping in Chrome 59. It's a way to run the Chrome browser in a headless environment. Essentially, running Chrome without chrome! It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line. + +PHPHeadlessChrome provides a simple usage helper class to create PDF and / or screenshots using Headless Chrome. +Trigger PDF / Screenshots generation from local file / URL. + +In order to use this PHPHeadlessChrome helper make sure Google Chrome is correctly installer from version 59 and onwards. \ No newline at end of file From af3aea6b02fe69ff10435028fc511fa87d01aa9f Mon Sep 17 00:00:00 2001 From: DaanDeSmedt Date: Fri, 17 Nov 2017 00:26:18 +0100 Subject: [PATCH 3/4] Updated README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e096045..17e6019 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -PHP ChromeToPdf +PHPHeadlessChrome =============== Headless Chrome is shipping in Chrome 59. It's a way to run the Chrome browser in a headless environment. Essentially, running Chrome without chrome! It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line. From 28bb71659971ef256218bdee71d24007d0059e48 Mon Sep 17 00:00:00 2001 From: DaanDeSmedt Date: Mon, 20 Nov 2017 13:34:46 +0100 Subject: [PATCH 4/4] Source / Tests --- composer.json | 4 +- composer.lock | 2 +- examples/PDF.php | 15 ++ examples/PDFCustomWindowSize.php | 16 ++ examples/PDFFromHTML.php | 15 ++ examples/PDFFromHTMLFile.php | 15 ++ examples/PDFMobileScreen.php | 16 ++ examples/Screenshot.php | 15 ++ examples/ScreenshotCustomWindowSize.php | 16 ++ examples/ScreenshotFromHTML.php | 15 ++ examples/ScreenshotFromHTMLFile.php | 15 ++ examples/ScreenshotMobileScreen.php | 16 ++ examples/assets/HTMLFile.html | 12 + examples/example.php | 7 - phpunit.xml | 31 +-- src/HeadlessChrome.php | 322 +++++++++++++++++++++++- tests/HeadlessChromeTestSuite.php | 126 +++++++++- 17 files changed, 617 insertions(+), 41 deletions(-) create mode 100644 examples/PDF.php create mode 100644 examples/PDFCustomWindowSize.php create mode 100644 examples/PDFFromHTML.php create mode 100644 examples/PDFFromHTMLFile.php create mode 100644 examples/PDFMobileScreen.php create mode 100644 examples/Screenshot.php create mode 100644 examples/ScreenshotCustomWindowSize.php create mode 100644 examples/ScreenshotFromHTML.php create mode 100644 examples/ScreenshotFromHTMLFile.php create mode 100644 examples/ScreenshotMobileScreen.php create mode 100644 examples/assets/HTMLFile.html delete mode 100644 examples/example.php diff --git a/composer.json b/composer.json index 4eb969e..4fc4990 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,12 @@ { "name": "daandesmedt/phpheadlesschrome", "description": "A PHP wrapper for using Google Chrome Headless mode. Convert URL or HTML to a PDF / screenshot. Easy to use and OOP interfaced.", - "type": "librbrary", + "type": "library", "require": { "mikehaertl/php-shellcommand": "^1.2" }, "license": "MIT", - "keywords": ["html","headless","php","pdf","chrome","screenshots"], + "keywords": ["HTML","headless","php","pdf","chrome","screenshots"], "authors": [ { "name": "Daan De Smedt", diff --git a/composer.lock b/composer.lock index 4bdc7fa..ecf396d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "1b71ac19bfa924a3a420ff74479ddab6", + "hash": "964d744cccf44aeffae8954f2ef03caa", "content-hash": "380fdd92ce7b92529c4ef253414c7345", "packages": [ { diff --git a/examples/PDF.php b/examples/PDF.php new file mode 100644 index 0000000..c589292 --- /dev/null +++ b/examples/PDF.php @@ -0,0 +1,15 @@ +setUrl('http://www.google.be'); +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); +// or $headlessChromer = new HeadlessChrome('http://www.google.be','C:\Program Files (x86)\Google\Chrome\Application\chrome'); + +$headlessChromer->setOutputDirectory(__DIR__); +$headlessChromer->toPDF('output.pdf'); + +print 'PDF generated to : ' . $headlessChromer->getFilePath(); \ No newline at end of file diff --git a/examples/PDFCustomWindowSize.php b/examples/PDFCustomWindowSize.php new file mode 100644 index 0000000..9571f9c --- /dev/null +++ b/examples/PDFCustomWindowSize.php @@ -0,0 +1,16 @@ +setUrl('http://www.google.be'); +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); +// or $headlessChromer = new HeadlessChrome('http://www.google.be','C:\Program Files (x86)\Google\Chrome\Application\chrome'); + +$headlessChromer->setOutputDirectory(__DIR__); +$headlessChromer->setWindowSize(375, 667); +$headlessChromer->toPDF('output.pdf'); + +print 'PDF generated to : ' . $headlessChromer->getFilePath(); \ No newline at end of file diff --git a/examples/PDFFromHTML.php b/examples/PDFFromHTML.php new file mode 100644 index 0000000..fe4e21b --- /dev/null +++ b/examples/PDFFromHTML.php @@ -0,0 +1,15 @@ +setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); +// or $headlessChromer = new HeadlessChrome(null,'C:\Program Files (x86)\Google\Chrome\Application\chrome'); + +$headlessChromer->setOutputDirectory(__DIR__); +$headlessChromer->setHTML('

Headless Chrome PHP example

HTML to PDF

'); +$headlessChromer->toPDF('output.pdf'); + +print 'PDF generated to : ' . $headlessChromer->getFilePath(); \ No newline at end of file diff --git a/examples/PDFFromHTMLFile.php b/examples/PDFFromHTMLFile.php new file mode 100644 index 0000000..b54294c --- /dev/null +++ b/examples/PDFFromHTMLFile.php @@ -0,0 +1,15 @@ +setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); +// or $headlessChromer = new HeadlessChrome(null,'C:\Program Files (x86)\Google\Chrome\Application\chrome'); + +$headlessChromer->setOutputDirectory(__DIR__); +$headlessChromer->setHTMLFile(__DIR__ . '\assets\HTMLFile.html'); +$headlessChromer->toPDF('output.pdf'); + +print 'PDF generated to : ' . $headlessChromer->getFilePath(); \ No newline at end of file diff --git a/examples/PDFMobileScreen.php b/examples/PDFMobileScreen.php new file mode 100644 index 0000000..3e64195 --- /dev/null +++ b/examples/PDFMobileScreen.php @@ -0,0 +1,16 @@ +setUrl('http://www.google.be'); +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); +// or $headlessChromer = new HeadlessChrome('http://www.google.be','C:\Program Files (x86)\Google\Chrome\Application\chrome'); + +$headlessChromer->setOutputDirectory(__DIR__); +$headlessChromer->useMobile(); +$headlessChromer->toPDF('output.pdf'); + +print 'PDF generated to : ' . $headlessChromer->getFilePath(); \ No newline at end of file diff --git a/examples/Screenshot.php b/examples/Screenshot.php new file mode 100644 index 0000000..bfad02f --- /dev/null +++ b/examples/Screenshot.php @@ -0,0 +1,15 @@ +setUrl('http://www.google.be'); +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); +// or $headlessChromer = new HeadlessChrome('http://www.google.be','C:\Program Files (x86)\Google\Chrome\Application\chrome'); + +$headlessChromer->setOutputDirectory(__DIR__); +$headlessChromer->toScreenShot('output.jpg'); + +print 'Screenshot saved to : ' . $headlessChromer->getFilePath(); \ No newline at end of file diff --git a/examples/ScreenshotCustomWindowSize.php b/examples/ScreenshotCustomWindowSize.php new file mode 100644 index 0000000..5219c6f --- /dev/null +++ b/examples/ScreenshotCustomWindowSize.php @@ -0,0 +1,16 @@ +setUrl('http://www.google.be'); +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); +// or $headlessChromer = new HeadlessChrome('http://www.google.be','C:\Program Files (x86)\Google\Chrome\Application\chrome'); + +$headlessChromer->setOutputDirectory(__DIR__); +$headlessChromer->setWindowSize(375, 667); +$headlessChromer->toScreenShot('output.jpg'); + +print 'Screenshot saved to : ' . $headlessChromer->getFilePath(); \ No newline at end of file diff --git a/examples/ScreenshotFromHTML.php b/examples/ScreenshotFromHTML.php new file mode 100644 index 0000000..84dd0fa --- /dev/null +++ b/examples/ScreenshotFromHTML.php @@ -0,0 +1,15 @@ +setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); +// or $headlessChromer = new HeadlessChrome(null,'C:\Program Files (x86)\Google\Chrome\Application\chrome'); + +$headlessChromer->setOutputDirectory(__DIR__); +$headlessChromer->setHTML('

Headless Chrome PHP example

HTML to PDF

'); +$headlessChromer->toScreenShot('output.jpg'); + +print 'Screenshot saved to : ' . $headlessChromer->getFilePath(); \ No newline at end of file diff --git a/examples/ScreenshotFromHTMLFile.php b/examples/ScreenshotFromHTMLFile.php new file mode 100644 index 0000000..f79d285 --- /dev/null +++ b/examples/ScreenshotFromHTMLFile.php @@ -0,0 +1,15 @@ +setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); +// or $headlessChromer = new HeadlessChrome(null,'C:\Program Files (x86)\Google\Chrome\Application\chrome'); + +$headlessChromer->setOutputDirectory(__DIR__); +$headlessChromer->setHTMLFile(__DIR__ . '\assets\HTMLFile.html'); +$headlessChromer->toScreenShot('output.jpg'); + +print 'Screenshot saved to : ' . $headlessChromer->getFilePath(); \ No newline at end of file diff --git a/examples/ScreenshotMobileScreen.php b/examples/ScreenshotMobileScreen.php new file mode 100644 index 0000000..f95d62b --- /dev/null +++ b/examples/ScreenshotMobileScreen.php @@ -0,0 +1,16 @@ +setUrl('http://www.google.be'); +$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome'); +// or $headlessChromer = new HeadlessChrome('http://www.google.be','C:\Program Files (x86)\Google\Chrome\Application\chrome'); + +$headlessChromer->setOutputDirectory(__DIR__); +$headlessChromer->useMobile(); +$headlessChromer->toScreenShot('output.jpg'); + +print 'Screenshot saved to : ' . $headlessChromer->getFilePath(); \ No newline at end of file diff --git a/examples/assets/HTMLFile.html b/examples/assets/HTMLFile.html new file mode 100644 index 0000000..d3be57d --- /dev/null +++ b/examples/assets/HTMLFile.html @@ -0,0 +1,12 @@ + + + +Test HTML File + + + + +

This is a very simple HTML file.

+ + + \ No newline at end of file diff --git a/examples/example.php b/examples/example.php deleted file mode 100644 index baeaaab..0000000 --- a/examples/example.php +++ /dev/null @@ -1,7 +0,0 @@ - - - - - tests/ - - + + + + tests + + + \ No newline at end of file diff --git a/src/HeadlessChrome.php b/src/HeadlessChrome.php index 2bf3b58..6b3c2cd 100644 --- a/src/HeadlessChrome.php +++ b/src/HeadlessChrome.php @@ -5,25 +5,104 @@ use Exception; use mikehaertl\shellcommand\Command; -class HeadlessChrome -{ + +class HeadlessChrome { /** * Path to the Chrome binary * @var String */ private $binaryPath; - + + /** + * Argument list to execute for Headless Chrome + * @var String + */ + private $arguments; + + /** + * Output directory for render + * @var String + */ + private $outputDirectory; + + /** + * URL to be rendered + * @var String + */ + private $url; + + /** + * Recerence to command + * @var mikehaertl\shellcommand\Command; + */ + private $command; + + /** + * Reference to the last know filepath + * @var String + */ + private $filePath; + /** * HeadlessChrome constructor * @param String $url The URL to be convereted * @param String $binaryPath Path to the Chrome binary + * + * Open chrome and type + * Check the value for + * */ - public function __construct($url = '', $binaryPath = ''){ + public function __construct($url = null, $binaryPath = null, $outputDirectory = null){ - // provide default options + $this->setArguments([ + '--headless' => '', + '--disable-gpu' => '', + '--incognito' => '', + '--enable-viewport' => '' + ]); + + if(isset($binaryPath)) { + $this->setBinaryPath($binaryPath); + } + + if (isset($url)){ + $this->setUrl($url); + } + + if(isset($outputDirectory)){ + $this->setOutputDirectory($outputDirectory); + } else { + $this->setOutputDirectory(sys_get_temp_dir()); + } + + } + + /** + * Set a list of arguments + * @param Array $arguments + */ + public function setArguments(array $arguments) { + foreach ($arguments as $argument => $value) { + $this->setArgument($argument, $value); + } + } + + + /** + * Set single argument + * @param String $argument + * @param String $value + */ + public function setArgument($argument, $value) { + if(!strlen(trim($argument)) && !strlen(trim($value))) return; + $argument = trim($argument); + if (!empty($value) && !strstr($argument, '=')) { + $argument .= '='; + } + $this->arguments[$argument] = trim($value); } @@ -35,4 +114,237 @@ public function setBinaryPath($binaryPath){ $this->binaryPath = $binaryPath; } + + /** + * Set the target URL + * @param String $url + */ + public function setUrl($url) { + $this->url = trim($url); + } + + + /** + * Set the directory for render output (PDF / Screenshot) + * @param String $directory + */ + public function setOutputDirectory($directory) + { + $this->outputDirectory = trim($directory); + if (!file_exists($directory)) { + @mkdir($directory); + } + } + + + /** + * Set the Chrome window size + * @param integer $width + * @param integer $height + */ + public function setWindowSize($width, $height) { + $this->setArgument('--window-size', $width . ',' . $height); + } + + + /** + * Set raw HTML + * @param String $html + */ + public function setHTML($html) { + $this->setUrl('data:text/html,' . rawurlencode($html)); + } + + + /** + * Set HTML file (file:// protocol) + * @param String $filePath + * @throws Exception If the file is not found + */ + public function setHTMLFile($filePath) { + if (!file_exists($filePath)) { + throw new Exception("$filePath not found"); + } + $this->setUrl("file://$filePath"); + } + + + /** + * Sets the mobile mode + */ + public function useMobile(){ + $this->setArgument('--user-agent', 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'); + } + + + /** + * + * Get path to the Chrome binary + * @return String $binaryPath + */ + public function getBinaryPath(){ + return $this->binaryPath; + } + + + /** + * Get the target URL + * @return String $url + */ + public function getUrl() { + return $this->url; + } + + + /** + * Get the directory for render output (PDF / Screenshot) + * @return String $directory + */ + public function getOutputDirectory() { + return $this->outputDirectory; + } + + + /** + * Gets arguments list used for executing Headless Chrome + * @return Array + */ + public function getArguments(){ + return $this->arguments; + } + + + /** + * Returns a unique filename with the given extension + * @param String $extension (pdf / html) + * @return String unique filename + */ + private function getUniqueName($extension) { + return md5(date('Y-m-d H:i:s:u')) . '.' . $extension; + } + + + /** + * To PDF for the specified URL + * @param String $PDFfilename + * @return String $location + * @throws Exception + */ + public function toPDF($PDFFilename = null){ + + if(!isset($PDFFilename)){ + $PDFfilename = $this->getUniqueName('pdf'); + } + + if ($PDFFilename && !strstr($PDFFilename, '.pdf')) { + $PDFFilename .= '.pdf'; + } + + $location = $this->getOutputDirectory() . '\\' . $PDFFilename; + $this->filePath = $location; + + $specific_arguments = [ + '--print-to-pdf=' => $location + ]; + + $arguments = array_merge($specific_arguments, $this->getArguments()); + if (!$this->executeChrome($arguments)) { + throw new Exception('An error occurred while creating PDF ; <' . $this->getExitCode() . '> with message "' . $this->getError() . '"'); + } + + return $location; + + } + + /** + * Convert the provided url to image and return the image's location + * @param String $imageFilename + * @return String $location + * @throws Exception + */ + public function toScreenShot($imageFilename = null) { + + if(!isset($imageFilename)){ + $imageFilename = $this->getUniqueName('jpg'); + } + + if ($imageFilename && !strstr($imageFilename, '.jpg') && !strstr($imageFilename, '.png')) { + $imageFilename .= '.jpg'; + } + + $location = $this->getOutputDirectory() . '\\' . $imageFilename; + $this->filePath = $location; + + $specific_arguments = [ + '--screenshot=' => $location + ]; + + $arguments = array_merge($specific_arguments, $this->getArguments()); + if (!$this->executeChrome($arguments)) { + throw new Exception('An error occurred while creating screenshot ; <' . $this->getExitCode() . '> with message "' . $this->getError() . '"'); + } + + return $location; + + } + + + /** + * Execute Chrome using all provided arguments + * @param Array $arguments + * @return Boolean + */ + private function executeChrome(array $arguments) { + $this->command = new Command('' . $this->binaryPath . ''); + foreach ($arguments as $argument => $value) { + $this->command->addArg($argument, $value ? $value : null); + } + $this->command->addArg($this->url, null); + + if (!$this->command->execute()) { + return false; + } + return true; + } + + + /** + * Get the last error message + * @return String the error message, either stderr or internal message. Empty if none. + */ + public function getError(){ + if(!$this->command) return; + return $this->command->getError(); + } + + + /** + * Get the last command exit code + * @return Int|null the exit code or null if command was not executed yet + */ + public function getExitCode(){ + if(!$this->command) return; + return $this->command->getExitCode(); + } + + + /** + * Get command string command + * @return string the current command string to execute + */ + public function getCommandString(){ + if(!$this->command) return; + return $this->command->__toString(); + } + + + /** + * Get last know filePath location + * @return string the current command string to execute + */ + public function getFilePath(){ + if(!$this->command) return; + return $this->filePath; + } + } \ No newline at end of file diff --git a/tests/HeadlessChromeTestSuite.php b/tests/HeadlessChromeTestSuite.php index bfb31a2..c087e94 100644 --- a/tests/HeadlessChromeTestSuite.php +++ b/tests/HeadlessChromeTestSuite.php @@ -1,19 +1,123 @@ url, $this->binaryPath); + $this->assertEquals($this->url, $headlessChromer->getUrl()); + $this->assertEquals($this->binaryPath, $headlessChromer->getBinaryPath()); + } + + + public function testSetBinaryPath() { + $headlessChromer = new HeadlessChrome(); + $headlessChromer->setBinaryPath($this->binaryPath); + $this->assertEquals($this->binaryPath, $headlessChromer->getBinaryPath()); + } + + + public function testSetURL() { + $headlessChromer = new HeadlessChrome(); + $headlessChromer->setUrl($this->url); + $this->assertEquals($this->url, $headlessChromer->getUrl()); + } + + + public function testSetOutputDirectory() { + $headlessChromer = new HeadlessChrome(); + $headlessChromer->setOutputDirectory($this->ouputDirectory); + $this->assertEquals($this->ouputDirectory, $headlessChromer->getOutputDirectory()); + } + + + public function testSetArguments() { + $arguments = [ + '--headless' => '', + '--disable-gpu' => '', + '--argument' => '', + '----extra-argument=' => 'test-value' + ]; + $headlessChromer = new HeadlessChrome(); + $headlessChromer->setArguments($arguments); + foreach ($arguments as $argument => $value) { + $this->assertArrayHasKey($argument, $headlessChromer->getArguments()); + $this->assertEquals($value, $headlessChromer->getArguments()[$argument]); + } + } + + + public function testSetArgumentsEmpty() { + $arguments = [ + '' => '', + ' ' => ' ', + ' ' => ' =', + ' --argument-with-value' => ' =' + ]; + $headlessChromer = new HeadlessChrome(); + $count = count($headlessChromer->getArguments()); + $headlessChromer->setArguments($arguments); + $this->assertCount($count + 2, $headlessChromer->getArguments()); + } + + + public function testSetArgument() { + $arguments = [ + '--headless' => '', + '--disable-gpu' => '', + '--incognito' => '', + '--enable-viewport' => '', + '--added-argument=' => 'value', + ]; + $headlessChromer = new HeadlessChrome(); + $headlessChromer->setArgument('--added-argument','value'); + foreach ($arguments as $argument => $value) { + $this->assertArrayHasKey($argument, $headlessChromer->getArguments()); + $this->assertEquals($value, $headlessChromer->getArguments()[$argument]); + } + } + + + public function testWindowSize() { + $headlessChromer = new HeadlessChrome(); + $headlessChromer->setWindowSize(1024,2048); + $this->assertEquals('1024,2048', $headlessChromer->getArguments()['--window-size=']); + } + + + public function testSetHTML() { + $HTML = 'test HTML content'; + $headlessChromer = new HeadlessChrome(); + $headlessChromer->setHTML($HTML); + $this->assertEquals('data:text/html,' . rawurlencode($HTML), $headlessChromer->getUrl()); + } -final class HeadlessChromeTestSuite extends TestCase -{ - public function test(){ - $this->assertEquals( - true, - true - ); + public function testSetHTMLFile() { + $filePath = __DIR__ . '\..\examples\assets\HTMLFile.html'; + $headlessChromer = new HeadlessChrome(); + $headlessChromer->setHTMLFile($filePath); + $this->assertEquals("file://$filePath", $headlessChromer->getUrl()); + } + + public function testSetHTMLFileException() { + $filePath = __DIR__ . '\HTMLFile.html'; + $headlessChromer = new HeadlessChrome(); + $this->setExpectedException(Exception::class); + $headlessChromer->setHTMLFile($filePath); + } + + public function testUseMobile() { + $headlessChromer = new HeadlessChrome(); + $headlessChromer->useMobile(); + $this->assertEquals('Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30', $headlessChromer->getArguments()['--user-agent=']); } -} +} \ No newline at end of file