Skip to content

Commit 8bae2c9

Browse files
authored
3.0 (#15)
* Updated dependencies, php compatibility, no longer tied to phantomjs * Added docs for puppeteer * Use composer's phpunit for travis * Fixed phpunit test case namespace * Fixed psr4 config
1 parent 4adc34b commit 8bae2c9

File tree

15 files changed

+136
-229
lines changed

15 files changed

+136
-229
lines changed

.travis.yml

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

33
php:
4-
- 5.5
5-
- 7.0
4+
- 7.1
65

76
matrix:
87
fast_finish: true
98

109
before_script:
1110
- composer update --dev
1211

13-
script: phpunit
12+
script: vendor/bin/phpunit
1413

1514
after_script:
1615
- php vendor/bin/coveralls -v

ConfigHelper.php

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,30 @@
77

88
class ConfigHelper
99
{
10-
/**
11-
* @var array
12-
*/
13-
protected $config;
10+
private $config;
11+
private $projectDir;
1412

15-
/**
16-
* @param array $config
17-
*/
18-
public function __construct(array $config)
13+
public function __construct(string $projectDir, array $config)
1914
{
2015
$this->config = $config;
16+
$this->projectDir = $projectDir;
2117
}
2218

23-
/**
24-
* @param InputStream $input
25-
* @param array $arguments
26-
*
27-
* @return Process
28-
*/
29-
public function buildProcess($input, $arguments = array())
19+
public function buildProcess(InputStream $input, array $arguments = []): Process
3020
{
3121
$process = new Process(
3222
array_merge(
33-
[$this->config['phantomjs']['callable']],
34-
$this->processPhantomjsOptions(),
35-
[ $this->config['script'] ],
23+
[
24+
$this->config['script']['callable'],
25+
$this->projectDir . DIRECTORY_SEPARATOR . $this->config['script']['path']
26+
],
3627
array_values(array_merge($this->config['arguments'], $arguments))
3728
),
38-
null, [], $input
29+
null,
30+
[],
31+
$input
3932
);
4033

4134
return $process;
4235
}
43-
44-
/**
45-
* @return array
46-
*/
47-
protected function processPhantomjsOptions()
48-
{
49-
$options = array();
50-
51-
foreach ($this->config['phantomjs']['options'] as $name => $value) {
52-
if (is_numeric($name)) {
53-
$options[] = $value;
54-
} else {
55-
if (is_bool($value)) {
56-
$value = ($value) ? 'true' : 'false';
57-
}
58-
$options[] = sprintf('%s=%s', $name, $value);
59-
}
60-
}
61-
62-
return $options;
63-
}
6436
}

DependencyInjection/Configuration.php

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
66
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
use Symfony\Component\HttpKernel\Kernel;
78

89
class Configuration implements ConfigurationInterface
910
{
1011
/**
11-
* {@inheritDoc}
12+
* {@inheritdoc}
1213
*/
1314
public function getConfigTreeBuilder()
1415
{
@@ -17,38 +18,16 @@ public function getConfigTreeBuilder()
1718

1819
$rootNode
1920
->children()
20-
->arrayNode('phantomjs')
21+
->arrayNode('script')
2122
->addDefaultsIfNotSet()
2223
->children()
23-
->scalarNode('callable')->defaultValue('phantomjs')->end()
24-
->arrayNode('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-
)
31-
->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()
45-
->prototype('scalar')->end()
24+
->scalarNode('callable')->defaultValue('node')->end()
25+
->scalarNode('path')
26+
->defaultValue($this->getAssetsDir() . DIRECTORY_SEPARATOR . 'rasterize.js')
27+
->info('Relative to project dir')
4628
->end()
4729
->end()
4830
->end()
49-
->scalarNode('script')
50-
->defaultValue('../web/bundles/padam87rasterize/js/rasterize.js')->info('Relative to root dir')
51-
->end()
5231
->arrayNode('arguments')
5332
->defaultValue(
5433
[
@@ -71,8 +50,13 @@ public function getConfigTreeBuilder()
7150
return $treeBuilder;
7251
}
7352

74-
private function isWin()
53+
private function getAssetsDir()
7554
{
76-
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
55+
switch (Kernel::MAJOR_VERSION) {
56+
case 4:
57+
return 'assets';
58+
default:
59+
return 'web';
60+
}
7761
}
7862
}

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ $response = new Response(
2929

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

32+
**Providers**
33+
- [Puppeteer](Resources/docs/provider/puppeteer.md)
34+
- [PhantomJS](Resources/docs/provider/phantomjs.md)
35+
- [Other](Resources/docs/provider/other.md)
36+
3237
**How to...**
3338
- [pass arguments to the javascript file?](Resources/docs/how_to_pass_arguments.md)
34-
- [ignore SSL errors?](Resources/docs/how_to_ignore_ssl_errors.md)
35-
3639

3740

Rasterizer.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,16 @@
77

88
class Rasterizer
99
{
10-
/**
11-
* @var ConfigHelper
12-
*/
1310
protected $configHelper;
14-
15-
/**
16-
* @var Stopwatch
17-
*/
1811
protected $stopwatch;
1912

20-
/**
21-
* @var array
22-
*/
23-
protected $environment;
24-
25-
/**
26-
* @param ConfigHelper $configHelper
27-
* @param Stopwatch $stopwatch
28-
* @param string $environment
29-
*/
30-
public function __construct(ConfigHelper $configHelper, Stopwatch $stopwatch = null, $environment)
13+
public function __construct(ConfigHelper $configHelper, Stopwatch $stopwatch = null)
3114
{
3215
$this->configHelper = $configHelper;
3316
$this->stopwatch = $stopwatch;
34-
$this->environment = [ $environment ];
3517
}
3618

37-
/**
38-
* @param string $html
39-
* @param array $arguments
40-
*
41-
* @return string
42-
*/
43-
public function rasterize($html, $arguments = array())
19+
public function rasterize(string $html, $arguments = []): string
4420
{
4521
if ($this->stopwatch instanceof Stopwatch) {
4622
$this->stopwatch->start('rasterizer');
@@ -49,7 +25,7 @@ public function rasterize($html, $arguments = array())
4925
$input = new InputStream();
5026

5127
$process = $this->configHelper->buildProcess($input, $arguments);
52-
$process->start(null, $this->environment);
28+
$process->start(null, []);
5329

5430
$input->write($html);
5531
$input->close();

Resources/config/services.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
<services>
1414
<service id="padam87_rasterize.config_helper" class="%padam87_rasterize.config_helper.class%">
15+
<argument>%kernel.project_dir%</argument>
1516
<argument>%padam87_rasterize.config%</argument>
1617
</service>
1718
<service id="padam87_rasterize.rasterizer" class="%padam87_rasterize.rasterizer.class%" public="true">
1819
<argument type="service" id="padam87_rasterize.config_helper" />
1920
<argument type="service" id="debug.stopwatch" on-invalid="null" />
20-
<argument>%kernel.environment%</argument>
2121
</service>
2222
</services>
2323
</container>

Resources/docs/configuration_reference.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
```YAML
66
padam87_rasterize:
7-
phantomjs:
8-
callable: phantomjs
9-
options: # http://phantomjs.org/api/command-line.html
10-
'--output-encoding': 'UTF-8' # will default to ISO-8859-1 on windows
11-
script: /bundles/padam87rasterize/js/rasterize.js # Relative to web dir
12-
arguments: # You can define your own custom arguments. Will be added by default to every process.
13-
format: pdf # Default, will always be added, even if you remove it from here.
7+
script:
8+
callable: node
9+
path: web\rasterize.js # Relative to project dir
10+
arguments:
11+
format: pdf # Default, will always be added, even if you remove it from here.
1412
```

Resources/docs/how_to_ignore_ssl_errors.md

Lines changed: 0 additions & 12 deletions
This file was deleted.

Resources/docs/how_to_pass_arguments.md

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ First, the configuration should be changed
66

77
```yaml
88
padam87_rasterize:
9-
script: /js/my-rasterize.js
109
arguments:
11-
format: pdf
1210
orientation: portrait
1311
```
1412
@@ -17,24 +15,11 @@ Note that the orientation argument has been added, by default it will be set as
1715
A custom javascript is also necessary, to handle the newly received argument.
1816

1917
```js
20-
var page = require('webpage').create(),
21-
system = require('system'),
22-
format;
23-
24-
format = system.args[1];
25-
orientation = system.args[2];
26-
27-
system.stdin.setEncoding('UTF-8'); // force utf8 input encoding even when output is different
28-
var content = system.stdin.read();
29-
30-
page.setContent(content, 'http://localhost');
31-
page.viewportSize = { width: 1920, height: 1080 };
32-
page.paperSize = { format: 'A4', orientation: orientation, border: '1cm' };
33-
34-
page.onLoadFinished = function(success) {
35-
page.render('/dev/stdout', {format: 'pdf'});
36-
phantom.exit();
37-
};
18+
// ...
19+
const args = process.argv;
20+
const format = args[2];
21+
const orientation = args[3];
22+
// ...
3823
```
3924

4025
To change the orientation to `landscape`, you need to add one more parameter to the rasterizer call.

Resources/docs/install.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ $bundles = array(
1313
);
1414
```
1515

16-
### Install assets ###
16+
### Chose a provider ###
1717

18-
`php app/console assets:install`
18+
- [Puppeteer](provider/puppeteer.md)
19+
- [PhantomJS](provider/phantomjs.md)
20+
- [Other](provider/other.md)

0 commit comments

Comments
 (0)