36 lines
1.1 KiB
PHP
36 lines
1.1 KiB
PHP
<?php
|
|
namespace Misuzu;
|
|
|
|
use Index\Templating\Extension\TplExtensionCommon;
|
|
use Twig\TwigFunction;
|
|
use Twig\Extension\LastModifiedExtensionInterface;
|
|
|
|
class AssetInfo implements LastModifiedExtensionInterface {
|
|
use TplExtensionCommon;
|
|
|
|
public const string CURRENT_PATH = Misuzu::PATH_ASSETS . DIRECTORY_SEPARATOR . 'current.json';
|
|
|
|
/** @var array<string, string> */
|
|
private array $assets;
|
|
|
|
/** @param string|array<string, string> $pathOrAssets */
|
|
public function __construct(string|array $pathOrAssets = []) {
|
|
if(is_string($pathOrAssets) && is_file($pathOrAssets)) {
|
|
$data = file_get_contents($pathOrAssets);
|
|
if($data !== false)
|
|
$pathOrAssets = json_decode($data, true);
|
|
}
|
|
|
|
$this->assets = is_array($pathOrAssets) && !array_is_list($pathOrAssets) ? $pathOrAssets : [];
|
|
}
|
|
|
|
public function getAssetUrl(string $name): string {
|
|
return array_key_exists($name, $this->assets) ? $this->assets[$name] : '';
|
|
}
|
|
|
|
public function getFunctions() {
|
|
return [
|
|
new TwigFunction('asset', $this->getAssetUrl(...)),
|
|
];
|
|
}
|
|
}
|