Skip to content

Instantly share code, notes, and snippets.

@radist2s
Created May 20, 2019 03:20
Show Gist options
  • Save radist2s/1b46d24fa2130b2abbe6609108f8c121 to your computer and use it in GitHub Desktop.
Save radist2s/1b46d24fa2130b2abbe6609108f8c121 to your computer and use it in GitHub Desktop.
PHP to Blade converter for WordPress templates (WooCommerce mostly)
<?php
$file_path = $argv[1] ?? null;
$file_path_abs = '';
if (!$file_path) {
throw new \Error('No file Path arg provided');
}
if (is_readable($file_path)) {
$file_path_abs = $file_path;
}
else {
$file_path_abs = realpath(rtrim(getcwd(), PHP_EOL) . ltrim($file_path, PHP_EOL));
}
if ($file_path_abs and !is_readable($file_path_abs)) {
throw new \Error("File Path arg: $file_path is not valid, no file found by path: $file_path_abs");
}
if (!$content = trim(file_get_contents($file_path_abs, 'UTF-8'))) {
throw new \Error("File: `$file_path_abs` is empty");
}
$replaces = [
'<\?php echo ([\s\S]+?);?\s*\?>' => '{!! $1 !!}',
'<\?php ([\s\S]+?);?\s*\?>' => '@php $1 @endphp',
'@php (esc_html_|esc_attr_)e([\s\S]+?)\s*@endphp' => '{!! $1_$2 !!}',
'\(\s*([\'"][\s\S]+?[\'"])\s*\)' => '($1)'
];
foreach ($replaces as $regexp => $replace) {
$content = preg_replace("~$regexp~iu", $replace, $content);
}
file_put_contents($file_path_abs, $content);
die($content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment