|
|
@ -9,125 +9,127 @@ use LightnCandy\LightnCandy; |
|
|
use Brick\VarExporter\VarExporter; |
|
|
use Brick\VarExporter\VarExporter; |
|
|
|
|
|
|
|
|
trait Custom_Handlebars { |
|
|
trait Custom_Handlebars { |
|
|
public array $custom_handlebars = []; |
|
|
public array $custom_handlebars = []; |
|
|
|
|
|
|
|
|
public function register_default_handlebar_helpers() { |
|
|
public function register_default_handlebar_helpers() { |
|
|
$this->add_handlebar( 'esc_url', function ( $context ) { |
|
|
$this->add_handlebar( 'esc_url', function ( $context ) { |
|
|
if ( function_exists( 'esc_url' ) ) { |
|
|
if ( function_exists( 'esc_url' ) ) { |
|
|
return esc_url( $context ); |
|
|
return esc_url( $context ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return $context; |
|
|
return $context; |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
|
|
|
$this->add_handlebar( 'esc_attr', function ( $context ) { |
|
|
$this->add_handlebar( 'esc_attr', function ( $context ) { |
|
|
if ( function_exists( 'esc_attr' ) ) { |
|
|
if ( function_exists( 'esc_attr' ) ) { |
|
|
return esc_attr( $context ); |
|
|
return esc_attr( $context ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return $context; |
|
|
return $context; |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
|
|
|
$this->add_handlebar( 'esc_html', function ( $context ) { |
|
|
$this->add_handlebar( 'esc_html', function ( $context ) { |
|
|
if ( function_exists( 'esc_html' ) ) { |
|
|
if ( function_exists( 'esc_html' ) ) { |
|
|
return esc_html( $context ); |
|
|
return esc_html( $context ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return $context; |
|
|
return $context; |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
|
|
|
$this->add_handlebar( 'safe_html', function ( $context ) { |
|
|
$this->add_handlebar( 'safe_html', function ( $context ) { |
|
|
if ( function_exists( 'wp_kses_post' ) ) { |
|
|
if ( function_exists( 'wp_kses_post' ) ) { |
|
|
return wp_kses_post( $context ); |
|
|
return wp_kses_post( $context ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return $context; |
|
|
return $context; |
|
|
} ); |
|
|
} ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function add_handlebar( $key, $func ) { |
|
|
public function add_handlebar( $key, $func ) { |
|
|
$this->custom_handlebars[ $key ] = $func; |
|
|
$this->custom_handlebars[ $key ] = $func; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
class Component_Builder { |
|
|
class Component_Builder { |
|
|
use Custom_Handlebars; |
|
|
use Custom_Handlebars; |
|
|
|
|
|
|
|
|
public string $component_name = ''; |
|
|
public string $component_name = ''; |
|
|
public string $module_path = ''; |
|
|
public string $module_path = ''; |
|
|
public string $project_path = ''; |
|
|
public string $project_path = ''; |
|
|
|
|
|
|
|
|
function __construct( $component_name, $module_path, $project_path ) { |
|
|
function __construct( $component_name, $module_path, $project_path ) { |
|
|
$this->module_path = $module_path; |
|
|
$this->module_path = $module_path; |
|
|
$this->project_path = $project_path; |
|
|
$this->project_path = $project_path; |
|
|
$this->component_name = $component_name; |
|
|
$this->component_name = $component_name; |
|
|
$this->register_default_handlebar_helpers(); |
|
|
$this->register_default_handlebar_helpers(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function build(): void { |
|
|
function build(): void { |
|
|
$root_path = __DIR__ . '/' . $this->module_path . '/../../' . $this->project_path; |
|
|
$root_path = __DIR__ . '/' . $this->module_path . '/../../' . $this->project_path; |
|
|
|
|
|
|
|
|
$this->buildTemplatePhpFile( $root_path ); |
|
|
$this->buildTemplatePhpFile( $root_path ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private function buildTemplatePhpFile( $root_path ) { |
|
|
private function buildTemplatePhpFile( $root_path ) { |
|
|
$file_name = $this->get_handlebars_template( "$root_path/src/$this->component_name.template.hbs" ); |
|
|
$file_name = $this->get_handlebars_template( "$root_path/src/$this->component_name.template.hbs" ); |
|
|
|
|
|
|
|
|
$output_folder = $root_path . '/exports/wordpress/templates'; |
|
|
$output_folder = $root_path . '/exports/wordpress/templates'; |
|
|
rename( $file_name, "$output_folder/$this->component_name.template.php" ); |
|
|
rename( $file_name, "$output_folder/$this->component_name.template.php" ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private function get_handlebars_template( $path = '' ): string { |
|
|
private function get_handlebars_template( $path = '' ): string { |
|
|
$template = file_get_contents( $path ); |
|
|
$template = file_get_contents( $path ); |
|
|
$phpStr = LightnCandy::compile( $template, |
|
|
$phpStr = LightnCandy::compile( $template, |
|
|
[ |
|
|
[ |
|
|
'flags' => Flags::FLAG_NOESCAPE | Flags::FLAG_PARENT | Flags::FLAG_SPVARS | Flags::FLAG_ELSE | Flags::FLAG_JSLENGTH | Flags::FLAG_JSTRUE, |
|
|
'flags' => Flags::FLAG_NOESCAPE | Flags::FLAG_PARENT | Flags::FLAG_SPVARS | Flags::FLAG_ELSE | Flags::FLAG_JSLENGTH | Flags::FLAG_JSTRUE, |
|
|
'helpers' => $this->custom_handlebars ?? [], |
|
|
'helpers' => $this->custom_handlebars ?? [], |
|
|
] |
|
|
] |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* NOTE: |
|
|
* NOTE: |
|
|
* PHP 8.0.0 has problems with the LightCandy lib. |
|
|
* 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. |
|
|
* 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 ); |
|
|
return self::create_cache_file( $path, $phpStr ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static function create_cache_file( string $file_path, string $content ): string { |
|
|
private static function create_cache_file( string $file_path, string $content ): string { |
|
|
$file_path_parts = explode( ".", $file_path ); |
|
|
$file_path_parts = explode( ".", $file_path ); |
|
|
array_pop( $file_path_parts ); // remove ".hbs" format. |
|
|
array_pop( $file_path_parts ); // remove ".hbs" format. |
|
|
$file_path_parts[] = 'php'; |
|
|
$file_path_parts[] = 'php'; |
|
|
$file_path = join( '.', $file_path_parts ); |
|
|
$file_path = join( '.', $file_path_parts ); |
|
|
|
|
|
|
|
|
$comment = " |
|
|
$comment = " |
|
|
/** |
|
|
/** |
|
|
* FILE INFO: |
|
|
* FILE INFO: |
|
|
* This file was generated by LightCandy::compile function. |
|
|
* This file was generated by LightCandy::compile function. |
|
|
* The original source is the .HBS(handlebars) file that is located next to this generated PHP file. |
|
|
* The original source is the .HBS(handlebars) file that is located next to this generated PHP file. |
|
|
|
|
|
* |
|
|
|
|
|
* Compiled at " . date( 'Y-m-d h:i:s' ) . " |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
"; |
|
|
"; |
|
|
|
|
|
|
|
|
$t = file_put_contents( $file_path, '<?php ' . $comment . $content . ' ?>' ); |
|
|
$t = file_put_contents( $file_path, '<?php ' . $comment . $content . ' ?>' ); |
|
|
if ( $t === false ) { |
|
|
if ( $t === false ) { |
|
|
die( "Error: Can't generate HBS template to PHP file. Cache folder is not accessible." ); |
|
|
die( "Error: Can't generate HBS template to PHP file. Cache folder is not accessible." ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return $file_path; |
|
|
return $file_path; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function build( $args = [] ) { |
|
|
function build( $args = [] ) { |
|
|
( new Component_Builder( $args['blockName'], $args['backPath'], $args['projectPath'] ) )->build(); |
|
|
( new Component_Builder( $args['blockName'], $args['backPath'], $args['projectPath'] ) )->build(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @throws ExportException |
|
|
* @throws ExportException |
|
|
*/ |
|
|
*/ |
|
|
function jsonToPhp( $args = [] ): ?string { |
|
|
function jsonToPhp( $args = [] ): ?string { |
|
|
$json = $args['json'] ?? []; |
|
|
$json = $args['json'] ?? []; |
|
|
|
|
|
|
|
|
return VarExporter::export( $json ); |
|
|
return VarExporter::export( $json ); |
|
|
} |
|
|
} |
|
|
|