1 | <?php |
||
13 | 1 | class AssetMacro extends MacroSet |
|
14 | { |
||
15 | |||
16 | // Name of the Latte provider for manifest service |
||
17 | public const MANIFEST_PROVIDER = 'assetMacroManifestService'; |
||
18 | |||
19 | |||
20 | 1 | public static function install(Latte\Compiler $compiler): void |
|
25 | |||
26 | |||
27 | 1 | public function macroAsset(Latte\MacroNode $node, Latte\PhpWriter $writer): string |
|
28 | { |
||
29 | 1 | if ($node->modifiers && $node->modifiers !== '|noescape') { |
|
30 | 1 | throw new Latte\CompileException('Only \'noescape\' modifier is allowed in ' . $node->getNotation()); |
|
31 | } |
||
32 | |||
33 | // Validate arguments count |
||
34 | 1 | $args = trim($node->args); |
|
35 | 1 | $argsCount = $args === '' ? 0 : (substr_count($args, ',') + 1); |
|
36 | 1 | if ($argsCount === 0) { |
|
37 | 1 | throw new Latte\CompileException('Asset macro requires at least one argument.'); |
|
38 | 1 | } elseif ($argsCount > 4) { |
|
39 | 1 | throw new Latte\CompileException('Asset macro must have no more than 4 arguments.'); |
|
40 | } |
||
41 | |||
42 | 1 | return $writer->write( |
|
43 | 1 | 'echo ' . ($node->modifiers !== '|noescape' ? '%escape' : '') . |
|
44 | 1 | '(' . self::class . '::getOutput(' . |
|
45 | 1 | '%node.word, ' . |
|
46 | 1 | '%node.array, ' . |
|
47 | 1 | '$baseUrl ?? null, ' . |
|
48 | 1 | '$this->global->' . self::MANIFEST_PROVIDER . ', ' . |
|
49 | 1 | '$this->global->cacheStorage ?? null))'); |
|
50 | } |
||
51 | |||
52 | |||
53 | 1 | public static function getOutput(string $asset, array $args, ?string $baseUrl, ManifestService $manifestService, IStorage $storage = null): string |
|
54 | { |
||
55 | 1 | $config = $manifestService->getConfig(); |
|
56 | |||
57 | // Cache |
||
58 | 1 | $cacheKey = md5(implode(';', [$asset, serialize($args), $config->getHash(), $baseUrl ?? ''])); |
|
59 | 1 | $cache = ($config->isCacheEnabled() && $storage) ? new Cache($storage, 'Webrouse.AssetMacro') : null; |
|
60 | |||
61 | // Load cached value |
||
62 | 1 | if ($cache && ($output = $cache->load($cacheKey)) !== null) { |
|
63 | 1 | return (string) $output; |
|
64 | } |
||
65 | |||
66 | // Generate output and store value to cache |
||
67 | 1 | $output = self::generateOutput($asset, $args, $config, $manifestService); |
|
68 | 1 | if ($cache) { |
|
69 | 1 | $cache->save($cacheKey, $output); |
|
70 | } |
||
71 | |||
72 | 1 | return $output; |
|
73 | } |
||
74 | |||
75 | |||
76 | 1 | public static function generateOutput(string $asset, array $args, Config $config, ManifestService $manifestService): ?string |
|
77 | { |
||
78 | 1 | [$format, $needed, $absolute] = self::processArguments($asset, $args, $config); |
|
79 | 1 | return $manifestService->format($asset, $needed, $format, $absolute); |
|
80 | } |
||
81 | |||
82 | |||
83 | 1 | private static function processArguments(string $asset, array $args, Config $config): array |
|
100 | } |
||
101 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.