+ component & twig function/filter for inject assets from manifest.json
This commit is contained in:
22
Plugin.php
22
Plugin.php
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
use Backend;
|
use Backend;
|
||||||
use System\Classes\PluginBase;
|
use System\Classes\PluginBase;
|
||||||
|
use Wpstudio\AssetsManifest\Classes\TwigFilters;
|
||||||
|
use Wpstudio\Assetsmanifest\Components\Manifest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* assetsmanifest Plugin Information File
|
* assetsmanifest Plugin Information File
|
||||||
@ -16,8 +18,8 @@ class Plugin extends PluginBase
|
|||||||
public function pluginDetails()
|
public function pluginDetails()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name' => 'assetsmanifest',
|
'name' => 'Assets manifest',
|
||||||
'description' => 'No description provided yet...',
|
'description' => 'Assets with manifest.json',
|
||||||
'author' => 'wpstudio',
|
'author' => 'wpstudio',
|
||||||
'icon' => 'icon-leaf'
|
'icon' => 'icon-leaf'
|
||||||
];
|
];
|
||||||
@ -50,10 +52,8 @@ class Plugin extends PluginBase
|
|||||||
*/
|
*/
|
||||||
public function registerComponents()
|
public function registerComponents()
|
||||||
{
|
{
|
||||||
return []; // Remove this line to activate
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'Wpstudio\Assetsmanifest\Components\MyComponent' => 'myComponent',
|
Manifest::class => 'manifest',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,4 +93,16 @@ class Plugin extends PluginBase
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function registerMarkupTags()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'filters' => [
|
||||||
|
'manifest' => [TwigFilters::class, 'manifest'],
|
||||||
|
],
|
||||||
|
'functions' => [
|
||||||
|
'manifest' => [TwigFilters::class, 'manifest'],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
6
classes/AssetsManifestException.php
Normal file
6
classes/AssetsManifestException.php
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?php namespace Wpstudio\AssetsManifest\Classes;
|
||||||
|
|
||||||
|
class AssetsManifestException extends \ErrorException
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
@ -4,12 +4,24 @@ class ManifestReader
|
|||||||
{
|
{
|
||||||
private $manifest = null;
|
private $manifest = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $manifestPath
|
||||||
|
* @throws AssetsManifestException
|
||||||
|
*/
|
||||||
public function __construct(string $manifestPath)
|
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));
|
$this->manifest = json_decode(file_get_contents($manifestPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get($assetName)
|
public function get(string $assetName)
|
||||||
{
|
{
|
||||||
return $this->manifest->$assetName;
|
return $this->manifest->$assetName;
|
||||||
}
|
}
|
||||||
|
15
classes/TwigFilters.php
Normal file
15
classes/TwigFilters.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
59
components/Manifest.php
Normal file
59
components/Manifest.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php namespace Wpstudio\Assetsmanifest\Components;
|
||||||
|
|
||||||
|
use Cms\Classes\ComponentBase;
|
||||||
|
use Wpstudio\AssetsManifest\Classes\ManifestReader;
|
||||||
|
|
||||||
|
class Manifest extends ComponentBase
|
||||||
|
{
|
||||||
|
const LANG_PREFIX = 'wpstudio.assetsmanifest::lang.components.manifest.';
|
||||||
|
|
||||||
|
private ?ManifestReader $manifestReader;
|
||||||
|
|
||||||
|
public function componentDetails(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => self::LANG_PREFIX . 'name',
|
||||||
|
'description' => self::LANG_PREFIX . 'description',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function defineProperties(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'path' => [
|
||||||
|
'title' => self::LANG_PREFIX . 'properties.path.title',
|
||||||
|
'description' => self::LANG_PREFIX . 'properties.path.description',
|
||||||
|
'validationPattern' => '^[^/].*/manifest.json',
|
||||||
|
'validationMessage' => self::LANG_PREFIX . 'properties.path.validationMessage',
|
||||||
|
'placeholder' => 'assets/build/manifest.json',
|
||||||
|
'showExternalParam' => false,
|
||||||
|
'required' => true,
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws \Wpstudio\AssetsManifest\Classes\AssetsManifestException
|
||||||
|
*/
|
||||||
|
public function init(): void
|
||||||
|
{
|
||||||
|
app()->instance(Manifest::class, $this);
|
||||||
|
|
||||||
|
$this->prepareVars();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws \Wpstudio\AssetsManifest\Classes\AssetsManifestException
|
||||||
|
*/
|
||||||
|
private function prepareVars(): void
|
||||||
|
{
|
||||||
|
$this->manifestReader = new ManifestReader(base_path($this->property('path')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getManifestReader(): ManifestReader
|
||||||
|
{
|
||||||
|
return $this->manifestReader;
|
||||||
|
}
|
||||||
|
}
|
3
components/manifest/default.htm
Normal file
3
components/manifest/default.htm
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<p>This is the default markup for component manifest</p>
|
||||||
|
|
||||||
|
<small>You can delete this file if you want</small>
|
18
lang/en/lang.php
Normal file
18
lang/en/lang.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'components' => [
|
||||||
|
'manifest' => [
|
||||||
|
'name' => 'Manifest path',
|
||||||
|
'description' => 'Accept manifest.json path',
|
||||||
|
'properties' => [
|
||||||
|
'path' => [
|
||||||
|
'title' => 'Manifest relative basepath',
|
||||||
|
'description' =>
|
||||||
|
'manifest.json path relative from basepath. Example: themes/demo/assets/build/manifest.json',
|
||||||
|
'validationMessage' => 'Incorrect manifest.json path',
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
18
lang/ru/lang.php
Normal file
18
lang/ru/lang.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'components' => [
|
||||||
|
'manifest' => [
|
||||||
|
'name' => 'Manifest path',
|
||||||
|
'description' => 'Устанавливает путь к manifest.json',
|
||||||
|
'properties' => [
|
||||||
|
'path' => [
|
||||||
|
'title' => 'Manifest относительно корня',
|
||||||
|
'description' =>
|
||||||
|
'Путь к файлу manifest.json относительно корня проекта. Например: themes/demo/assets/build/manifest.json',
|
||||||
|
'validationMessage' => 'Неправильный формат пути к файлу manifest.json',
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
Reference in New Issue
Block a user