Skip to content

Commit

Permalink
Add sidecar method
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed Aug 23, 2023
1 parent a5ca98e commit 0926412
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,37 @@ use Spatie\Mjml\Mjml;
Mjml::new()->canConvertWithoutErrors($mjml); // returns a boolean
```

## Sidecar

This package also supports running through [Sidecar](https://github.com/hammerstonedev/sidecar) in Laravel projects.

To use the `->sidecar()` method, a few extra steps are needed:

Install the Sidecar package:

```shell
composer require spatie/mjml-sidecar
```

Register the `MjmlFunction` in your `sidecar.php` config file.

```php
/*
* All of your function classes that you'd like to deploy go here.
*/
'functions' => [
\Spatie\MjmlSidecar\MjmlFunction::class,
],
```

Deploy the Lambda function by running:

```shell
php artisan sidecar:deploy --activate
```

See the [Sidecar documentation](https://hammerstone.dev/sidecar/docs/main/functions/deploying) for details.

## Testing

```bash
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"laravel/pint": "^1.11",
"spatie/ray": "^1.37.2"
},
"suggest": {
"spatie/mjml-sidecar": "When you want to run MJML compilation through Sidecar in a Laravel project"
},
"autoload": {
"psr-4": {
"Spatie\\Mjml\\": "src"
Expand Down
38 changes: 29 additions & 9 deletions src/Mjml.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Mjml

protected string $workingDirectory;

protected bool $sidecar = false;

public static function new(): self
{
return new static();
Expand Down Expand Up @@ -68,6 +70,13 @@ public function minify(bool $minify = true): self
return $this;
}

public function sidecar(bool $sidecar = true): self
{
$this->sidecar = $sidecar;

return $this;
}

public function validationLevel(ValidationLevel $validationLevel): self
{
$this->validationLevel = $validationLevel;
Expand Down Expand Up @@ -123,18 +132,29 @@ public function convert(string $mjml, array $options = []): MjmlResult
$this->configOptions($options),
];

$process = new Process(
$this->getCommand($arguments),
$this->workingDirectory,
);
if ($this->sidecar) {
if (! class_exists(\Spatie\MjmlSidecar\MjmlFunction::class)) {
throw new CouldNotConvertMjml("You must install the spatie/mjml-sidecar package to convert MJML using Sidecar");
}

$process->run();
$resultString = \Spatie\MjmlSidecar\MjmlFunction::execute([
'mjml' => $arguments[0],
'options' => $arguments[1],
])->body();
} else {
$process = new Process(
$this->getCommand($arguments),
$this->workingDirectory,
);

if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$process->run();

$resultString = $process->getOutput();
if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}

$resultString = $process->getOutput();
}

$resultProperties = json_decode($resultString, true);

Expand Down
6 changes: 6 additions & 0 deletions tests/MjmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
expect(Mjml::new()->canConvertWithoutErrors(mjmlSnippetWithError()))->toBeFalse();
});

it('requires the sidecar package when called with sidecar', function () {
$this->expectException(CouldNotConvertMjml::class);

Mjml::new()->sidecar()->toHtml(mjmlSnippet());
});

function mjmlSnippet(): string
{
return <<<'MJML'
Expand Down

0 comments on commit 0926412

Please sign in to comment.