Skip to content

Commit 91364c8

Browse files
authored
Use streams instead of temp files (2.0) (#13)
* Use streams instead of temp files * Test and docfix * Fix tests
1 parent 8d2bc44 commit 91364c8

File tree

14 files changed

+104
-502
lines changed

14 files changed

+104
-502
lines changed

.travis.yml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
language: php
22

33
php:
4-
- 5.3
5-
- 5.6
6-
7-
env:
8-
- SYMFONY_VERSION=2.3.*
9-
- SYMFONY_VERSION=2.8.*
10-
- SYMFONY_VERSION=3.0.*
4+
- 5.5
5+
- 7.0
116

127
matrix:
138
fast_finish: true
14-
exclude:
15-
- php: 5.3
16-
env: SYMFONY_VERSION=3.0.*
179

1810
before_script:
19-
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update
2011
- composer update --dev
2112

2213
script: phpunit

ConfigHelper.php

Lines changed: 8 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Padam87\RasterizeBundle;
44

5+
use Symfony\Component\Process\InputStream;
56
use Symfony\Component\Process\Process;
67
use Symfony\Component\Process\ProcessBuilder;
7-
use Symfony\Component\Routing\RequestContext;
88

99
class ConfigHelper
1010
{
@@ -14,195 +14,37 @@ class ConfigHelper
1414
protected $config;
1515

1616
/**
17-
* @var string
17+
* @param array $config
1818
*/
19-
protected $rootDir;
20-
21-
/**
22-
* @var RequestContext
23-
*/
24-
protected $context;
25-
26-
/**
27-
* @var string
28-
*/
29-
protected $contextBaseUrl;
30-
31-
/**
32-
* @param array $config
33-
* @param string $rootDir
34-
* @param RequestContext $context
35-
* @param string $contextBaseUrl
36-
*/
37-
public function __construct(array $config, $rootDir, RequestContext $context, $contextBaseUrl = "")
19+
public function __construct(array $config)
3820
{
3921
$this->config = $config;
40-
$this->rootDir = $rootDir;
41-
$this->context = $context;
42-
$this->contextBaseUrl = $contextBaseUrl;
4322
}
4423

4524
/**
46-
* @return array
47-
*/
48-
public function getConfig()
49-
{
50-
return $this->config;
51-
}
52-
53-
/**
54-
* @param array $config
55-
*
56-
* @return $this
57-
*/
58-
public function setConfig($config)
59-
{
60-
$this->config = $config;
61-
62-
return $this;
63-
}
64-
65-
/**
66-
* @return string
67-
*/
68-
public function getRootDir()
69-
{
70-
return $this->rootDir;
71-
}
72-
73-
/**
74-
* @param string $rootDir
75-
*
76-
* @return $this
77-
*/
78-
public function setRootDir($rootDir)
79-
{
80-
$this->rootDir = $rootDir;
81-
82-
return $this;
83-
}
84-
85-
/**
86-
* @return RequestContext
87-
*/
88-
public function getContext()
89-
{
90-
return $this->context;
91-
}
92-
93-
/**
94-
* @param RequestContext $context
95-
*
96-
* @return $this
97-
*/
98-
public function setContext(RequestContext $context)
99-
{
100-
$this->context = $context;
101-
102-
return $this;
103-
}
104-
105-
/**
106-
* @return string
107-
*/
108-
public function getContextBaseUrl()
109-
{
110-
return $this->contextBaseUrl;
111-
}
112-
113-
/**
114-
* @param string $contextBaseUrl
115-
*
116-
* @return $this
117-
*/
118-
public function setContextBaseUrl($contextBaseUrl)
119-
{
120-
$this->contextBaseUrl = $contextBaseUrl;
121-
122-
return $this;
123-
}
124-
125-
/**
126-
* @param string $url
127-
* @param string $uniqueId
128-
* @param array $arguments
25+
* @param InputStream $input
26+
* @param array $arguments
12927
*
13028
* @return Process
13129
*/
132-
public function buildProcess($url, $uniqueId, $arguments = array())
30+
public function buildProcess($input, $arguments = array())
13331
{
134-
$script = $this->getWebDir() . DIRECTORY_SEPARATOR . $this->config['script'];
135-
$output = $this->getOutputFilePath($uniqueId);
136-
13732
$builder = new ProcessBuilder();
138-
13933
$builder
14034
->setPrefix($this->config['phantomjs']['callable'])
14135
->setArguments(
14236
array_merge(
14337
$this->processPhantomjsOptions(),
144-
array($script, $url, $output),
38+
[$this->config['script']],
14539
array_values(array_merge($this->config['arguments'], $arguments))
14640
)
14741
)
42+
->setInput($input)
14843
;
14944

15045
return $builder->getProcess();
15146
}
15247

153-
/**
154-
* @param string $uniqueId
155-
*
156-
* @return string
157-
*/
158-
public function getInputFilePath($uniqueId)
159-
{
160-
return $this->getTempDir() . DIRECTORY_SEPARATOR . $uniqueId . '.html';
161-
}
162-
163-
/**
164-
* @param string $uniqueId
165-
*
166-
* @return string
167-
*/
168-
public function getOutputFilePath($uniqueId)
169-
{
170-
return $this->getTempDir() . DIRECTORY_SEPARATOR . $uniqueId . '.' . $this->config['arguments']['format'];
171-
}
172-
173-
/**
174-
* @param $uniqueId
175-
*
176-
* @return string
177-
*/
178-
public function getOutputFileUrl($uniqueId)
179-
{
180-
return sprintf(
181-
"%s://%s%s%s/%s.html",
182-
$this->context->getScheme(),
183-
$this->context->getHost(),
184-
$this->contextBaseUrl === "" ? $this->context->getBaseUrl() : $this->contextBaseUrl,
185-
$this->config['temp_dir'],
186-
$uniqueId
187-
);
188-
}
189-
190-
/**
191-
* @return string
192-
*/
193-
protected function getTempDir()
194-
{
195-
return $this->getWebDir() . $this->config['temp_dir'];
196-
}
197-
198-
/**
199-
* @return string
200-
*/
201-
protected function getWebDir()
202-
{
203-
return $this->rootDir . $this->config['web_dir'];
204-
}
205-
20648
/**
20749
* @return array
20850
*/

DependencyInjection/Configuration.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,50 @@ public function getConfigTreeBuilder()
1717

1818
$rootNode
1919
->children()
20-
->scalarNode('web_dir')
21-
->defaultValue('/../web')
22-
->info('Temp dir location related to %kernel.root_dir%.')
23-
->end()
24-
->scalarNode('temp_dir')
25-
->defaultValue('/bundles/padam87rasterize/temp')
26-
->info('Temp dir location related to web dir. Must be in a location accessible by the web server.')
27-
->end()
2820
->arrayNode('phantomjs')
2921
->addDefaultsIfNotSet()
3022
->children()
3123
->scalarNode('callable')->defaultValue('phantomjs')->end()
3224
->arrayNode('options')
33-
->info("https://github.com/ariya/phantomjs/wiki/API-Reference#wiki-command-line-options")
25+
->info('http://phantomjs.org/api/command-line.html')
26+
->defaultValue(
27+
[
28+
'--output-encoding' => $this->isWin() ? 'ISO-8859-1' : 'UTF-8',
29+
]
30+
)
3431
->normalizeKeys(false)
32+
->beforeNormalization()
33+
->ifTrue(function ($v) {
34+
return !isset($v['--output-encoding']);
35+
})
36+
->then(function ($v) {
37+
return array_merge(
38+
[
39+
'--output-encoding' => $this->isWin() ? 'ISO-8859-1' : 'UTF-8',
40+
],
41+
$v
42+
);
43+
})
44+
->end()
3545
->prototype('scalar')->end()
3646
->end()
3747
->end()
3848
->end()
3949
->scalarNode('script')
40-
->defaultValue('/bundles/padam87rasterize/js/rasterize.js')->info('Relative to web dir')
50+
->defaultValue('../web/bundles/padam87rasterize/js/rasterize.js')->info('Relative to root dir')
4151
->end()
4252
->arrayNode('arguments')
43-
->defaultValue(array(
44-
'format' => 'pdf'
45-
))
53+
->defaultValue(
54+
[
55+
'format' => 'pdf'
56+
]
57+
)
4658
->beforeNormalization()
4759
->ifTrue(function ($v) {
4860
return !isset($v['format']);
4961
})
5062
->then(function ($v) {
51-
return array_merge(array('format' => 'pdf'), $v);
63+
return array_merge(['format' => 'pdf'], $v);
5264
})
5365
->end()
5466
->prototype('scalar')->end()
@@ -58,4 +70,9 @@ public function getConfigTreeBuilder()
5870

5971
return $treeBuilder;
6072
}
73+
74+
private function isWin()
75+
{
76+
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
77+
}
6178
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ $response = new Response(
2525
);
2626
```
2727

28-
[Installation](Resources/docs/install.md)
28+
[Installation](Resources/docs/configuration_reference.md)
2929

3030
[Configuration reference](Resources/docs/configuration_reference.md)
3131

0 commit comments

Comments
 (0)