2018-03-27 05:44:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Helpers;
|
|
|
|
|
2019-12-22 23:41:38 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2018-03-27 05:44:14 +00:00
|
|
|
use Illuminate\Filesystem\FilesystemManager;
|
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
|
|
|
|
|
|
class AssetHashService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Location of the manifest file generated by gulp.
|
|
|
|
*/
|
|
|
|
public const MANIFEST_PATH = './assets/manifest.json';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Filesystem\Filesystem
|
|
|
|
*/
|
|
|
|
private $filesystem;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Foundation\Application
|
|
|
|
*/
|
|
|
|
private $application;
|
|
|
|
|
|
|
|
/**
|
2020-06-28 22:43:44 +00:00
|
|
|
* @var array|null
|
2018-03-27 05:44:14 +00:00
|
|
|
*/
|
|
|
|
protected static $manifest;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AssetHashService constructor.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $application
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Filesystem\FilesystemManager $filesystem
|
2018-03-27 05:44:14 +00:00
|
|
|
*/
|
2019-12-22 23:41:38 +00:00
|
|
|
public function __construct(Application $application, FilesystemManager $filesystem)
|
2018-03-27 05:44:14 +00:00
|
|
|
{
|
|
|
|
$this->application = $application;
|
|
|
|
$this->filesystem = $filesystem->createLocalDriver(['root' => public_path()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modify a URL to append the asset hash.
|
|
|
|
*
|
|
|
|
* @param string $resource
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
|
|
|
*/
|
2018-03-31 20:52:11 +00:00
|
|
|
public function url(string $resource): string
|
2018-03-27 05:44:14 +00:00
|
|
|
{
|
|
|
|
$file = last(explode('/', $resource));
|
2019-12-22 23:41:38 +00:00
|
|
|
$data = Arr::get($this->manifest(), $file) ?? $file;
|
2018-03-27 05:44:14 +00:00
|
|
|
|
2019-12-22 23:41:38 +00:00
|
|
|
return str_replace($file, Arr::get($data, 'src') ?? $file, $resource);
|
2018-06-04 02:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the data integrity hash for a resource.
|
|
|
|
*
|
|
|
|
* @param string $resource
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
|
|
|
*/
|
|
|
|
public function integrity(string $resource): string
|
|
|
|
{
|
|
|
|
$file = last(explode('/', $resource));
|
|
|
|
$data = array_get($this->manifest(), $file, $file);
|
|
|
|
|
2019-12-22 23:41:38 +00:00
|
|
|
return Arr::get($data, 'integrity') ?? '';
|
2018-03-27 05:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 20:52:11 +00:00
|
|
|
* Return a built CSS import using the provided URL.
|
|
|
|
*
|
|
|
|
* @param string $resource
|
|
|
|
* @return string
|
2018-03-27 05:44:14 +00:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
|
|
|
*/
|
2018-03-31 20:52:11 +00:00
|
|
|
public function css(string $resource): string
|
2018-03-27 05:44:14 +00:00
|
|
|
{
|
2020-09-02 02:37:05 +00:00
|
|
|
$attributes = [
|
|
|
|
'href' => $this->url($resource),
|
|
|
|
'rel' => 'stylesheet preload',
|
|
|
|
'as' => 'style',
|
|
|
|
'crossorigin' => 'anonymous',
|
|
|
|
'referrerpolicy' => 'no-referrer',
|
|
|
|
];
|
|
|
|
|
|
|
|
if (config('pterodactyl.assets.use_hash')) {
|
|
|
|
$attributes['integrity'] = $this->integrity($resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = '<link';
|
|
|
|
foreach ($attributes as $key => $value) {
|
|
|
|
$output .= " $key=\"$value\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output . '>';
|
2018-03-27 05:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 20:52:11 +00:00
|
|
|
* Return a built JS import using the provided URL.
|
2018-03-27 05:44:14 +00:00
|
|
|
*
|
|
|
|
* @param string $resource
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
|
|
|
*/
|
2018-03-31 20:52:11 +00:00
|
|
|
public function js(string $resource): string
|
2018-03-27 05:44:14 +00:00
|
|
|
{
|
2020-09-02 02:37:05 +00:00
|
|
|
$attributes = [
|
|
|
|
'src' => $this->url($resource),
|
|
|
|
'crossorigin' => 'anonymous',
|
|
|
|
];
|
|
|
|
|
|
|
|
if (config('pterodactyl.assets.use_hash')) {
|
|
|
|
$attributes['integrity'] = $this->integrity($resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = '<script';
|
|
|
|
foreach ($attributes as $key => $value) {
|
|
|
|
$output .= " $key=\"$value\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output . '></script>';
|
2018-03-27 05:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 20:52:11 +00:00
|
|
|
* Get the asset manifest and store it in the cache for quicker lookups.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
2018-03-27 05:44:14 +00:00
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
|
|
|
*/
|
2018-03-31 20:52:11 +00:00
|
|
|
protected function manifest(): array
|
2018-03-27 05:44:14 +00:00
|
|
|
{
|
2019-12-22 23:41:38 +00:00
|
|
|
return self::$manifest ?: self::$manifest = json_decode(
|
|
|
|
$this->filesystem->get(self::MANIFEST_PATH), true
|
|
|
|
);
|
2018-03-27 05:44:14 +00:00
|
|
|
}
|
|
|
|
}
|