1 | <?php |
||
16 | 1 | class AssetMacro extends MacroSet |
|
17 | { |
||
18 | |||
19 | const CONFIG_PROVIDER = 'assetMacroConfig'; |
||
20 | |||
21 | private static $manifestCache = []; |
||
22 | |||
23 | /** |
||
24 | * @param Latte\Compiler $compiler |
||
25 | */ |
||
26 | 1 | public static function install(Latte\Compiler $compiler) |
|
27 | { |
||
28 | 1 | $me = new self($compiler); |
|
29 | 1 | $me->addMacro('asset', [$me, 'macroAsset']); |
|
30 | 1 | } |
|
31 | |||
32 | |||
33 | /** |
||
34 | * @param Latte\MacroNode $node |
||
35 | * @param Latte\PhpWriter $writer |
||
36 | * @return string |
||
37 | * @throws Latte\CompileException |
||
38 | */ |
||
39 | 1 | public function macroAsset(Latte\MacroNode $node, Latte\PhpWriter $writer) |
|
40 | { |
||
41 | 1 | $args = trim($node->args); |
|
42 | |||
43 | // Validate arguments count |
||
44 | 1 | $argsCount = $args === '' ? 0 : (substr_count($args, ',') + 1); |
|
45 | 1 | if ($argsCount === 0) { |
|
46 | 1 | throw new Latte\CompileException("Asset macro requires at least one argument."); |
|
47 | } |
||
48 | 1 | if ($argsCount > 3) { |
|
49 | 1 | throw new Latte\CompileException("Asset macro must have no more than 3 arguments."); |
|
50 | } |
||
51 | |||
52 | 1 | return $writer->write( |
|
53 | 'echo %escape(' . self::class . '::resolveAssetPath(' . |
||
54 | 1 | '%node.word, %node.array, $basePath, $this->global->' . self::CONFIG_PROVIDER . '))'); |
|
55 | } |
||
56 | |||
57 | |||
58 | /** |
||
59 | * @param string $asset Asset relative path |
||
60 | * @param array $args Other macro arguments |
||
61 | * @param string $basePath Base path |
||
62 | * @param array $config Macro configuration |
||
63 | * @return string |
||
64 | */ |
||
65 | 1 | public static function resolveAssetPath($asset, array $args, $basePath, array $config) |
|
66 | { |
||
67 | 1 | list($path, $format, $need) = self::processArguments($asset, $args); |
|
68 | 1 | $wwwDir = Utils::normalizePath($config['wwwDir']); |
|
69 | 1 | $manifest = self::resolveManifest($path, $need, $wwwDir, $config); |
|
70 | 1 | $revision = $manifest === NULL ? NULL : self::resolveRevision($manifest, $path, $need, $config); |
|
71 | |||
72 | // Is revision only version (query parameter) or full path to asset? |
||
73 | 1 | $revisionIsVersion = $revision === NULL || ! Strings::match($revision, '/[.\/]/'); |
|
74 | |||
75 | // Check if asset exists |
||
76 | 1 | $ds = DIRECTORY_SEPARATOR; |
|
77 | 1 | $filePath = $revisionIsVersion ? |
|
78 | 1 | ($wwwDir . $ds . $path) : |
|
79 | 1 | ($wwwDir . $ds . Utils::normalizePath($revision)); |
|
80 | 1 | if ( ! file_exists($filePath)) { |
|
81 | 1 | Utils::throwError( |
|
82 | 1 | new AssetNotFoundException(sprintf("Asset '%s' not found.", $filePath)), |
|
83 | 1 | $config['missingAsset'], |
|
84 | 1 | $need |
|
85 | ); |
||
86 | 1 | return ''; |
|
87 | } |
||
88 | |||
89 | // Format output |
||
90 | 1 | return self::formatOutput($format, $basePath, $path, $revision, $revisionIsVersion); |
|
91 | } |
||
92 | |||
93 | |||
94 | /** |
||
95 | * @param string $format Output format |
||
96 | * @param string $basePath Base path |
||
97 | * @param string $path Asset relative path |
||
98 | * @param string|null $revision Asset revision (version or path to file) |
||
99 | * @param bool $revisionIsVersion Is revision only version or full path? |
||
100 | * @return string |
||
101 | */ |
||
102 | private static function formatOutput($format, $basePath, $path, $revision, $revisionIsVersion) |
||
132 | |||
133 | |||
134 | /** |
||
135 | * @param string $asset Asset path specified in macro |
||
136 | * @param array $args Macro arguments |
||
137 | * @return array |
||
138 | */ |
||
139 | 1 | private static function processArguments($asset, array $args) |
|
152 | |||
153 | |||
154 | /** |
||
155 | * @param string $asset Asset path specified in macro |
||
156 | * @param bool $need Fail if manifest doesn't exist? |
||
157 | * @param string $wwwDir Public www dir |
||
158 | * @param array $config Macro configuration |
||
159 | * @return null|array |
||
160 | */ |
||
161 | 1 | private static function resolveManifest($asset, $need, $wwwDir, array $config) |
|
186 | |||
187 | |||
188 | /** |
||
189 | * @param string $asset Asset path specified in macro |
||
190 | * @param string $wwwDir Public www dir |
||
191 | * @param bool $need Fail if asset/manifest doesn't exist? |
||
192 | * @param array $config Macro configuration |
||
193 | * @return null|array |
||
194 | */ |
||
195 | 1 | private static function autodetectManifest($asset, $wwwDir, $need, array $config) |
|
220 | |||
221 | /** |
||
222 | * Get manifest content and cache it |
||
223 | * @param string $path |
||
224 | * @return array |
||
225 | */ |
||
226 | private static function getManifest($path) { |
||
232 | |||
233 | |||
234 | /** |
||
235 | * @param null|array $manifest Array of revisions |
||
236 | * @param string $path Asset path |
||
237 | * @param bool $need Fail if revision doesn't exist? |
||
238 | * @param array $config Macro configuration |
||
239 | * @return null|string |
||
240 | */ |
||
241 | 1 | private static function resolveRevision($manifest, $path, $need, array $config) |
|
255 | |||
256 | } |
||
257 |