Path: blob/1.0-develop/app/Services/Helpers/AssetHashService.php
10280 views
<?php12namespace Pterodactyl\Services\Helpers;34use Illuminate\Support\Arr;5use Illuminate\Filesystem\FilesystemManager;6use Illuminate\Contracts\Filesystem\Filesystem;7use Pterodactyl\Exceptions\ManifestDoesNotExistException;89class AssetHashService10{11/**12* Location of the manifest file generated by gulp.13*/14public const MANIFEST_PATH = './assets/manifest.json';1516private Filesystem $filesystem;1718protected static mixed $manifest = null;1920/**21* AssetHashService constructor.22*/23public function __construct(FilesystemManager $filesystem)24{25$this->filesystem = $filesystem->createLocalDriver(['root' => public_path()]);26}2728/**29* Modify a URL to append the asset hash.30*/31public function url(string $resource): string32{33$file = last(explode('/', $resource));34$data = Arr::get($this->manifest(), $file) ?? $file;3536return str_replace($file, Arr::get($data, 'src') ?? $file, $resource);37}3839/**40* Return the data integrity hash for a resource.41*/42public function integrity(string $resource): string43{44$file = last(explode('/', $resource));45$data = array_get($this->manifest(), $file, $file);4647return Arr::get($data, 'integrity') ?? '';48}4950/**51* Return a built CSS import using the provided URL.52*/53public function css(string $resource): string54{55$attributes = [56'href' => $this->url($resource),57'rel' => 'stylesheet preload',58'as' => 'style',59'crossorigin' => 'anonymous',60'referrerpolicy' => 'no-referrer',61];6263if (config('pterodactyl.assets.use_hash')) {64$attributes['integrity'] = $this->integrity($resource);65}6667$output = '<link';68foreach ($attributes as $key => $value) {69$output .= " $key=\"$value\"";70}7172return $output . '>';73}7475/**76* Return a built JS import using the provided URL.77*/78public function js(string $resource): string79{80$attributes = [81'src' => $this->url($resource),82'crossorigin' => 'anonymous',83];8485if (config('pterodactyl.assets.use_hash')) {86$attributes['integrity'] = $this->integrity($resource);87}8889$output = '<script';90foreach ($attributes as $key => $value) {91$output .= " $key=\"$value\"";92}9394return $output . '></script>';95}9697/**98* Get the asset manifest and store it in the cache for quicker lookups.99*/100protected function manifest(): array101{102if (static::$manifest === null) {103self::$manifest = json_decode(104$this->filesystem->get(self::MANIFEST_PATH),105true106);107}108109$manifest = static::$manifest;110if ($manifest === null) {111throw new ManifestDoesNotExistException();112}113114return $manifest;115}116}117118119