Last active
August 29, 2015 14:12
-
-
Save ilhnctn/286a34e4bd9b949c925a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "----------------------------------\n"; | |
echo "| PHP to Swig Template Converter |\n"; | |
echo "| |\n"; | |
echo "| @yasinaydin @edigu |\n"; | |
echo "----------------------------------\n"; | |
// User settings | |
$regexPrefix = '/'; | |
$regexSuffix = '/m'; | |
$pageStartContent = "{% extends '../index.html' %}" . PHP_EOL . "{% block page %}" . PHP_EOL; | |
$pageEndContent = PHP_EOL . "{% endblock %}"; | |
$rules = array( | |
'echo_var' => array( | |
'<\?php\s+echo\s+\$(.*?)\?>', | |
'{{ $1 }}', | |
), | |
'include_with_space' => array( | |
'(<\?php\s+)include(|_once)\s+"templates\/(.*)\.php"?;?\s+(\?>)', | |
'{% include "../$3.html" %}', | |
), | |
'include_function' => array( | |
'(<\?php\s+)include(|_once)\("templates\/(.*)\.php"\)?;?\s+(\?>)', | |
'{% include "../$3.html" %}', | |
), | |
'include_multiline_with_variable' => array( | |
'(<\?php(\n|\r)\s+\$)(.*)(;)(\n|\r)\s+include\s+\"(templates)(.*).php\";(\n|\r|\s+)\?>', | |
'{% set $3 %}'.PHP_EOL.'{% include "..$7.html" %}', | |
), | |
'for' => array( | |
'<\?php\s*for\s*\(\s*\$(\w*)\s*=\s*(\d*)\s*;\s*\$\w*\s*<[=]?\s*[\$]?(.*)\s*;\s*(.*):\s*\?>', | |
'{% for $1 in range($2,$3) -%}', | |
), | |
'endfor' => array( | |
'<\?php\s+endfor(\;)?\s*(\;)?\?>', | |
'{%- endfor %}', | |
), | |
'if' => array( | |
'(\n*|\r*|\s*)<\?php\s+if\s*\(\$(.*)\)\s*:\s*\?>', | |
'$1{% if $2 %}', | |
), | |
'else' => array( | |
'<\?php\s+else:\s*\?>', | |
'{% else %}', | |
), | |
'endif' => array( | |
'<\?php\s+endif[\;]*\s*\?>', | |
'{% endif %}', | |
), | |
'set_with_microtime' => array( | |
'<\?php\n*\s*\r*\$(\w*)\s*=\s*"(.*)"\s*.\s*microtime\s*\(true\)\s*;*\n*\s*\r*\?>', | |
'{% set $1 = "$2"+microtime %}', | |
), | |
'equals_to_microtime' => array( | |
'<\?php\n*\s*\r*\$([\_\w]+)\s*=\s*microtime\s*\(true\).*\?>', | |
'{% set $1 = microtime %}' | |
), | |
'two_var_one_include' => array( | |
'<\?php\n*\r*(\s*)\$(\w*)\s*=\s*"(.*)";\n*\r*\s*\$(\w*)\s*=\s*"(.*)";\n*\r*\s*include\s*"templates(.*).php"(;)?\n*\r*\s*\?>', | |
PHP_EOL.'{% set $2 = "$3" %}'.PHP_EOL.'{% set $4 = "$5" %}'.PHP_EOL.'{% include "..$6.html" %}' | |
), | |
'three_var' => array( | |
'<\?php\n*\r*\s*\$(.*)\s*=\s*(.*);\n*\r*\s*\$(.*)\s*=\s*(.*);\n*\r*\s*\$(.*)\s*=\s*(.*);\n*\r*\s*\?>', | |
PHP_EOL.'{% set $1 = $2 %}'.PHP_EOL.'{% set $3 = $4 %}'.PHP_EOL.'{% set $5 = $6 %}' | |
), | |
'echo_isset' => array( | |
'<\?php\s*echo\s*isset\(\$(\w*)\)\s*\?\s*\$(\w*)\s*:\s*""\s*\?>', | |
'{{ $2 }}' | |
), | |
'one_var_one_include' => array( | |
'.*<\?php\s*\$([a-zA-Z1-9\-ÇŞĞÜÖİçşğüöı]+)\s*=\s*(["?]*)([a-zA-Z1-9\-\' ÇŞĞÜÖİçşğüöı]+)(["?]*)\s*;[\n\s]*include\s*"templates\/(\w*)\/([a-zA-Z1-9\-]+).php"[;?]*\s*\?>', | |
'{% set $1 = $2$3$4 %}{% include "../$5/$6.html" %}' | |
), | |
'set_var' => array( | |
'.*<\?php\s*\$(\w*\d*)\s*=\s*"(\w*\d*)"\s*;\s*\?>', | |
'{% set $1 = "$2" %}' | |
) | |
); | |
// DO NOT CHANGE BELOW | |
// Initializing regex patterns | |
$patterns = array(); | |
$replaces = array(); | |
// Creating regex patterns | |
foreach ($rules as $rule) { | |
array_push($patterns, $regexPrefix . $rule[0] . $regexSuffix); | |
array_push($replaces, $rule[1]); | |
} | |
// Getting CLI arg[1] for source directory | |
if (count($argv) != 2 ) { | |
echo "Error! Please provide directory name as path (absolute or relative).\n"; | |
echo "Exiting.\n"; | |
exit(0); | |
} | |
// Loading iterator for files | |
$dir = new RecursiveDirectoryIterator($argv[1]); | |
$rec = new RecursiveIteratorIterator($dir); | |
$pattern = new RegexIterator($rec, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH); | |
// Processing files | |
echo "Converting files...\t"; | |
foreach ($pattern as $name => $file) { | |
$contents = file_get_contents($name); | |
if ( explode("/",$name)[1] === "pages" ) { | |
$contents = $pageStartContent . $contents . $pageEndContent; | |
} | |
$newContent = preg_replace($patterns, $replaces, $contents); | |
$newFilename = str_replace('.php', '.html', $name); | |
file_put_contents($newFilename, $newContent); | |
unlink($name); | |
} | |
echo "Done!\n"; | |
/* | |
// @TODO: will add PHP check after complete | |
// @TODO: tests | |
// @TODO: will replace \n with .PHP_EOL. in regex replaces() | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment