WebOrbiton
v1.0.0.0

StocketBase

47 lines · 1.3 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. final class Asset
  6. {
  7. private static array $cache = [];
  8. ​
  9. public static function url(string $relativePath): string
  10. {
  11. $relativePath = ltrim($relativePath, '/');
  12. ​
  13. if (isset(self::$cache[$relativePath])) {
  14. return self::$cache[$relativePath];
  15. }
  16. ​
  17. $absolutePath = dirname(__DIR__) . '/' . $relativePath;
  18. $url = class_exists('Languages', false) && Languages::current() !== '' ? Languages::basePath() . '/' . $relativePath : $relativePath;
  19. ​
  20. if (is_file($absolutePath)) {
  21. $modifiedTime = filemtime($absolutePath);
  22. if ($modifiedTime !== false) {
  23. $url .= '?v=' . $modifiedTime;
  24. }
  25. }
  26. ​
  27. self::$cache[$relativePath] = $url;
  28. ​
  29. return $url;
  30. }
  31. ​
  32. public static function css(string $relativePath): string
  33. {
  34. return '<link rel="stylesheet" href="' . htmlspecialchars(self::url($relativePath)) . '">';
  35. }
  36. ​
  37. public static function js(string $relativePath): string
  38. {
  39. return '<script src="' . htmlspecialchars(self::url($relativePath)) . '"></script>';
  40. }
  41. ​
  42. public static function favicon(): string
  43. {
  44. return '<link rel="icon" type="image/x-icon" href="' . htmlspecialchars(self::url('favicon.ico')) . '">';
  45. }
  46. }
  47. ​