Added WordPress adaptation.

This commit is contained in:
2022-08-18 05:46:55 +03:00
parent 0d4baf6b11
commit 2917cb7332
7 changed files with 255 additions and 9 deletions
+103
View File
@@ -0,0 +1,103 @@
<?php
// Composer - Autoloader
require_once __DIR__ . '/vendor/autoload.php';
use JetBrains\PhpStorm\NoReturn;
use LightnCandy\Flags;
use LightnCandy\LightnCandy;
trait Custom_Handlebars {
public array $custom_handlebars = [];
public function register_default_handlebar_helpers() {
$this->add_handlebar( 'esc_url', function ( $context ) {
if ( function_exists( 'esc_url' ) ) {
return esc_url( $context );
}
return $context;
} );
$this->add_handlebar( 'esc_attr', function ( $context ) {
if ( function_exists( 'esc_attr' ) ) {
return esc_attr( $context );
}
return $context;
} );
$this->add_handlebar( 'esc_html', function ( $context ) {
if ( function_exists( 'esc_html' ) ) {
return esc_html( $context );
}
return $context;
} );
$this->add_handlebar( 'wp_kses_post', function ( $context ) {
if ( function_exists( 'wp_kses_post' ) ) {
return wp_kses_post( $context );
}
return $context;
} );
}
public function add_handlebar( $key, $func ) {
$this->custom_handlebars[ $key ] = $func;
}
}
class Component_Builder {
use Custom_Handlebars;
public string $component_name = '';
private string $module_path = '';
function __construct( $component_name, $module_path ) {
$this->module_path = $module_path;
$this->component_name = $component_name;
$this->register_default_handlebar_helpers();
}
#[NoReturn]
function build() {
$file_name = $this->get_handlebars_template( __DIR__ . '/' . $this->module_path . '/../../src/' . $this->component_name . '.template.hbs' );
echo "Generated '$file_name'.";
}
private function get_handlebars_template( $path = '' ): string {
$template = file_get_contents( $path );
$phpStr = LightnCandy::compile( $template,
[
'flags' => Flags::FLAG_NOESCAPE | Flags::FLAG_PARENT | Flags::FLAG_SPVARS | Flags::FLAG_ELSE | Flags::FLAG_JSLENGTH | Flags::FLAG_JSTRUE,
'helpers' => $this->custom_handlebars ?? [],
]
);
/**
* NOTE:
* PHP 8.0.0 has problems with the LightCandy lib.
* If you're running the exact php8.0.0 version, try to downgrade to LightCandy@1.2.5.
*/
return self::create_cache_file( $path, $phpStr );
}
private static function create_cache_file( string $file_path, string $content ): string {
$file_path_parts = explode( ".", $file_path );
array_pop( $file_path_parts ); // remove ".hbs" format.
$file_path_parts[] = 'php';
$file_path = join( '.', $file_path_parts );
$t = file_put_contents( $file_path, '<?php ' . $content . ' ?>' );
if ( $t === false ) {
die( "Error: Can't generate HBS template to PHP file. Cache folder is not accessible." );
}
return $file_path;
}
}
( new Component_Builder( $argv[1], $argv[2] ) )->build();
+11
View File
@@ -0,0 +1,11 @@
{
"config": {
"platform": {
"php": "8.0.0"
}
},
"require": {
"php": ">=8.0",
"zordius/lightncandy": "^1.2.6"
}
}
+80
View File
@@ -0,0 +1,80 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "1d9b4e7a02be366b48383c185193727f",
"packages": [
{
"name": "zordius/lightncandy",
"version": "v1.2.6",
"source": {
"type": "git",
"url": "https://github.com/zordius/lightncandy.git",
"reference": "b451f73e8b5c73e62e365997ba3c993a0376b72a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zordius/lightncandy/zipball/b451f73e8b5c73e62e365997ba3c993a0376b72a",
"reference": "b451f73e8b5c73e62e365997ba3c993a0376b72a",
"shasum": ""
},
"require": {
"php": ">=7.1.0"
},
"require-dev": {
"phpunit/phpunit": ">=7"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.2.5-dev"
}
},
"autoload": {
"psr-4": {
"LightnCandy\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Zordius Chen",
"email": "zordius@gmail.com"
}
],
"description": "An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ).",
"homepage": "https://github.com/zordius/lightncandy",
"keywords": [
"handlebars",
"logicless",
"mustache",
"php",
"template"
],
"support": {
"issues": "https://github.com/zordius/lightncandy/issues",
"source": "https://github.com/zordius/lightncandy/tree/v1.2.6"
},
"time": "2021-07-11T04:52:41+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=8.0"
},
"platform-dev": [],
"platform-overrides": {
"php": "8.0.0"
},
"plugin-api-version": "2.1.0"
}