v1.0.0.0
StocketBase
- <?php
-
- declare(strict_types=1);
-
- final class Asset
- {
- private static array $cache = [];
-
- public static function url(string $relativePath): string
- {
- $relativePath = ltrim($relativePath, '/');
-
- if (isset(self::$cache[$relativePath])) {
- return self::$cache[$relativePath];
- }
-
- $absolutePath = dirname(__DIR__) . '/' . $relativePath;
- $url = class_exists('Languages', false) && Languages::current() !== '' ? Languages::basePath() . '/' . $relativePath : $relativePath;
-
- if (is_file($absolutePath)) {
- $modifiedTime = filemtime($absolutePath);
- if ($modifiedTime !== false) {
- $url .= '?v=' . $modifiedTime;
- }
- }
-
- self::$cache[$relativePath] = $url;
-
- return $url;
- }
-
- public static function css(string $relativePath): string
- {
- return '<link rel="stylesheet" href="' . htmlspecialchars(self::url($relativePath)) . '">';
- }
-
- public static function js(string $relativePath): string
- {
- return '<script src="' . htmlspecialchars(self::url($relativePath)) . '"></script>';
- }
-
- public static function favicon(): string
- {
- return '<link rel="icon" type="image/x-icon" href="' . htmlspecialchars(self::url('favicon.ico')) . '">';
- }
- }
-