Browse Source

Update publicLink() method for Page

Minified files are included automatically.
Visman 2 năm trước cách đây
mục cha
commit
7a3f22f0c3
1 tập tin đã thay đổi với 22 bổ sung8 xóa
  1. 22 8
      app/Models/Page.php

+ 22 - 8
app/Models/Page.php

@@ -514,22 +514,36 @@ abstract class Page extends Model
 
     /**
      * Возвращает url для $path заданного в каталоге public
+     * Если для файла name.ext есть файл name.min.ext, то url возвращается для него
      * Ведущий слеш обязателен O_o
      */
     public function publicLink(string $path, bool $returnEmpty = false): string
     {
-        $fullPath = $this->c->DIR_PUBLIC . $path;
+        if (\preg_match('%^(.+)(\.[^.\\/]++)$%D', $path, $matches)) {
+            $variants = [
+                $matches[1] . '.min' => $matches[2],
+                $matches[1]          => $matches[2],
+            ];
+        } else {
+            $variants = [
+                $path                => '',
+            ];
+        }
+
+        foreach ($variants as $start => $end) {
+            $fullPath = $this->c->DIR_PUBLIC . $start . $end;
 
-        if (\is_file($fullPath)) {
-            $time = \filemtime($fullPath) ?: '0';
+            if (\is_file($fullPath)) {
+                if ('' === $end) {
+                    return $this->c->PUBLIC_URL . $start;
+                } else {
+                    $time = \filemtime($fullPath) ?: '0';
 
-            if (\preg_match('%^(.+)\.([^.\\/]++)$%D', $path, $matches)) {
-                return $this->c->PUBLIC_URL . "{$matches[1]}.v.{$time}.{$matches[2]}";
+                    return $this->c->PUBLIC_URL . $start . '.v.' . $time . $end;
+                }
             }
-        } elseif ($returnEmpty) {
-            return '';
         }
 
-        return $this->c->PUBLIC_URL . $path;
+        return $returnEmpty ? '' : $this->c->PUBLIC_URL . $path;
     }
 }