arubacao /
asset-cdn
| 1 | <?php |
||
| 2 | |||
| 3 | use Illuminate\Support\HtmlString; |
||
| 4 | use Illuminate\Support\Str; |
||
| 5 | |||
| 6 | if (! function_exists('mix_cdn')) { |
||
| 7 | /** |
||
| 8 | * Get the path to a versioned Mix file. |
||
| 9 | * |
||
| 10 | * @param string $path |
||
| 11 | * @param string $manifestDirectory |
||
| 12 | * @return \Illuminate\Support\HtmlString |
||
| 13 | * |
||
| 14 | * @throws \Exception |
||
| 15 | */ |
||
| 16 | function mix_cdn($path, $manifestDirectory = '') |
||
| 17 | { |
||
| 18 | if (! config('asset-cdn.use_cdn')) { |
||
| 19 | return mix($path, $manifestDirectory); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 20 | } |
||
| 21 | |||
| 22 | static $manifests = []; |
||
| 23 | |||
| 24 | if (! Str::startsWith($path, '/')) { |
||
| 25 | $path = "/{$path}"; |
||
| 26 | } |
||
| 27 | |||
| 28 | if ($manifestDirectory && ! Str::startsWith($manifestDirectory, '/')) { |
||
| 29 | $manifestDirectory = "/{$manifestDirectory}"; |
||
| 30 | } |
||
| 31 | |||
| 32 | $manifestPath = public_path($manifestDirectory.'/mix-manifest.json'); |
||
| 33 | |||
| 34 | if (! isset($manifests[$manifestPath])) { |
||
| 35 | if (! file_exists($manifestPath)) { |
||
| 36 | throw new Exception('The Mix manifest does not exist.'); |
||
| 37 | } |
||
| 38 | |||
| 39 | $manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true); |
||
| 40 | } |
||
| 41 | |||
| 42 | $manifest = $manifests[$manifestPath]; |
||
| 43 | |||
| 44 | if (! isset($manifest[$path])) { |
||
| 45 | throw new Exception("Unable to locate Mix file: {$path}."); |
||
| 46 | } |
||
| 47 | |||
| 48 | $cdnUrl = config('asset-cdn.cdn_url'); |
||
| 49 | // Remove slashes from ending of the path |
||
| 50 | $cdnUrl = rtrim($cdnUrl, '/'); |
||
| 51 | |||
| 52 | return new HtmlString($cdnUrl.$manifestDirectory.$manifest[$path]); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | if (! function_exists('asset_cdn')) { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Generate an asset path for the application. |
||
| 60 | * |
||
| 61 | * @param string $path |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | function asset_cdn($path) |
||
| 65 | { |
||
| 66 | if (! config('asset-cdn.use_cdn')) { |
||
| 67 | return asset($path); |
||
| 68 | } |
||
| 69 | |||
| 70 | $cdnUrl = config('asset-cdn.cdn_url'); |
||
| 71 | // Remove slashes from ending of the path |
||
| 72 | $cdnUrl = rtrim($cdnUrl, '/'); |
||
| 73 | |||
| 74 | return $cdnUrl.'/'.trim($path, '/'); |
||
| 75 | } |
||
| 76 | } |
||
| 77 |