2018-03-27 05:44:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Helpers;
|
|
|
|
|
2019-09-05 03:24:46 +00:00
|
|
|
use Cake\Chronos\Chronos;
|
2018-03-27 05:44:14 +00:00
|
|
|
use Illuminate\Filesystem\FilesystemManager;
|
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
|
|
|
|
|
|
|
class AssetHashService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Location of the manifest file generated by gulp.
|
|
|
|
*/
|
|
|
|
public const MANIFEST_PATH = './assets/manifest.json';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Cache\Repository
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Filesystem\Filesystem
|
|
|
|
*/
|
|
|
|
private $filesystem;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Foundation\Application
|
|
|
|
*/
|
|
|
|
private $application;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var null|array
|
|
|
|
*/
|
|
|
|
protected static $manifest;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AssetHashService constructor.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $application
|
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache
|
|
|
|
* @param \Illuminate\Filesystem\FilesystemManager $filesystem
|
|
|
|
*/
|
|
|
|
public function __construct(Application $application, CacheRepository $cache, FilesystemManager $filesystem)
|
|
|
|
{
|
|
|
|
$this->application = $application;
|
|
|
|
$this->cache = $cache;
|
|
|
|
$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));
|
2018-06-04 02:35:50 +00:00
|
|
|
$data = array_get($this->manifest(), $file, $file);
|
2018-03-27 05:44:14 +00:00
|
|
|
|
2018-06-07 04:44:52 +00:00
|
|
|
return str_replace($file, array_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);
|
|
|
|
|
|
|
|
return array_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
|
|
|
{
|
2018-06-04 02:35:50 +00:00
|
|
|
return '<link href="' . $this->url($resource) . '"
|
|
|
|
rel="stylesheet preload"
|
2018-06-16 19:43:32 +00:00
|
|
|
as="style"
|
2018-06-04 02:35:50 +00:00
|
|
|
crossorigin="anonymous"
|
|
|
|
integrity="' . $this->integrity($resource) . '"
|
|
|
|
referrerpolicy="no-referrer">';
|
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
|
|
|
{
|
2018-06-04 02:35:50 +00:00
|
|
|
return '<script src="' . $this->url($resource) . '"
|
|
|
|
integrity="' . $this->integrity($resource) . '"
|
|
|
|
crossorigin="anonymous"></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
|
|
|
{
|
2018-03-31 20:52:11 +00:00
|
|
|
if (! is_null(self::$manifest)) {
|
|
|
|
return self::$manifest;
|
|
|
|
}
|
2018-03-27 05:44:14 +00:00
|
|
|
|
2018-03-31 20:52:11 +00:00
|
|
|
// Skip checking the cache if we are not in production.
|
|
|
|
if ($this->application->environment() === 'production') {
|
|
|
|
$stored = $this->cache->get('Core:AssetManifest');
|
|
|
|
if (! is_null($stored)) {
|
|
|
|
return self::$manifest = $stored;
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 05:44:14 +00:00
|
|
|
|
2018-03-31 20:52:11 +00:00
|
|
|
$contents = json_decode($this->filesystem->get(self::MANIFEST_PATH), true);
|
2019-09-05 03:24:46 +00:00
|
|
|
$this->cache->put('Core:AssetManifest', $contents, Chronos::now()->addMinutes(1440));
|
2018-03-27 05:44:14 +00:00
|
|
|
|
2018-03-31 20:52:11 +00:00
|
|
|
return self::$manifest = $contents;
|
2018-03-27 05:44:14 +00:00
|
|
|
}
|
|
|
|
}
|