Skip to content

Commit c479871

Browse files
author
Raymond J. Kolbe
committed
Added service manager to test cases.
Added DOMPDF has a callable service. Unique instance of DOMPDF returned each time. Decoupled DOMPDF instance from renderer. It is now injected instead. Refactored configuration loading; DOMPDFFactory now handles this.
1 parent a7bfc57 commit c479871

File tree

9 files changed

+197
-97
lines changed

9 files changed

+197
-97
lines changed

config/module.config.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace DOMPDFModule;
4+
35
return array(
46
'dompdf_module' => array(
57
/**
@@ -270,9 +272,17 @@
270272
)
271273
),
272274
'service_manager' => array(
273-
'factories' => array(
274-
'ViewPdfRenderer' => 'DOMPDFModule\Mvc\Service\ViewPdfRendererFactory',
275-
'ViewPdfStrategy' => 'DOMPDFModule\Mvc\Service\ViewPdfStrategyFactory',
275+
'shared' => array(
276+
/**
277+
* DOMPDF itself has issues rendering twice in a row so we force a
278+
* new instance to be created.
279+
*/
280+
'DOMPDF' => false
281+
),
282+
'factories' => array(
283+
'DOMPDF' => __NAMESPACE__ . '\Service\DOMPDFFactory',
284+
'ViewPdfRenderer' => __NAMESPACE__ . '\Mvc\Service\ViewPdfRendererFactory',
285+
'ViewPdfStrategy' => __NAMESPACE__ . '\Mvc\Service\ViewPdfStrategyFactory',
276286
)
277287
),
278288
);

src/DOMPDFModule/Module.php

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -19,86 +19,8 @@
1919

2020
namespace DOMPDFModule;
2121

