Skip to content

Commit

Permalink
Cache: include $baseUrl in cache key (server can be accessed from mul…
Browse files Browse the repository at this point in the history
…tiple URLs)
  • Loading branch information
michaljurecko committed Jan 16, 2019
1 parent cafd235 commit d6fb24f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Webrouse/AssetMacro/AssetMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@ public function macroAsset(Latte\MacroNode $node, Latte\PhpWriter $writer): stri
'(' . self::class . '::getOutput(' .
'%node.word, ' .
'%node.array, ' .
'$baseUrl ?? null, ' .
'$this->global->' . self::MANIFEST_PROVIDER . ', ' .
'$this->global->cacheStorage ?? null))');
}


public static function getOutput(string $asset, array $args, ManifestService $manifestService, IStorage $storage = null): string
public static function getOutput(string $asset, array $args, ?string $baseUrl, ManifestService $manifestService, IStorage $storage = null): string
{
$config = $manifestService->getConfig();

// Cache
$cacheKey = md5(implode(';', [$asset, serialize($args), $config->getHash()]));
$cacheKey = md5(implode(';', [$asset, serialize($args), $config->getHash(), $baseUrl ?? '']));
$cache = ($config->isCacheEnabled() && $storage) ? new Cache($storage, 'Webrouse.AssetMacro') : null;

// Load cached value
Expand Down
4 changes: 2 additions & 2 deletions tests/AssetMacroTests/CompileTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class CompileTest extends TestCase
// main.js
$template1 = '{asset "assets/compiled/main.js"}';
Assert::contains(
AssetMacro::class . '::getOutput("assets/compiled/main.js", [], $this->global->' . AssetMacro::MANIFEST_PROVIDER . ', $this->global->cacheStorage ?? null)',
AssetMacro::class . '::getOutput("assets/compiled/main.js", [], $baseUrl ?? null, $this->global->' . AssetMacro::MANIFEST_PROVIDER . ', $this->global->cacheStorage ?? null)',
$latte->compile($template1)
);

// assets/compiled/main.css
$template2 = '{asset "assets/compiled/main.css"}';
Assert::contains(
AssetMacro::class . '::getOutput("assets/compiled/main.css", [], $this->global->' . AssetMacro::MANIFEST_PROVIDER . ', $this->global->cacheStorage ?? null)',
AssetMacro::class . '::getOutput("assets/compiled/main.css", [], $baseUrl ?? null, $this->global->' . AssetMacro::MANIFEST_PROVIDER . ', $this->global->cacheStorage ?? null)',
$latte->compile($template2)
);
}
Expand Down

0 comments on commit d6fb24f

Please sign in to comment.