Update publicLink() method for Page

Minified files are included automatically.
This commit is contained in:
Visman 2023-04-20 20:18:08 +07:00
parent 00dd2eea9c
commit 7a3f22f0c3

View file

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