22-
use Zend\ModuleManager\ModuleManager;
23-
use Zend\ModuleManager\ModuleEvent;
24-
2522
class Module
2623
{
27-
/**
28-
* An array of keys that map DOMPDF define keys to DOMPDFModule config's
29-
* keys.
30-
*
31-
* @var array
32-
*/
33-
private static $configCompatMapping = array(
34-
'font_directory' => 'DOMPDF_FONT_DIR',
35-
'font_cache_directory' => 'DOMPDF_FONT_CACHE',
36-
'temporary_directory' => 'DOMPDF_TEMP_DIR',
37-
'chroot' => 'DOMPDF_CHROOT',
38-
'unicode_enabled' => 'DOMPDF_UNICODE_ENABLED',
39-
'enable_fontsubsetting' => 'DOMPDF_ENABLE_FONTSUBSETTING',
40-
'pdf_backend' => 'DOMPDF_PDF_BACKEND',
41-
'default_media_type' => 'DOMPDF_DEFAULT_MEDIA_TYPE',
42-
'default_paper_size' => 'DOMPDF_DEFAULT_PAPER_SIZE',
43-
'default_font' => 'DOMPDF_DEFAULT_FONT',
44-
'dpi' => 'DOMPDF_DPI',
45-
'enable_php' => 'DOMPDF_ENABLE_PHP',
46-
'enable_javascript' => 'DOMPDF_ENABLE_JAVASCRIPT',
47-
'enable_remote' => 'DOMPDF_ENABLE_REMOTE',
48-
'log_output_file' => 'DOMPDF_LOG_OUTPUT_FILE',
49-
'font_height_ratio' => 'DOMPDF_FONT_HEIGHT_RATIO',
50-
'enable_css_float' => 'DOMPDF_ENABLE_CSS_FLOAT',
51-
'enable_html5parser' => 'DOMPDF_ENABLE_HTML5PARSER',
52-
'debug_png' => 'DEBUGPNG',
53-
'debug_keep_temp' => 'DEBUGKEEPTEMP',
54-
'debug_css' => 'DEBUGCSS',
55-
'debug_layout' => 'DEBUG_LAYOUT',
56-
'debug_layout_links' => 'DEBUG_LAYOUT_LINES',
57-
'debug_layout_blocks' => 'DEBUG_LAYOUT_BLOCKS',
58-
'debug_layout_inline' => 'DEBUG_LAYOUT_INLINE',
59-
'debug_layout_padding_box' => 'DEBUG_LAYOUT_PADDINGBOX'
60-
);
61-
62-
/**
63-
* @param ModuleManager $moduleManager
64-
* @return void
65-
*/
66-
public function init(ModuleManager $moduleManager)
67-
{
68-
$eventManager = $moduleManager->getEventManager();
69-
$eventManager->attach('loadModules.post', array($this, 'loadConfiguration'));
70-
}
71-
72-
/**
73-
* Event callback called after modules are loaded, responsible
74-
* for loading DOMPDF config.
75-
*
76-
* @param ModuleEvent $e
77-
* @return void
78-
*/
79-
public function loadConfiguration(ModuleEvent $event)
80-
{
81-
define("DOMPDF_DIR", __DIR__ . '/../../vendor/dompdf');
82-
define("DOMPDF_INC_DIR", DOMPDF_DIR . "/include");
83-
define("DOMPDF_LIB_DIR", DOMPDF_DIR . "/lib");
84-
85-
$config = $event->getConfigListener()->getMergedConfig();
86-
foreach ($config['dompdf_module'] as $key => $value) {
87-
if (! array_key_exists($key, self::$configCompatMapping)) {
88-
continue;
89-
}
90-
91-
define(self::$configCompatMapping[$key], $value);
92-
}
93-
94-
define("DOMPDF_AUTOLOAD_PREPEND", false);
95-
96-
require_once DOMPDF_INC_DIR . '/functions.inc.php';
97-
require_once DOMPDF_LIB_DIR . '/html5lib/Parser.php';
98-
require_once DOMPDF_INC_DIR . '/autoload.inc.php';
99-
require_once __DIR__ . '/../../config/module.compat.php';
100-
}
101-
10224
/**
10325
* @return array
10426
*/

src/DOMPDFModule/Mvc/Service/ViewPdfRendererFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public function createService(ServiceLocatorInterface $serviceLocator)
3838
$pdfRenderer = new PdfRenderer();
3939
$pdfRenderer->setResolver($viewManager->getResolver());
4040
$pdfRenderer->setHtmlRenderer($viewManager->getRenderer());
41+
$pdfRenderer->setEngine($serviceLocator->get('dompdf'));
4142

4243
return $pdfRenderer;
4344
}
44-
}
45-
45+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/*
3+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*
15+
* @author Raymond J. Kolbe <[email protected]>
16+
* @copyright Copyright (c) 2012 University of Maine
17+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
18+
*/
19+
20+
namespace DOMPDFModule\Service;
21+
22+
use Zend\ServiceManager\FactoryInterface;
23+
use Zend\ServiceManager\ServiceLocatorInterface;
24+
use DOMPDF;
25+
26+
class DOMPDFFactory implements FactoryInterface
27+
{
28+
/**
29+
* An array of keys that map DOMPDF define keys to DOMPDFModule config's
30+
* keys.
31+
*
32+
* @var array
33+
*/
34+
private static $configCompatMapping = array(
35+
'font_directory' => 'DOMPDF_FONT_DIR',
36+
'font_cache_directory' => 'DOMPDF_FONT_CACHE',
37+
'temporary_directory' => 'DOMPDF_TEMP_DIR',
38+
'chroot' => 'DOMPDF_CHROOT',
39+
'unicode_enabled' => 'DOMPDF_UNICODE_ENABLED',
40+
'enable_fontsubsetting' => 'DOMPDF_ENABLE_FONTSUBSETTING',
41+
'pdf_backend' => 'DOMPDF_PDF_BACKEND',
42+
'default_media_type' => 'DOMPDF_DEFAULT_MEDIA_TYPE',
43+
'default_paper_size' => 'DOMPDF_DEFAULT_PAPER_SIZE',
44+
'default_font' => 'DOMPDF_DEFAULT_FONT',
45+
'dpi' => 'DOMPDF_DPI',
46+
'enable_php' => 'DOMPDF_ENABLE_PHP',
47+
'enable_javascript' => 'DOMPDF_ENABLE_JAVASCRIPT',
48+
'enable_remote' => 'DOMPDF_ENABLE_REMOTE',
49+
'log_output_file' => 'DOMPDF_LOG_OUTPUT_FILE',
50+
'font_height_ratio' => 'DOMPDF_FONT_HEIGHT_RATIO',
51+
'enable_css_float' => 'DOMPDF_ENABLE_CSS_FLOAT',
52+
'enable_html5parser' => 'DOMPDF_ENABLE_HTML5PARSER',
53+
'debug_png' => 'DEBUGPNG',
54+
'debug_keep_temp' => 'DEBUGKEEPTEMP',
55+
'debug_css' => 'DEBUGCSS',
56+
'debug_layout' => 'DEBUG_LAYOUT',
57+
'debug_layout_links' => 'DEBUG_LAYOUT_LINES',
58+
'debug_layout_blocks' => 'DEBUG_LAYOUT_BLOCKS',
59+
'debug_layout_inline' => 'DEBUG_LAYOUT_INLINE',
60+
'debug_layout_padding_box' => 'DEBUG_LAYOUT_PADDINGBOX'
61+
);
62+
63+
/**
64+
* Creates an instance of DOMPDF.
65+
*
66+
* @param ServiceLocatorInterface $serviceLocator
67+
* @return PdfRenderer
68+
*/
69+
public function createService(ServiceLocatorInterface $serviceLocator)
70+
{
71+
$config = $serviceLocator->get('config');
72+
$config = $config['dompdf_module'];
73+
74+
if (defined('DOMPDF_DIR') === false) {
75+
define("DOMPDF_DIR", __DIR__ . '/../../../vendor/dompdf');
76+
}
77+
78+
if (defined('DOMPDF_INC_DIR') === false) {
79+
define("DOMPDF_INC_DIR", DOMPDF_DIR . "/include");
80+
}
81+
82+
if (defined('DOMPDF_LIB_DIR') === false) {
83+
define("DOMPDF_LIB_DIR", DOMPDF_DIR . "/lib");
84+
}
85+
86+
if (defined('DOMPDF_AUTOLOAD_PREPEND') === false) {
87+
define("DOMPDF_AUTOLOAD_PREPEND", false);
88+
}
89+
90+
foreach ($config as $key => $value) {
91+
if (! array_key_exists($key, static::$configCompatMapping)) {
92+
continue;
93+
}
94+
95+
if (defined(static::$configCompatMapping[$key])) {
96+
continue;
97+
}
98+
99+
define(static::$configCompatMapping[$key], $value);
100+
}
101+
102+
require_once DOMPDF_INC_DIR . '/functions.inc.php';
103+
require_once DOMPDF_LIB_DIR . '/html5lib/Parser.php';
104+
require_once DOMPDF_INC_DIR . '/autoload.inc.php';
105+
require_once __DIR__ . '/../../../config/module.compat.php';
106+
107+
return new DOMPDF();
108+
}
109+
}
110+

src/DOMPDFModule/View/Renderer/PdfRenderer.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121

2222
use Zend\View\Renderer\RendererInterface as Renderer;
2323
use Zend\View\Resolver\ResolverInterface as Resolver;
24-
use \DOMPDF;
24+
use DOMPDF;
2525

2626
class PdfRenderer implements Renderer
2727
{
28+
private $dompdf = null;
2829
private $resolver = null;
2930
private $htmlRenderer = null;
3031

@@ -39,6 +40,17 @@ public function getHtmlRenderer()
3940
return $this->htmlRenderer;
4041
}
4142

43+
public function setEngine(DOMPDF $dompdf)
44+
{
45+
$this->dompdf = $dompdf;
46+
return $this;
47+
}
48+
49+
public function getEngine()
50+
{
51+
return $this->dompdf;
52+
}
53+
4254
/**
4355
* Renders values as a PDF
4456
*
@@ -54,7 +66,7 @@ public function render($nameOrModel, $values = null)
5466
$paperOrientation = $nameOrModel->getOption('paperOrientation');
5567
$basePath = $nameOrModel->getOption('basePath');
5668

57-
$pdf = new DOMPDF();
69+
$pdf = $this->getEngine();
5870
$pdf->set_paper($paperSize, $paperOrientation);
5971
$pdf->set_base_path($basePath);
6072

@@ -64,13 +76,9 @@ public function render($nameOrModel, $values = null)
6476
return $pdf->output();
6577
}
6678

67-
public function getEngine()
68-
{
69-
return $this;
70-
}
71-
7279
public function setResolver(Resolver $resolver)
7380
{
7481
$this->resolver = $resolver;
82+
return $this;
7583
}
7684
}

tests/Bootstrap.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
<?php
22

3+
use DOMPDFModuleTest\Framework\TestCase;
34
use Zend\ServiceManager\ServiceManager;
45
use Zend\Mvc\Service\ServiceManagerConfig;
6+
use Zend\Loader\StandardAutoloader;
57

6-
error_reporting( E_ALL | E_STRICT );
8+
error_reporting(E_ALL | E_STRICT);
79

810
chdir(__DIR__);
911

1012
$previousDir = '.';
1113
while (!file_exists('config/application.config.php')) {
1214
$dir = dirname(getcwd());
13-
if($previousDir === $dir) {
15+
if ($previousDir === $dir) {
1416
throw new RuntimeException(
15-
'Unable to locate "config/application.config.php": ' .
16-
'is DOMPDFModule in a subdir of your application skeleton?'
17+
'Unable to locate "config/application.config.php": ' .
18+
'is DOMPDFModule in a subdir of your application skeleton?'
1719
);
1820
}
1921
$previousDir = $dir;
@@ -27,11 +29,22 @@
2729
}
2830

2931
// Assumes PHP Composer autoloader w/compiled classmaps, etc.
30-
require_once('vendor/autoload.php');
32+
require_once 'vendor/autoload.php';
33+
34+
// This namespace is not in classmap.
35+
$loader = new StandardAutoloader(
36+
array(
37+
StandardAutoloader::LOAD_NS => array(
38+
'DOMPDFModuleTest' => __DIR__ . '/DOMPDFModuleTest'
39+
),
40+
));
41+
$loader->register();
3142

3243
$serviceManager = new ServiceManager(new ServiceManagerConfig($configuration['service_manager']));
3344
$serviceManager->setService('ApplicationConfig', $configuration);
3445
$serviceManager->setAllowOverride(true);
3546

3647
$moduleManager = $serviceManager->get('ModuleManager');
3748
$moduleManager->loadModules();
49+
50+
TestCase::setServiceManager($serviceManager);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace DOMPDFModuleTest\Framework;
4+
5+
use PHPUnit_Framework_TestCase;
6+
use Zend\ServiceManager\ServiceManager;
7+
8+
class TestCase extends PHPUnit_Framework_TestCase
9+
{
10+
protected static $serviceManager = null;
11+
12+
public static function setServiceManager(ServiceManager $sm)
13+
{
14+
self::$serviceManager = $sm;
15+
}
16+
17+
public function getServiceManager()
18+
{
19+
return self::$serviceManager;
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DOMPDFModuleTest\Service;
4+
5+
use DOMPDFModuleTest\Framework\TestCase;
6+
7+
class PdfStrategyTest extends TestCase
8+
{
9+
public function testUniqueInstancesFromFactory()
10+
{
11+
$dompdfInstance1 = $this->getServiceManager()->get('dompdf');
12+
$dompdfInstance2 = $this->getServiceManager()->get('dompdf');
13+
14+
$this->assertNotSame($dompdfInstance1, $dompdfInstance2);
15+
}
16+
}

0 commit comments

Comments
 (0)