Skip to content

Commit

Permalink
Add option to set path to node
Browse files Browse the repository at this point in the history
  • Loading branch information
notFloran committed Jan 17, 2021
1 parent d5b6345 commit 9df66a6
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.5

* A new option to set the path to the node binary is available #35

## 3.4.1 (2020-04-26)

* Remove suggest of SwiftMailer
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ mjml:
renderer: binary # default: binary
options:
binary: '%kernel.project_dir%/node_modules/.bin/mjml' # default: mjml
node: '/Users/user/.nvm/versions/node/v10.16.0/bin/node' # default: null
minify: true # default: false
validation_level: skip # default: strict. See https://mjml.io/documentation/#validating-mjml
```
The `node` option is there for those who have problems with `$PATH`, see [#35](https://github.com/notFloran/mjml-bundle/issues/35).

### Custom

First you must create a class which implements `NotFloran\MjmlBundle\Renderer\RendererInterface`, then declare it as a service.
Expand Down
6 changes: 5 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public function getConfigTreeBuilder()
->defaultValue(function () use ($finder) {
return $finder->find('mjml');
})
->info('Path to the binary')
->info('Path to the MJML binary')
->end()
->scalarNode('node')
->defaultNull()
->info('Path to node')
->end()
->scalarNode('service_id')
->info('Service id when renderer is defined to "service"')
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/MjmlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function load(array $configs, ContainerBuilder $container)
$rendererDefinition->addArgument($config['options']['binary']);
$rendererDefinition->addArgument($config['options']['minify']);
$rendererDefinition->addArgument($config['options']['validation_level']);
$rendererDefinition->addArgument($config['options']['node']);
$container->setDefinition($rendererDefinition->getClass(), $rendererDefinition);
$rendererServiceId = $rendererDefinition->getClass();
} elseif ('service' === $config['renderer']) {
Expand Down
40 changes: 25 additions & 15 deletions src/Renderer/BinaryRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,30 @@ final class BinaryRenderer implements RendererInterface
*/
private $mjmlVersion;

public function __construct(string $bin, bool $minify, string $validationLevel)
/**
* @var string|null
*/
private $node;

public function __construct(string $bin, bool $minify, string $validationLevel, string $node = null)
{
$this->bin = $bin;
$this->minify = $minify;
$this->validationLevel = $validationLevel;
$this->node = $node;
}

public function getMjmlVersion(): int
{
if (null === $this->mjmlVersion) {
$process = new Process([
$this->bin,
'--version',
]);
$command = [];
if ($this->node) {
$command[] = $this->node;
}

array_push($command, $this->bin, '--version');

$process = new Process($command);
$process->mustRun();

$this->mjmlVersion = self::VERSION_4;
Expand All @@ -58,30 +68,30 @@ public function render(string $mjmlContent): string
{
$version = $this->getMjmlVersion();

// Tab arguments
$arguments = [
$this->bin,
'-i',
'-s',
];
$command = [];
if ($this->node) {
$command[] = $this->node;
}

array_push($command, $this->bin, '-i', '-s');

$strictArgument = '-l';
if (self::VERSION_4 === $version) {
$strictArgument = '--config.validationLevel';
}

array_push($arguments, $strictArgument, $this->validationLevel);
array_push($command, $strictArgument, $this->validationLevel);

if (true === $this->minify) {
if (self::VERSION_4 === $version) {
array_push($arguments, '--config.minify', 'true');
array_push($command, '--config.minify', 'true');
} else {
$arguments[] = '-m';
$command[] = '-m';
}
}

// Create process
$process = new Process($arguments);
$process = new Process($command);
$process->setInput($mjmlContent);
$process->mustRun();

Expand Down
5 changes: 5 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ protected function getMjmlBinary(): string

return $binary;
}

protected function getNode(): string
{
return 'node';
}
}
8 changes: 8 additions & 0 deletions tests/Renderer/BinaryRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ public function testBinaryNotFound()
$renderer = new BinaryRenderer('mjml-not-found', false, 'strict');
$renderer->render(file_get_contents(__DIR__.'/../fixtures/basic.mjml'));
}

public function testUseNode()
{
$renderer = new BinaryRenderer($this->getMjmlBinary(), false, 'strict', $this->getNode());
$html = $renderer->render(file_get_contents(__DIR__.'/../fixtures/basic.mjml'));

$this->assertContains('html', $html);
}
}

0 comments on commit 9df66a6

Please sign in to comment.