Generate Template_Defaults file.

This commit is contained in:
2022-11-12 23:32:01 +02:00
parent 05660eb4e4
commit ac70e7eb9a
9 changed files with 1224 additions and 65 deletions
+22 -15
View File
@@ -3,8 +3,10 @@
// Composer - Autoloader
require_once __DIR__ . '/vendor/autoload.php';
use Brick\VarExporter\ExportException;
use LightnCandy\Flags;
use LightnCandy\LightnCandy;
use Brick\VarExporter\VarExporter;
trait Custom_Handlebars {
public array $custom_handlebars = [];
@@ -52,33 +54,27 @@ class Component_Builder {
use Custom_Handlebars;
public string $component_name = '';
private string $module_path = '';
public string $module_path = '';
public string $project_path = '';
function __construct( $component_name, $module_path, $project_path ) {
$this->module_path = $module_path;
$this->project_path = $project_path;
$this->project_path = $project_path;
$this->component_name = $component_name;
$this->register_default_handlebar_helpers();
}
function build(): void {
$root_path = __DIR__ . '/' . $this->module_path . '/../../' . $this->project_path;
$this->buildTemplatePhpFile( $root_path );
}
private function buildTemplatePhpFile( $root_path ) {
$file_name = $this->get_handlebars_template( "$root_path/src/$this->component_name.template.hbs" );
$output_folder = $root_path . '/exports/wordpress/templates';
foreach ( [ 'styles', 'scripts', 'images' ] as $dir ) {
$output_dir = "$output_folder/$dir";
if ( is_dir( $output_dir ) === false ) {
mkdir( $output_dir, 0777, true );
}
}
rename( $file_name, "$output_folder/$this->component_name.template.php" );
copy( "$root_path/src/styles/$this->component_name.min.css", "$output_folder/styles/$this->component_name.min.css" );
copy( "$root_path/src/scripts/$this->component_name.min.js", "$output_folder/scripts/$this->component_name.min.js" );
shell_exec( "cp -r $root_path/src/images $output_folder" );
echo "Generated '$file_name'.";
}
private function get_handlebars_template( $path = '' ): string {
@@ -123,4 +119,15 @@ class Component_Builder {
}
}
( new Component_Builder( $argv[1], $argv[2], $argv[3] ) )->build();
function build( $args = [] ) {
( new Component_Builder( $args['blockName'], $args['backPath'], $args['projectPath'] ) )->build();
}
/**
* @throws ExportException
*/
function jsonToPhp( $args = [] ): ?string {
$json = $args['json'] ?? [];
return VarExporter::export( $json );
}