From 829231a79f7e1669d84ead207454140627276da6 Mon Sep 17 00:00:00 2001 From: "Alexander Demidov (Vostro)" Date: Sun, 9 Jan 2022 19:09:18 +0300 Subject: [PATCH] + component & twig function/filter for inject assets from manifest.json --- Plugin.php | 22 ++++++++++---- classes/AssetsManifestException.php | 6 ++++ classes/ManifestReader.php | 14 ++++++++- classes/TwigFilters.php | 15 ++++++++++ components/Manifest.php | 59 +++++++++++++++++++++++++++++++++++++ components/manifest/default.htm | 3 ++ lang/en/lang.php | 18 +++++++++++ lang/ru/lang.php | 18 +++++++++++ 8 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 classes/AssetsManifestException.php create mode 100644 classes/TwigFilters.php create mode 100644 components/Manifest.php create mode 100644 components/manifest/default.htm create mode 100644 lang/en/lang.php create mode 100644 lang/ru/lang.php diff --git a/Plugin.php b/Plugin.php index b17f203..22cec54 100644 --- a/Plugin.php +++ b/Plugin.php @@ -2,6 +2,8 @@ use Backend; use System\Classes\PluginBase; +use Wpstudio\AssetsManifest\Classes\TwigFilters; +use Wpstudio\Assetsmanifest\Components\Manifest; /** * assetsmanifest Plugin Information File @@ -16,8 +18,8 @@ class Plugin extends PluginBase public function pluginDetails() { return [ - 'name' => 'assetsmanifest', - 'description' => 'No description provided yet...', + 'name' => 'Assets manifest', + 'description' => 'Assets with manifest.json', 'author' => 'wpstudio', 'icon' => 'icon-leaf' ]; @@ -50,10 +52,8 @@ class Plugin extends PluginBase */ public function registerComponents() { - return []; // Remove this line to activate - 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'], + ], + ]; + } } diff --git a/classes/AssetsManifestException.php b/classes/AssetsManifestException.php new file mode 100644 index 0000000..a89a88a --- /dev/null +++ b/classes/AssetsManifestException.php @@ -0,0 +1,6 @@ +manifest = json_decode(file_get_contents($manifestPath)); } - public function get($assetName) + public function get(string $assetName) { return $this->manifest->$assetName; } diff --git a/classes/TwigFilters.php b/classes/TwigFilters.php new file mode 100644 index 0000000..acd8f72 --- /dev/null +++ b/classes/TwigFilters.php @@ -0,0 +1,15 @@ +get(Manifest::class); + + assert($manifest instanceof Manifest); + + return $manifest->getManifestReader()->get($assetName); + } +} diff --git a/components/Manifest.php b/components/Manifest.php new file mode 100644 index 0000000..16b8ced --- /dev/null +++ b/components/Manifest.php @@ -0,0 +1,59 @@ + 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; + } +} diff --git a/components/manifest/default.htm b/components/manifest/default.htm new file mode 100644 index 0000000..ef63b50 --- /dev/null +++ b/components/manifest/default.htm @@ -0,0 +1,3 @@ +

This is the default markup for component manifest

+ +You can delete this file if you want diff --git a/lang/en/lang.php b/lang/en/lang.php new file mode 100644 index 0000000..355d3cc --- /dev/null +++ b/lang/en/lang.php @@ -0,0 +1,18 @@ + [ + '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', + ] + ] + ] + ] +]; diff --git a/lang/ru/lang.php b/lang/ru/lang.php new file mode 100644 index 0000000..8916d50 --- /dev/null +++ b/lang/ru/lang.php @@ -0,0 +1,18 @@ + [ + 'manifest' => [ + 'name' => 'Manifest path', + 'description' => 'Устанавливает путь к manifest.json', + 'properties' => [ + 'path' => [ + 'title' => 'Manifest относительно корня', + 'description' => + 'Путь к файлу manifest.json относительно корня проекта. Например: themes/demo/assets/build/manifest.json', + 'validationMessage' => 'Неправильный формат пути к файлу manifest.json', + ] + ] + ] + ] +];