mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-14 23:47:57 +00:00
Compare commits
9 commits
418f0cb59f
...
2045ce9966
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2045ce9966 | ||
|
|
8fa75c017b | ||
|
|
a14c9c1835 | ||
|
|
c02c413987 | ||
|
|
7715fba4c8 | ||
|
|
7808209963 | ||
|
|
5374ff2e95 | ||
|
|
91a294e1db | ||
|
|
c0f37586f3 |
5 changed files with 282 additions and 12 deletions
|
|
@ -18,6 +18,8 @@ ENV TRMNL_LIQUID_ENABLED=1
|
||||||
# Switch to the root user so we can do root things
|
# Switch to the root user so we can do root things
|
||||||
USER root
|
USER root
|
||||||
|
|
||||||
|
COPY --chown=www-data:www-data --from=bnussbau/trmnl-liquid-cli:0.1.0 /usr/local/bin/trmnl-liquid-cli /usr/local/bin/
|
||||||
|
|
||||||
# Set the working directory
|
# Set the working directory
|
||||||
WORKDIR /var/www/html
|
WORKDIR /var/www/html
|
||||||
|
|
||||||
|
|
@ -51,7 +53,5 @@ FROM base AS production
|
||||||
# Copy the assets from the assets image
|
# Copy the assets from the assets image
|
||||||
COPY --chown=www-data:www-data --from=assets /app/public/build /var/www/html/public/build
|
COPY --chown=www-data:www-data --from=assets /app/public/build /var/www/html/public/build
|
||||||
COPY --chown=www-data:www-data --from=assets /app/node_modules /var/www/html/node_modules
|
COPY --chown=www-data:www-data --from=assets /app/node_modules /var/www/html/node_modules
|
||||||
|
|
||||||
COPY --chown=www-data:www-data --from=bnussbau/trmnl-liquid-cli:latest /usr/local/bin/trmnl-liquid-cli /usr/local/bin/
|
|
||||||
# Drop back to the www-data user
|
# Drop back to the www-data user
|
||||||
USER www-data
|
USER www-data
|
||||||
|
|
|
||||||
|
|
@ -130,9 +130,10 @@ class Plugin extends Model
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split URLs by newline and filter out empty lines
|
// Resolve Liquid variables in the entire polling_url field first, then split by newline
|
||||||
|
$resolvedPollingUrls = $this->resolveLiquidVariables($this->polling_url);
|
||||||
$urls = array_filter(
|
$urls = array_filter(
|
||||||
array_map('trim', explode("\n", $this->polling_url)),
|
array_map('trim', explode("\n", $resolvedPollingUrls)),
|
||||||
fn ($url): bool => ! empty($url)
|
fn ($url): bool => ! empty($url)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -147,8 +148,8 @@ class Plugin extends Model
|
||||||
$httpRequest = $httpRequest->withBody($resolvedBody);
|
$httpRequest = $httpRequest->withBody($resolvedBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve Liquid variables in the polling URL
|
// URL is already resolved, use it directly
|
||||||
$resolvedUrl = $this->resolveLiquidVariables($url);
|
$resolvedUrl = $url;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Make the request based on the verb
|
// Make the request based on the verb
|
||||||
|
|
@ -183,8 +184,8 @@ class Plugin extends Model
|
||||||
$httpRequest = $httpRequest->withBody($resolvedBody);
|
$httpRequest = $httpRequest->withBody($resolvedBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve Liquid variables in the polling URL
|
// URL is already resolved, use it directly
|
||||||
$resolvedUrl = $this->resolveLiquidVariables($url);
|
$resolvedUrl = $url;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Make the request based on the verb
|
// Make the request based on the verb
|
||||||
|
|
@ -344,19 +345,48 @@ class Plugin extends Model
|
||||||
return $template;
|
return $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a template contains a Liquid for loop pattern
|
||||||
|
*
|
||||||
|
* @param string $template The template string to check
|
||||||
|
* @return bool True if the template contains a for loop pattern
|
||||||
|
*/
|
||||||
|
private function containsLiquidForLoop(string $template): bool
|
||||||
|
{
|
||||||
|
return preg_match('/{%-?\s*for\s+/i', $template) === 1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve Liquid variables in a template string using the Liquid template engine
|
* Resolve Liquid variables in a template string using the Liquid template engine
|
||||||
*
|
*
|
||||||
|
* Uses the external trmnl-liquid renderer when:
|
||||||
|
* - preferred_renderer is 'trmnl-liquid'
|
||||||
|
* - External renderer is enabled in config
|
||||||
|
* - Template contains a Liquid for loop pattern
|
||||||
|
*
|
||||||
|
* Otherwise uses the internal PHP-based Liquid renderer.
|
||||||
|
*
|
||||||
* @param string $template The template string containing Liquid variables
|
* @param string $template The template string containing Liquid variables
|
||||||
* @return string The resolved template with variables replaced with their values
|
* @return string The resolved template with variables replaced with their values
|
||||||
*
|
*
|
||||||
* @throws LiquidException
|
* @throws LiquidException
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function resolveLiquidVariables(string $template): string
|
public function resolveLiquidVariables(string $template): string
|
||||||
{
|
{
|
||||||
// Get configuration variables - make them available at root level
|
// Get configuration variables - make them available at root level
|
||||||
$variables = $this->configuration ?? [];
|
$variables = $this->configuration ?? [];
|
||||||
|
|
||||||
|
// Check if external renderer should be used
|
||||||
|
$useExternalRenderer = $this->preferred_renderer === 'trmnl-liquid'
|
||||||
|
&& config('services.trmnl.liquid_enabled')
|
||||||
|
&& $this->containsLiquidForLoop($template);
|
||||||
|
|
||||||
|
if ($useExternalRenderer) {
|
||||||
|
// Use external Ruby liquid renderer
|
||||||
|
return $this->renderWithExternalLiquidRenderer($template, $variables);
|
||||||
|
}
|
||||||
|
|
||||||
// Use the Liquid template engine to resolve variables
|
// Use the Liquid template engine to resolve variables
|
||||||
$environment = App::make('liquid.environment');
|
$environment = App::make('liquid.environment');
|
||||||
$environment->filterRegistry->register(StandardFilters::class);
|
$environment->filterRegistry->register(StandardFilters::class);
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,7 @@ return [
|
||||||
'force_https' => env('FORCE_HTTPS', false),
|
'force_https' => env('FORCE_HTTPS', false),
|
||||||
'puppeteer_docker' => env('PUPPETEER_DOCKER', false),
|
'puppeteer_docker' => env('PUPPETEER_DOCKER', false),
|
||||||
'puppeteer_mode' => env('PUPPETEER_MODE', 'local'),
|
'puppeteer_mode' => env('PUPPETEER_MODE', 'local'),
|
||||||
'puppeteer_wait_for_network_idle' => env('PUPPETEER_WAIT_FOR_NETWORK_IDLE', false),
|
'puppeteer_wait_for_network_idle' => env('PUPPETEER_WAIT_FOR_NETWORK_IDLE', true),
|
||||||
'puppeteer_window_size_strategy' => env('PUPPETEER_WINDOW_SIZE_STRATEGY', null),
|
'puppeteer_window_size_strategy' => env('PUPPETEER_WINDOW_SIZE_STRATEGY', null),
|
||||||
|
|
||||||
'notifications' => [
|
'notifications' => [
|
||||||
|
|
|
||||||
|
|
@ -296,6 +296,16 @@ new class extends Component {
|
||||||
<flux:heading size="lg">Import from TRMNL Recipe Catalog
|
<flux:heading size="lg">Import from TRMNL Recipe Catalog
|
||||||
<flux:badge color="yellow" class="ml-2">Alpha</flux:badge>
|
<flux:badge color="yellow" class="ml-2">Alpha</flux:badge>
|
||||||
</flux:heading>
|
</flux:heading>
|
||||||
|
<flux:callout class="mb-4 mt-4" color="yellow">
|
||||||
|
<flux:heading size="sm">Limitations</flux:heading>
|
||||||
|
<ul class="list-disc pl-5 mt-2">
|
||||||
|
<li><flux:text>Only full view will be imported; shared markup will be prepended</flux:text></li>
|
||||||
|
<li><flux:text>Requires <span class="font-mono">trmnl-liquid-cli</span>. (Included in Docker container)</flux:text></li>
|
||||||
|
<li><flux:text>API responses in formats other than <span class="font-mono">JSON</span> are not yet fully supported.</flux:text></li>
|
||||||
|
<li><flux:text>There are limitations in payload size (Data Payload, Template).</flux:text></li>
|
||||||
|
</ul>
|
||||||
|
<flux:text class="mt-1">Please report issues, aside from the known limitations, on <a href="https://github.com/usetrmnl/byos_laravel/issues/new" target="_blank" class="underline">GitHub</a>. Include the recipe URL.</flux:text></li>
|
||||||
|
</flux:callout>
|
||||||
</div>
|
</div>
|
||||||
<livewire:catalog.trmnl lazy />
|
<livewire:catalog.trmnl lazy />
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -357,3 +357,233 @@ test('resolveLiquidVariables handles empty configuration', function (): void {
|
||||||
|
|
||||||
expect($plugin->resolveLiquidVariables($template))->toBe($expected);
|
expect($plugin->resolveLiquidVariables($template))->toBe($expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('resolveLiquidVariables uses external renderer when preferred_renderer is trmnl-liquid and template contains for loop', function (): void {
|
||||||
|
Illuminate\Support\Facades\Process::fake([
|
||||||
|
'*' => Illuminate\Support\Facades\Process::result(
|
||||||
|
output: 'https://api1.example.com/data\nhttps://api2.example.com/data',
|
||||||
|
exitCode: 0
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
config(['services.trmnl.liquid_enabled' => true]);
|
||||||
|
config(['services.trmnl.liquid_path' => '/usr/local/bin/trmnl-liquid-cli']);
|
||||||
|
|
||||||
|
$plugin = Plugin::factory()->create([
|
||||||
|
'preferred_renderer' => 'trmnl-liquid',
|
||||||
|
'configuration' => [
|
||||||
|
'recipe_ids' => '1,2',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$template = <<<'LIQUID'
|
||||||
|
{% assign ids = recipe_ids | split: "," %}
|
||||||
|
{% for id in ids %}
|
||||||
|
https://api{{ id }}.example.com/data
|
||||||
|
{% endfor %}
|
||||||
|
LIQUID;
|
||||||
|
|
||||||
|
$result = $plugin->resolveLiquidVariables($template);
|
||||||
|
|
||||||
|
// Trim trailing newlines that may be added by the process
|
||||||
|
expect(mb_trim($result))->toBe('https://api1.example.com/data\nhttps://api2.example.com/data');
|
||||||
|
|
||||||
|
Illuminate\Support\Facades\Process::assertRan(function ($process): bool {
|
||||||
|
$command = is_array($process->command) ? implode(' ', $process->command) : $process->command;
|
||||||
|
|
||||||
|
return str_contains($command, 'trmnl-liquid-cli') &&
|
||||||
|
str_contains($command, '--template') &&
|
||||||
|
str_contains($command, '--context');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resolveLiquidVariables uses internal renderer when preferred_renderer is not trmnl-liquid', function (): void {
|
||||||
|
$plugin = Plugin::factory()->create([
|
||||||
|
'preferred_renderer' => 'php',
|
||||||
|
'configuration' => [
|
||||||
|
'recipe_ids' => '1,2',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$template = <<<'LIQUID'
|
||||||
|
{% assign ids = recipe_ids | split: "," %}
|
||||||
|
{% for id in ids %}
|
||||||
|
https://api{{ id }}.example.com/data
|
||||||
|
{% endfor %}
|
||||||
|
LIQUID;
|
||||||
|
|
||||||
|
// Should use internal renderer even with for loop
|
||||||
|
$result = $plugin->resolveLiquidVariables($template);
|
||||||
|
|
||||||
|
// Internal renderer should process the template
|
||||||
|
expect($result)->toBeString();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resolveLiquidVariables uses internal renderer when external renderer is disabled', function (): void {
|
||||||
|
config(['services.trmnl.liquid_enabled' => false]);
|
||||||
|
|
||||||
|
$plugin = Plugin::factory()->create([
|
||||||
|
'preferred_renderer' => 'trmnl-liquid',
|
||||||
|
'configuration' => [
|
||||||
|
'recipe_ids' => '1,2',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$template = <<<'LIQUID'
|
||||||
|
{% assign ids = recipe_ids | split: "," %}
|
||||||
|
{% for id in ids %}
|
||||||
|
https://api{{ id }}.example.com/data
|
||||||
|
{% endfor %}
|
||||||
|
LIQUID;
|
||||||
|
|
||||||
|
// Should use internal renderer when external is disabled
|
||||||
|
$result = $plugin->resolveLiquidVariables($template);
|
||||||
|
|
||||||
|
expect($result)->toBeString();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resolveLiquidVariables uses internal renderer when template does not contain for loop', function (): void {
|
||||||
|
config(['services.trmnl.liquid_enabled' => true]);
|
||||||
|
config(['services.trmnl.liquid_path' => '/usr/local/bin/trmnl-liquid-cli']);
|
||||||
|
|
||||||
|
$plugin = Plugin::factory()->create([
|
||||||
|
'preferred_renderer' => 'trmnl-liquid',
|
||||||
|
'configuration' => [
|
||||||
|
'api_key' => 'test123',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$template = 'https://api.example.com/data?key={{ api_key }}';
|
||||||
|
|
||||||
|
// Should use internal renderer when no for loop
|
||||||
|
$result = $plugin->resolveLiquidVariables($template);
|
||||||
|
|
||||||
|
expect($result)->toBe('https://api.example.com/data?key=test123');
|
||||||
|
|
||||||
|
Illuminate\Support\Facades\Process::assertNothingRan();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resolveLiquidVariables detects for loop with standard opening tag', function (): void {
|
||||||
|
Illuminate\Support\Facades\Process::fake([
|
||||||
|
'*' => Illuminate\Support\Facades\Process::result(
|
||||||
|
output: 'resolved',
|
||||||
|
exitCode: 0
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
config(['services.trmnl.liquid_enabled' => true]);
|
||||||
|
config(['services.trmnl.liquid_path' => '/usr/local/bin/trmnl-liquid-cli']);
|
||||||
|
|
||||||
|
$plugin = Plugin::factory()->create([
|
||||||
|
'preferred_renderer' => 'trmnl-liquid',
|
||||||
|
'configuration' => [],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Test {% for pattern
|
||||||
|
$template = '{% for item in items %}test{% endfor %}';
|
||||||
|
$plugin->resolveLiquidVariables($template);
|
||||||
|
|
||||||
|
Illuminate\Support\Facades\Process::assertRan(function ($process): bool {
|
||||||
|
$command = is_array($process->command) ? implode(' ', $process->command) : (string) $process->command;
|
||||||
|
|
||||||
|
return str_contains($command, 'trmnl-liquid-cli');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resolveLiquidVariables detects for loop with whitespace stripping tag', function (): void {
|
||||||
|
Illuminate\Support\Facades\Process::fake([
|
||||||
|
'*' => Illuminate\Support\Facades\Process::result(
|
||||||
|
output: 'resolved',
|
||||||
|
exitCode: 0
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
config(['services.trmnl.liquid_enabled' => true]);
|
||||||
|
config(['services.trmnl.liquid_path' => '/usr/local/bin/trmnl-liquid-cli']);
|
||||||
|
|
||||||
|
$plugin = Plugin::factory()->create([
|
||||||
|
'preferred_renderer' => 'trmnl-liquid',
|
||||||
|
'configuration' => [],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Test {%- for pattern (with whitespace stripping)
|
||||||
|
$template = '{%- for item in items %}test{% endfor %}';
|
||||||
|
$plugin->resolveLiquidVariables($template);
|
||||||
|
|
||||||
|
Illuminate\Support\Facades\Process::assertRan(function ($process): bool {
|
||||||
|
$command = is_array($process->command) ? implode(' ', $process->command) : (string) $process->command;
|
||||||
|
|
||||||
|
return str_contains($command, 'trmnl-liquid-cli');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('updateDataPayload resolves entire polling_url field first then splits by newline', function (): void {
|
||||||
|
Http::fake([
|
||||||
|
'https://api1.example.com/data' => Http::response(['data' => 'test1'], 200),
|
||||||
|
'https://api2.example.com/data' => Http::response(['data' => 'test2'], 200),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$plugin = Plugin::factory()->create([
|
||||||
|
'data_strategy' => 'polling',
|
||||||
|
'polling_url' => "https://api1.example.com/data\nhttps://api2.example.com/data",
|
||||||
|
'polling_verb' => 'get',
|
||||||
|
'configuration' => [
|
||||||
|
'recipe_ids' => '1,2',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$plugin->updateDataPayload();
|
||||||
|
|
||||||
|
// Should have split the multi-line URL and generated two requests
|
||||||
|
expect($plugin->data_payload)->toHaveKey('IDX_0');
|
||||||
|
expect($plugin->data_payload)->toHaveKey('IDX_1');
|
||||||
|
expect($plugin->data_payload['IDX_0'])->toBe(['data' => 'test1']);
|
||||||
|
expect($plugin->data_payload['IDX_1'])->toBe(['data' => 'test2']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('updateDataPayload handles multi-line polling_url with for loop using external renderer', function (): void {
|
||||||
|
Illuminate\Support\Facades\Process::fake([
|
||||||
|
'*' => Illuminate\Support\Facades\Process::result(
|
||||||
|
output: "https://api1.example.com/data\nhttps://api2.example.com/data",
|
||||||
|
exitCode: 0
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
Http::fake([
|
||||||
|
'https://api1.example.com/data' => Http::response(['data' => 'test1'], 200),
|
||||||
|
'https://api2.example.com/data' => Http::response(['data' => 'test2'], 200),
|
||||||
|
]);
|
||||||
|
|
||||||
|
config(['services.trmnl.liquid_enabled' => true]);
|
||||||
|
config(['services.trmnl.liquid_path' => '/usr/local/bin/trmnl-liquid-cli']);
|
||||||
|
|
||||||
|
$plugin = Plugin::factory()->create([
|
||||||
|
'data_strategy' => 'polling',
|
||||||
|
'preferred_renderer' => 'trmnl-liquid',
|
||||||
|
'polling_url' => <<<'LIQUID'
|
||||||
|
{% assign ids = recipe_ids | split: "," %}
|
||||||
|
{% for id in ids %}
|
||||||
|
https://api{{ id }}.example.com/data
|
||||||
|
{% endfor %}
|
||||||
|
LIQUID
|
||||||
|
,
|
||||||
|
'polling_verb' => 'get',
|
||||||
|
'configuration' => [
|
||||||
|
'recipe_ids' => '1,2',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$plugin->updateDataPayload();
|
||||||
|
|
||||||
|
// Should have used external renderer and generated two URLs
|
||||||
|
expect($plugin->data_payload)->toHaveKey('IDX_0');
|
||||||
|
expect($plugin->data_payload)->toHaveKey('IDX_1');
|
||||||
|
expect($plugin->data_payload['IDX_0'])->toBe(['data' => 'test1']);
|
||||||
|
expect($plugin->data_payload['IDX_1'])->toBe(['data' => 'test2']);
|
||||||
|
|
||||||
|
Illuminate\Support\Facades\Process::assertRan(function ($process): bool {
|
||||||
|
$command = is_array($process->command) ? implode(' ', $process->command) : (string) $process->command;
|
||||||
|
|
||||||
|
return str_contains($command, 'trmnl-liquid-cli');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue