Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
trsteel88 committed Dec 18, 2014
2 parents bf134fa + 6b4c556 commit 313786f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 67 deletions.
12 changes: 4 additions & 8 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ You will have to download wkhtmltopdf `0.12.x` in order to use Snappy.

## Installation using [Composer](http://getcomposer.org/)

Add to your `composer.json`:

```json
{
"require" : {
"knplabs/knp-snappy": "*"
}
}
```bash
$ composer require knplabs/knp-snappy
```

## Usage
Expand Down Expand Up @@ -61,6 +55,8 @@ $snappy->setOption('no-background', true);
$snappy->setOption('allow', array('/path1', '/path2'));
$snappy->setOption('cookie', array('key' => 'value', 'key2' => 'value2'));
$snappy->setOption('cover', 'pathToCover.html');
// .. or pass a cover as html
$snappy->setOption('cover', '<h1>Bill cover</h1>');
$snappy->setOption('toc', true);
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
],
"extra": {
"branch-alias": {
"dev-master": "0.2.x-dev"
"dev-master": "0.3.x-dev"
}
}
}
71 changes: 58 additions & 13 deletions src/Knp/Snappy/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ abstract class AbstractGenerator implements GeneratorInterface
private $timeout = false;
private $defaultExtension;

/**
* @var string
*/
protected $temporaryFolder;

/**
* @var array
*/
public $temporaryFiles = array();

/**
* Constructor
*
Expand All @@ -36,6 +46,11 @@ public function __construct($binary, array $options = array(), array $env = null
$this->env = $env;
}

public function __destruct()
{
$this->removeTemporaryFiles();
}

/**
* This method must configure the media options
*
Expand Down Expand Up @@ -150,10 +165,6 @@ public function generateFromHtml($html, $output, array $options = array(), $over
}

$this->generate($fileNames, $output, $options, $overwrite);

foreach ($fileNames as $filename) {
$this->unlink($filename);
}
}

/**
Expand All @@ -167,8 +178,6 @@ public function getOutput($input, array $options = array())

$result = $this->getFileContents($filename);

$this->unlink($filename);

return $result;
}

Expand All @@ -188,10 +197,6 @@ public function getOutputFromHtml($html, array $options = array())

$result = $this->getOutput($fileNames, $options);

foreach ($fileNames as $filename) {
$this->unlink($filename);
}

return $result;
}

Expand Down Expand Up @@ -291,7 +296,7 @@ protected function mergeOptions(array $options)
* @param string $output The output filename
* @param string $command The generation command
*
* @throws RuntimeException if the output file generation failed
* @throws \RuntimeException if the output file generation failed
*/
protected function checkOutput($output, $command)
{
Expand Down Expand Up @@ -320,7 +325,7 @@ protected function checkOutput($output, $command)
* @param string $stderr The stderr content
* @param string $command The run command
*
* @throws RuntimeException if the output file generation failed
* @throws \RuntimeException if the output file generation failed
*/
protected function checkProcessStatus($status, $stdout, $stderr, $command)
{
Expand All @@ -346,19 +351,31 @@ protected function checkProcessStatus($status, $stdout, $stderr, $command)
*/
protected function createTemporaryFile($content = null, $extension = null)
{
$filename = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . uniqid('knp_snappy', true);
$filename = rtrim($this->getTemporaryFolder(), DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR . uniqid('knp_snappy', true);

if (null !== $extension) {
$filename .= '.'.$extension;
}

if (null !== $content) {
file_put_contents($filename, $content);
$this->temporaryFiles[md5($filename)] = $filename;
}

return $filename;
}

/**
* Removes all temporary files
*/
protected function removeTemporaryFiles()
{
foreach ($this->temporaryFiles as $file) {
$this->unlink($file);
}
}

/**
* Builds the command string
*
Expand Down Expand Up @@ -503,6 +520,34 @@ protected function prepareOutput($filename, $overwrite)
}
}

/**
* Get TemporaryFolder
*
* @return string
*/
public function getTemporaryFolder()
{
if ($this->temporaryFolder === null) {
return sys_get_temp_dir();
}

return $this->temporaryFolder;
}

/**
* Set temporaryFolder
*
* @param string $temporaryFolder
*
* @return $this
*/
public function setTemporaryFolder($temporaryFolder)
{
$this->temporaryFolder = $temporaryFolder;

return $this;
}

/**
* Wrapper for the "file_get_contents" function
*
Expand Down
4 changes: 2 additions & 2 deletions src/Knp/Snappy/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Image extends AbstractGenerator
/**
* {@inheritDoc}
*/
public function __construct($binary = null, array $options = array())
public function __construct($binary = null, array $options = array(), array $env = null)
{
$this->setDefaultExtension('jpg');

parent::__construct($binary, $options);
parent::__construct($binary, $options, $env);
}

/**
Expand Down
67 changes: 53 additions & 14 deletions src/Knp/Snappy/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,33 @@ class Pdf extends AbstractGenerator
/**
* {@inheritDoc}
*/
public function __construct($binary = null, array $options = array())
public function __construct($binary = null, array $options = array(), array $env = null)
{
$this->setDefaultExtension('pdf');

parent::__construct($binary, $options);
parent::__construct($binary, $options, $env );
}

/**
* handle options to transform HTML header-html or footer-html into files contains HTML
* @param array $options
* @return array $options Tranformed options
* @return array $options Transformed options
*/
protected function handleOptions(array $options = array())
{
$this->hasHtmlHeader = false;
$this->hasHtmlFooter = false;

$headerHtml = isset($options['header-html']) ? $options['header-html'] : null;
if (null !== $headerHtml && !filter_var($headerHtml, FILTER_VALIDATE_URL) && !$this->isFile($headerHtml)) {
$options['header-html'] = $this->createTemporaryFile($headerHtml, 'html');
if ($this->isFileHeader($options) && !$this->isFile($options['header-html'])) {
$options['header-html'] = $this->createTemporaryFile($options['header-html'], 'html');
}

$footerHtml = isset($options['footer-html']) ? $options['footer-html'] : null;
if (null !== $footerHtml && !filter_var($footerHtml, FILTER_VALIDATE_URL) && !$this->isFile($footerHtml)) {
$options['footer-html'] = $this->createTemporaryFile($footerHtml, 'html');
if ($this->isFileFooter($options) && !$this->isFile($options['footer-html'])) {
$options['footer-html'] = $this->createTemporaryFile($options['footer-html'], 'html');
}

if ($this->isFileCover($options) && !$this->isFile($options['cover'])) {
$options['cover'] = $this->createTemporaryFile($options['cover'], 'html');
}

return $options;
Expand All @@ -53,15 +55,52 @@ public function generate($input, $output, array $options = array(), $overwrite =
$options = $this->handleOptions($options);

parent::generate($input, $output, $options, $overwrite);
}

// to delete header or footer generated files
if (array_key_exists('header-html', $options) && !filter_var($options['header-html'], FILTER_VALIDATE_URL)) {
$this->unlink($options['header-html']);
/**
* @param array $options
* @return bool
*/
protected function isFileHeader($options)
{
if (isset($options['header-html'])) {
return !$this->isOptionUrl($options['header-html']);
}
return false;
}

if (array_key_exists('footer-html', $options) && !filter_var($options['footer-html'], FILTER_VALIDATE_URL)) {
$this->unlink($options['footer-html']);
/**
* @param array $options
* @return bool
*/
protected function isFileFooter($options)
{
if (isset($options['footer-html'])) {
return !$this->isOptionUrl($options['footer-html']);
}
return false;
}

/**
* @param array $options
* @return bool
*/
protected function isFileCover($options)
{
if (isset($options['cover'])) {
return !$this->isOptionUrl($options['cover']);
}

return false;
}

/**
* @param $option
* @return bool
*/
protected function isOptionUrl($option)
{
return (bool)filter_var($option, FILTER_VALIDATE_URL);
}

/**
Expand Down
28 changes: 0 additions & 28 deletions test/Knp/Snappy/AbstractGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ public function testGenerateFromHtml()
'configure',
'generate',
'createTemporaryFile',
'unlink'
),
array(
'the_binary'
Expand All @@ -211,12 +210,6 @@ public function testGenerateFromHtml()
)
->will($this->returnValue('the_temporary_file'))
;
$media
->expects($this->once())
->method('unlink')
->with($this->equalTo('the_temporary_file'))
->will($this->returnValue(true))
;
$media
->expects($this->once())
->method('generate')
Expand All @@ -238,7 +231,6 @@ public function testGenerateFromHtmlWithHtmlArray()
'configure',
'generate',
'createTemporaryFile',
'unlink'
),
array(
'the_binary'
Expand All @@ -255,12 +247,6 @@ public function testGenerateFromHtmlWithHtmlArray()
)
->will($this->returnValue('the_temporary_file'))
;
$media
->expects($this->once())
->method('unlink')
->with($this->equalTo('the_temporary_file'))
->will($this->returnValue(true))
;
$media
->expects($this->once())
->method('generate')
Expand Down Expand Up @@ -337,7 +323,6 @@ public function testGetOutputFromHtml()
'configure',
'getOutput',
'createTemporaryFile',
'unlink'
),
array(),
'',
Expand All @@ -361,12 +346,6 @@ public function testGetOutputFromHtml()
)
->will($this->returnValue('the output'))
;
$media
->expects($this->once())
->method('unlink')
->with($this->equalTo('the_temporary_file'))
->will($this->returnValue(true))
;

$this->assertEquals('the output', $media->getOutputFromHtml('<html>foo</html>', array('foo' => 'bar')));
}
Expand All @@ -379,7 +358,6 @@ public function testGetOutputFromHtmlWithHtmlArray()
'configure',
'getOutput',
'createTemporaryFile',
'unlink'
),
array(),
'',
Expand All @@ -403,12 +381,6 @@ public function testGetOutputFromHtmlWithHtmlArray()
)
->will($this->returnValue('the output'))
;
$media
->expects($this->once())
->method('unlink')
->with($this->equalTo('the_temporary_file'))
->will($this->returnValue(true))
;

$this->assertEquals('the output', $media->getOutputFromHtml(array('<html>foo</html>'), array('foo' => 'bar')));
}
Expand Down
Loading

0 comments on commit 313786f

Please sign in to comment.