diff --git a/.gitignore b/.gitignore index 486d2a6..d6a14f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -/vendor -phpunit.xml +build/ +vendor/ composer.lock - diff --git a/.travis.yml b/.travis.yml index 79314dd..1d5f21b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,20 +1,20 @@ language: php php: - - 5.5 - - 5.6 - - 7.0 - - nightly - - hhvm - -matrix: - allow_failures: - - php: nightly - - php: hhvm + - 7.1 + - 7.2 + - 7.3 + - 7.4 before_script: - travis_retry composer self-update - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source script: - - phpunit + - ./vendor/bin/phpunit + - ./vendor/bin/phpcs + +after_script: + - php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml + - php vendor/bin/php-coveralls + \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f24f486 --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2020, rarog +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md index 4fd161f..5277125 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,16 @@ -dompdfmodule +DompdfHelper ============ -DOMPDF library wrapper as lightweight ZF2/ZF3 module. +DompdfHelper - a lightweight library wrapper Laminas module -[![Build Status](https://travis-ci.org/mikemix/dompdfmodule.svg?branch=master)](https://travis-ci.org/mikemix/dompdfmodule) +[![Build Status](https://travis-ci.org/rarog/dompdf-helper.svg?branch=master)](https://travis-ci.org/rarog/dompdf-helper) +[![Coverage Status](https://coveralls.io/repos/github/rarog/dompdf-helper/badge.svg?branch=master)](https://coveralls.io/github/rarog/dompdf-helper?branch=master) ## Requirements - - [Zend Framework 2 or 3](https://framework.zend.com/) + - [Laminas](https://getlaminas.org/) ## Installation -Installation of DOMPDFModule uses PHP Composer. For more information about +Installation of DompdfHelper uses PHP Composer. For more information about PHP Composer, please visit the official [PHP Composer site](http://getcomposer.org/). #### Installation steps @@ -20,40 +21,85 @@ PHP Composer, please visit the official [PHP Composer site](http://getcomposer.o ```json { "require": { - "mikemix/dompdfmodule": "^3.0" + "rarog/dompdf-helper": "^4.0" } } ``` 3. install PHP Composer via `curl -s http://getcomposer.org/installer | php` (on windows, download http://getcomposer.org/installer and execute it with PHP) 4. run `php composer.phar install` - 5. open `my/project/directory/config/application.config.php` and add the following key to your `modules`: + 5. open `my/project/directory/config/application.config.php` and add the following key to your `modules`: ```php - 'dompdfmodule', + 'DompdfHelper', ``` #### Configuration options -You can override default options via the `dompdf` key in your local or global config files. See the [dompdfmoule\Service\dompdfFactory.php](https://github.com/mikemix/dompdfmodule/blob/master/src/dompdfmodule/Service/dompdfFactory.php#L39) file for the list of default settings. +You can override default options via the `dompdf` key in your local or global config files. See the [config/dompdf.config.php.dist](https://github.com/rarog/dompdf-helper/blob/master/config/dompdf.config.php.dist) file for the list of default settings. -Full list of possible settings is available at the official [DOMPDF library](https://github.com/dompdf/dompdf) site. +Full list of possible settings is available at the official [Dompdf library](https://github.com/dompdf/dompdf) site. #### Example usage -> Side note: use of `getServiceLocator()` in the controller is deprecated since in ZF3. Make sure you create your controller via a factory and inject the Dompdf object in the constructor. +Controller factory ```php get('dompdf') + ); + } +} +``` + +Controller + +```php +dompdf = $dompdf; + } public function indexAction() { - /** @var \Dompdf\Dompdf $dompdf */ - $dompdf = $this->getServiceLocator()->get('dompdf'); - $dompdf->load_html('Ehlo World'); - $dompdf->render(); + $this->dompdf->load_html('Hello World'); + $this->dompdf->render(); - file_put_contents(__DIR__ . '/document.pdf', $dompdf->output()); + file_put_contents(__DIR__ . '/document.pdf', $this->dompdf->output()); } +} ``` diff --git a/composer.json b/composer.json index b132b04..803ddad 100644 --- a/composer.json +++ b/composer.json @@ -1,20 +1,37 @@ { - "name": "mikemix/dompdfmodule", - "type": "library", - "description": "DOMPDF Zend Framework lightweight module", - "keywords": ["pdf","dompdf", "zf2", "zf3"], - "homepage": "https://www.phpcontext.com", - "require": { - "php": "^5.5 || ^7.0", - "dompdf/dompdf": "^0.7.0", - "zendframework/zend-servicemanager": "^2.0 || ^3.0" - }, - "autoload": { - "psr-4": { - "dompdfmodule\\": "src/" - } - }, - "suggest": { - "mikemix/mxdi-module": "Configure dependency injection with annotations" - } + "name" : "rarog/dompdf-helper", + "type" : "library", + "description" : "DompdfHelper - a lightweight library wrapper Laminas module", + "keywords" : [ + "pdf", + "dompdf", + "laminas" + ], + "require" : { + "php" : "^8.0", + "dompdf/dompdf" : "^2.0", + "laminas/laminas-servicemanager" : "^3.6", + "laminas/laminas-modulemanager" : "^2.10" + }, + "require-dev" : { + "phpunit/phpunit" : "^9.0", + "squizlabs/php_codesniffer" : "^3.6", + "php-coveralls/php-coveralls" : "^2.0" + }, + "autoload" : { + "psr-4" : { + "DompdfHelper\\" : "src/" + } + }, + "scripts" : { + "cs-check" : "phpcs", + "cs-fix" : "phpcbf", + "test" : "phpunit" + }, + "authors" : [{ + "name" : "mikemix", + "role" : "Original developer" + } + ], + "license" : "BSD-3-Clause" } diff --git a/config/dompdf.config.php.dist b/config/dompdf.config.php.dist index 11f056e..dc1cfc6 100644 --- a/config/dompdf.config.php.dist +++ b/config/dompdf.config.php.dist @@ -1,26 +1,26 @@ false, - 'defaultMediaType' => 'screen', - 'defaultPaperSize' => 'A4', - 'defaultFont' => 'serif', - 'dpi' => 96, - 'pdfBackend' => 'CPDF', - 'fontHeightRatio' => 1.1, - 'isPhpEnabled' => false, - 'isRemoteEnabled' => false, - 'isJavascriptEnabled' => false, - 'isHtml5ParserEnabled' => true, - 'isFontSubsettingEnabled' => false, - 'debugPng' => false, - 'debugKeepTemp' => false, - 'debugCss' => false, - 'debugLayout' => false, - 'debugLayoutLines' => false, - 'debugLayoutBlocks' => false, - 'debugLayoutInline' => false, - 'debugLayoutPaddingBox' => false, + 'dompdf' => [ + 'logOutputFile' => false, + 'defaultMediaType' => 'screen', + 'defaultPaperSize' => 'A4', + 'defaultFont' => 'serif', + 'dpi' => 96, + 'pdfBackend' => 'CPDF', + 'fontHeightRatio' => 1.1, + 'isPhpEnabled' => false, + 'isRemoteEnabled' => false, + 'isJavascriptEnabled' => false, + 'isHtml5ParserEnabled' => true, + 'isFontSubsettingEnabled' => false, + 'debugPng' => false, + 'debugKeepTemp' => false, + 'debugCss' => false, + 'debugLayout' => false, + 'debugLayoutLines' => false, + 'debugLayoutBlocks' => false, + 'debugLayoutInline' => false, + 'debugLayoutPaddingBox' => false, + ], ]; diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..1294840 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,30 @@ + + + Laminas Coding Standard + + + + + + + + + + + + + + + + + + + + + + + + config + src + test + diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..c0805c1 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,27 @@ + + + + ./test + + + + ./src + + + + + + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 7da1eef..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,22 +0,0 @@ - - - - ./src/Tests - - - - ./src - - - diff --git a/src/Factory/dompdfFactory.php b/src/Factory/DompdfFactory.php similarity index 66% rename from src/Factory/dompdfFactory.php rename to src/Factory/DompdfFactory.php index 0a3d1fa..100902e 100644 --- a/src/Factory/dompdfFactory.php +++ b/src/Factory/DompdfFactory.php @@ -1,37 +1,33 @@ createFromConfig($container->get('config')); } - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this->createFromConfig($serviceLocator->get('config')); - } - + /** + * @param array $config Configuration + * @return Dompdf Dompdf instance + */ protected function createFromConfig(array $config) { $userConfig = isset($config['dompdf']) ? $config['dompdf'] : []; - // evaluate library directory - $dompdfDir = isset($userConfig['DOMPDF_DIR']) ? - $userConfig['DOMPDF_DIR'] : - realpath('vendor/dompdf/dompdf'); - - // merge default config with user config if necessary - $dompdfConfig = count($userConfig) ? - array_merge($this->createDefaultSettings($dompdfDir), $userConfig) : - $this->createDefaultSettings($dompdfDir); + // merge default config with user config + $dompdfConfig = array_merge($this->createDefaultSettings(), $userConfig); // set options $options = new Options(); @@ -46,12 +42,11 @@ protected function createFromConfig(array $config) /** * Some settings can be evaluated by default. - * @param string $dompdfDir DOMPDF library directory * @return array Default settings */ - protected function createDefaultSettings($dompdfDir) + protected function createDefaultSettings(): array { - return array( + return [ 'logOutputFile' => false, 'defaultMediaType' => 'screen', 'defaultPaperSize' => 'A4', @@ -73,6 +68,6 @@ protected function createDefaultSettings($dompdfDir) 'debugLayoutBlocks' => false, 'debugLayoutInline' => false, 'debugLayoutPaddingBox' => false, - ); + ]; } } diff --git a/src/Module.php b/src/Module.php index 158c337..6e44921 100644 --- a/src/Module.php +++ b/src/Module.php @@ -1,8 +1,16 @@ false, ], 'factories' => [ - 'dompdf' => 'dompdfmodule\Factory\dompdfFactory', + 'dompdf' => DompdfFactory::class, ], ], ]; diff --git a/src/Tests/Factory/dompdfFactoryTest.php b/src/Tests/Factory/dompdfFactoryTest.php deleted file mode 100644 index f620011..0000000 --- a/src/Tests/Factory/dompdfFactoryTest.php +++ /dev/null @@ -1,19 +0,0 @@ -factory = new dompdfFactory(); - } - - public function test_it_is_initializable() - { - self::assertInstanceOf('dompdfmodule\Factory\dompdfFactory', $this->factory); - } -} diff --git a/src/Tests/ModuleTest.php b/src/Tests/ModuleTest.php deleted file mode 100644 index 2c2f77c..0000000 --- a/src/Tests/ModuleTest.php +++ /dev/null @@ -1,14 +0,0 @@ -getConfig()); - } -} diff --git a/test/Factory/DompdfFactoryTest.php b/test/Factory/DompdfFactoryTest.php new file mode 100644 index 0000000..912dcd6 --- /dev/null +++ b/test/Factory/DompdfFactoryTest.php @@ -0,0 +1,50 @@ +factory = new DompdfFactory(); + } + + /** + * {@inheritDoc} + * @see \PHPUnit\Framework\TestCase::tearDown() + */ + protected function tearDown(): void + { + unset($this->factory); + } + + /** + * @covers DompdfHelper\Factory\DompdfFactory::__invoke + * @covers DompdfHelper\Factory\DompdfFactory::createFromConfig + * @covers DompdfHelper\Factory\DompdfFactory::createDefaultSettings + */ + public function testInitWentThrough(): void + { + $factory = $this->factory; + + $serviceManager = new ServiceManager(); + $serviceManager->setService('config', []); + + $domPdf = $factory($serviceManager, null); + $this->assertInstanceOf(Dompdf::class, $domPdf); + } +} diff --git a/test/ModuleTest.php b/test/ModuleTest.php new file mode 100644 index 0000000..4f2b252 --- /dev/null +++ b/test/ModuleTest.php @@ -0,0 +1,19 @@ +assertIsArray($module->getConfig()); + } +}