You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.8 KiB
103 lines
2.8 KiB
<?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();
|
|
|