Skip to content

Commit c747d57

Browse files
Add support to set temporary directory
1 parent c9fc515 commit c747d57

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ following in configuration `components` section:
2121
'class' => '\Snappy\PdfComponent',
2222
'binary' => '/usr/local/bin/wkhtmltopdf',
2323
'options' => array('orientation' => 'landscape'),
24+
'tempdir' => __DIR__.'/../runtime/pdf',
2425
),
2526
'image' => array(
2627
'class' => '\Snappy\ImageComponent',
2728
'binary' => '/usr/local/bin/wkhtmltoimage',
29+
'tempdir' => __DIR__.'/../runtime/image',
2830
),
2931
```
3032

3133
Specifying paths to binaries is mandatory. Optionally you can configure
3234
default command line arguments that can be overridden or adjusted later.
35+
It is also possible to set temporary directory for both components using
36+
`snappy_tempdir` application parameter.
3337

3438
## Usage
3539

src/AbstractComponent.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ abstract class AbstractComponent extends \CApplicationComponent
2020
* @var array Command line options
2121
*/
2222
public $options = array();
23+
/**
24+
* @var string Path to directory used for temporary files
25+
*/
26+
public $tempdir;
2327

2428
/**
2529
* Creates and returns generator instance.
@@ -33,13 +37,24 @@ abstract protected function getGenerator();
3337
*/
3438
public function __call($name, $parameters)
3539
{
36-
if (method_exists('\Knp\Snappy\GeneratorInterface', $name)) {
37-
return call_user_func_array(
38-
array($this->getGenerator(), $name),
39-
$parameters
40-
);
40+
if (!method_exists('\Knp\Snappy\GeneratorInterface', $name)) {
41+
return parent::__call($name, $parameters);
4142
}
4243

43-
return parent::__call($name, $parameters);
44+
$generator = $this->getGenerator()
45+
->setTemporaryFolder($this->resolveTempdir())
46+
;
47+
48+
return call_user_func_array(array($generator, $name), $parameters);
49+
}
50+
51+
/**
52+
* Resolves path to temporary directory.
53+
*
54+
* @return string|null
55+
*/
56+
protected function resolveTempdir()
57+
{
58+
return $this->tempdir ?: \Yii::app()->params['snappy_tempdir'];
4459
}
4560
}

0 commit comments

Comments
 (0)