+ component & twig function/filter for inject assets from manifest.json

This commit is contained in:
2022-01-09 19:09:18 +03:00
parent ae36a5cb0a
commit 829231a79f
8 changed files with 149 additions and 6 deletions

View File

@ -0,0 +1,6 @@
<?php namespace Wpstudio\AssetsManifest\Classes;
class AssetsManifestException extends \ErrorException
{
}

View File

@ -4,12 +4,24 @@ class ManifestReader
{
private $manifest = null;
/**
* @param string $manifestPath
* @throws AssetsManifestException
*/
public function __construct(string $manifestPath)
{
if (!file_exists($manifestPath)) {
throw new AssetsManifestException('Not found: ' . $manifestPath);
}
if (!is_readable($manifestPath)) {
throw new AssetsManifestException('Not readable: ' . $manifestPath);
}
$this->manifest = json_decode(file_get_contents($manifestPath));
}
public function get($assetName)
public function get(string $assetName)
{
return $this->manifest->$assetName;
}

15
classes/TwigFilters.php Normal file
View File

@ -0,0 +1,15 @@
<?php namespace Wpstudio\AssetsManifest\Classes;
use Wpstudio\Assetsmanifest\Components\Manifest;
class TwigFilters
{
public static function manifest($assetName): string
{
$manifest = app()->get(Manifest::class);
assert($manifest instanceof Manifest);
return $manifest->getManifestReader()->get($assetName);
}
}