Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
File
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 path
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 publicPath
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 bust
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Assets;
6
7use Celemas\Core\Request;
8use Cosray\Util\Path;
9
10class File
11{
12    public function __construct(
13        protected readonly Request $request,
14        protected readonly Assets $assets,
15        protected readonly string $file,
16    ) {}
17
18    public function path(): string
19    {
20        return Path::inside($this->assets->assetsDir, $this->file);
21    }
22
23    public function publicPath(bool $bust = false): string
24    {
25        $path = implode('/', array_map('rawurlencode', explode('/', str_replace(
26            '\\',
27            '/',
28            $this->path(),
29        ))));
30
31        if ($bust) {
32            $path = $this->bust($path);
33        }
34
35        return substr($path, strlen($this->assets->publicDir));
36    }
37
38    public function url(bool $bust = true): string
39    {
40        return $this->request->origin() . $this->publicPath($bust);
41    }
42
43    protected function bust(string $path): string
44    {
45        $buster = hash('xxh32', (string) filemtime($path));
46
47        return $path . '?v=' . $buster;
48    }
49}