From 9df66a63c0bcaee60804e69dbc712796c4b0566c Mon Sep 17 00:00:00 2001 From: Floran Brutel Date: Sun, 17 Jan 2021 18:01:17 +0100 Subject: [PATCH] Add option to set path to node --- CHANGELOG.md | 4 +++ README.md | 3 ++ src/DependencyInjection/Configuration.php | 6 +++- src/DependencyInjection/MjmlExtension.php | 1 + src/Renderer/BinaryRenderer.php | 40 ++++++++++++++--------- tests/AbstractTestCase.php | 5 +++ tests/Renderer/BinaryRendererTest.php | 8 +++++ 7 files changed, 51 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a482d65..ca521a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 6b8153b..6d075c2 100755 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index ce8a2e7..b8dfe95 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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"') diff --git a/src/DependencyInjection/MjmlExtension.php b/src/DependencyInjection/MjmlExtension.php index 1ce49ad..850d428 100644 --- a/src/DependencyInjection/MjmlExtension.php +++ b/src/DependencyInjection/MjmlExtension.php @@ -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']) { diff --git a/src/Renderer/BinaryRenderer.php b/src/Renderer/BinaryRenderer.php index a5e7d50..733edd9 100644 --- a/src/Renderer/BinaryRenderer.php +++ b/src/Renderer/BinaryRenderer.php @@ -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; @@ -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(); diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 7911b07..305a4fd 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -16,4 +16,9 @@ protected function getMjmlBinary(): string return $binary; } + + protected function getNode(): string + { + return 'node'; + } } diff --git a/tests/Renderer/BinaryRendererTest.php b/tests/Renderer/BinaryRendererTest.php index 0f9f237..58f078a 100644 --- a/tests/Renderer/BinaryRendererTest.php +++ b/tests/Renderer/BinaryRendererTest.php @@ -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); + } }