AssetMacro::install()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Webrouse\AssetMacro;
5
6
use Latte;
7
use Latte\Macros\MacroSet;
8
use Nette\Caching\Cache;
9
use Nette\Caching\IStorage;
10
use Nette\Utils\Strings;
11
use Nette\Utils\Validators;
12
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
21
	{
22 1
		$me = new self($compiler);
23 1
		$me->addMacro('asset', [$me, 'macroAsset']);
24 1
	}
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);
0 ignored issues
show
Bug introduced by
The variable $format does not exist. Did you forget to declare it?

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.

Loading history...
Bug introduced by
The variable $needed does not exist. Did you forget to declare it?

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.

Loading history...
Bug introduced by
The variable $absolute does not exist. Did you forget to declare it?

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.

Loading history...
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
84
	{
85 1
		$format = $args['format'] ?? ($args[0] ?? $config->getDefaultFormat());
86 1
		$needed = $args['need'] ?? ($args[1] ?? true);
87 1
		$absolute = $args['absolute'] ?? ($args[2] ?? false);
88
89 1
		Validators::assert($asset, 'string', 'path');
90 1
		Validators::assert($format, 'string', 'format');
91 1
		Validators::assert($needed, 'bool', 'need');
92 1
		Validators::assert($needed, 'bool', 'absolute');
93
94 1
		if (Strings::startsWith($asset, '//')) {
95 1
			$absolute = true;
96
		}
97
98 1
		return [$format, $needed, $absolute];
99
	}
100
}
101