Skip to content

Commit c3c0f62

Browse files
authored
Env vars and process customization (#18)
* Env vars and process customization * Documentation for env vars and custom process
1 parent 676014a commit c3c0f62

File tree

8 files changed

+89
-4
lines changed

8 files changed

+89
-4
lines changed

ConfigHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct(string $projectDir, array $config)
1616
$this->projectDir = $projectDir;
1717
}
1818

19-
public function buildProcess(InputStream $input, array $arguments = []): Process
19+
public function buildProcess(InputStream $input, array $arguments = [], array $env = []): Process
2020
{
2121
$process = new Process(
2222
array_merge(
@@ -27,7 +27,7 @@ public function buildProcess(InputStream $input, array $arguments = []): Process
2727
array_values(array_merge($this->config['arguments'], $arguments))
2828
),
2929
null,
30-
[],
30+
array_merge($this->config['env_vars'], $env),
3131
$input
3232
);
3333

DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function getConfigTreeBuilder()
4444
->end()
4545
->prototype('scalar')->end()
4646
->end()
47+
->arrayNode('env_vars')
48+
->prototype('scalar')->end()
49+
->end()
4750
->end()
4851
;
4952

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ $response = new Response(
3636

3737
**How to...**
3838
- [pass arguments to the javascript file?](Resources/docs/how_to_pass_arguments.md)
39+
- [fully customize the Symfony Process?](Resources/docs/how_to_customize_the_process.md)
40+
3941

4042

Rasterizer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@ public function __construct(ConfigHelper $configHelper, Stopwatch $stopwatch = n
1616
$this->stopwatch = $stopwatch;
1717
}
1818

19-
public function rasterize(string $html, $arguments = []): string
19+
public function rasterize(string $html, array $arguments = [], array $env = [], callable $callback = null): string
2020
{
2121
if ($this->stopwatch instanceof Stopwatch) {
2222
$this->stopwatch->start('rasterizer');
2323
}
2424

2525
$input = new InputStream();
2626

27-
$process = $this->configHelper->buildProcess($input, $arguments);
27+
$process = $this->configHelper->buildProcess($input, $arguments, $env);
28+
29+
if ($callback) {
30+
$callback($process);
31+
}
32+
2833
$process->start(null, []);
2934

3035
$input->write($html);

Resources/docs/configuration_reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ padam87_rasterize:
99
path: web\rasterize.js # Relative to project dir
1010
arguments:
1111
format: pdf # Default, will always be added, even if you remove it from here.
12+
env_vars: # Empty by default
13+
# NODE_PATH: /path/to/node
1214
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# How to fully customize the process?
2+
3+
The rasterize methods accepts a callback as the 4th parameter.
4+
5+
```php
6+
$response = new Response(
7+
$this->get('padam87_rasterize.rasterizer')->rasterize(
8+
$this->renderView('Bundle:Folder:template.pdf.twig'),
9+
[],
10+
[],
11+
function (Process $process) {
12+
// HERE
13+
}
14+
),
15+
200, [
16+
'Content-Type' => 'application/pdf',
17+
'Content-Disposition' => 'attachment; filename="my.pdf"'
18+
]
19+
);
20+
```

Tests/ConfigHelperTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function setUp()
3030
],
3131
'arguments' => [
3232
'format' => 'pdf'
33+
],
34+
'env_vars' => [
35+
'NODE_PATH' => '/usr/local/lib/node_modules',
3336
]
3437
];
3538
}
@@ -48,5 +51,32 @@ public function isTheProcessBuilt()
4851
implode(DIRECTORY_SEPARATOR, ['node ' . __DIR__, 'assets', 'rasterize.js']) . ' pdf',
4952
str_replace(['"', "'"], '', $process->getCommandLine())
5053
);
54+
55+
$this->assertCount(1, $process->getEnv());
56+
}
57+
58+
/**
59+
* @test
60+
*/
61+
public function attributeMerge()
62+
{
63+
$configHelper = new ConfigHelper(__DIR__, $this->config);
64+
$process = $configHelper->buildProcess(new InputStream(), ['paper' => 'A4']);
65+
66+
$this->assertEquals(
67+
implode(DIRECTORY_SEPARATOR, ['node ' . __DIR__, 'assets', 'rasterize.js']) . ' pdf A4',
68+
str_replace(['"', "'"], '', $process->getCommandLine())
69+
);
70+
}
71+
72+
/**
73+
* @test
74+
*/
75+
public function envMerge()
76+
{
77+
$configHelper = new ConfigHelper(__DIR__, $this->config);
78+
$process = $configHelper->buildProcess(new InputStream(), [], ['MY_ENV' => 'something']);
79+
80+
$this->assertCount(2, $process->getEnv());
5181
}
5282
}

Tests/RasterizerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,27 @@ public function testRasterize()
4646

4747
$this->assertSame('pdfcontent', $rasterizer->rasterize('<html></html>'));
4848
}
49+
50+
/**
51+
* @test
52+
*/
53+
public function testCallback()
54+
{
55+
$this->stopwatch->shouldReceive('start')->once();
56+
$this->stopwatch->shouldReceive('stop')->once();
57+
58+
$this->configHelper->shouldReceive('buildProcess')->once()->andReturn($this->process);
59+
60+
$this->process->shouldReceive('start');
61+
$this->process->shouldReceive('wait');
62+
$this->process->shouldReceive('setTimeout');
63+
$this->process->shouldReceive('getOutput')->andReturn('pdfcontent');
64+
65+
$rasterizer = new Rasterizer($this->configHelper, $this->stopwatch);
66+
$output = $rasterizer->rasterize('<html></html>', [], [], function (Process $process) {
67+
$process->setTimeout(999);
68+
});
69+
70+
$this->assertSame('pdfcontent', $output);
71+
}
4972
}

0 commit comments

Comments
 (0)