Skip to content

Instantly share code, notes, and snippets.

@Thijzer
Last active August 29, 2015 13:56
Show Gist options
  • Save Thijzer/9319606 to your computer and use it in GitHub Desktop.
Save Thijzer/9319606 to your computer and use it in GitHub Desktop.
Fork: spoon 2 twig converter
#!/usr/bin/php -q
<?php
// declaring some vars
(!empty($argv[1])) ? $inputFile = $argv[1] : exit('no input file given as parameter' . PHP_EOL);
date_default_timezone_set("Europe/Brussels");
$selection = false;
$method = false;
$fh = fopen('php://stdin', 'r');
$options = array(
1 => 'spoon-2-twig-tpl',
2 => 'spoon-2-twig-mod',
3 => 'twig-2-spoon-tmp',
4 => 'twig-2-spoon-mod',
5 => 'manager-2-dev'
);
// grab file from command line parameter
if (!file_exists($inputFile)) {
$inputFile = realpath(dirname(__FILE__)) . '/' . $inputFile;
}
elseif (file_exists($inputFile)) {
$stream = fopen($inputFile, 'r');
$filedata = stream_get_contents($stream, -1, 10);
fclose($stream);
}
else exit('Could not open input file: ' . $inputFile . PHP_EOL);
// command line loop
while (!$method) {
if ($selection > 0 AND $selection < 6) {
$method = $options[$selection];
} elseif($selection) {
echo " -> You have to choose a number from 1 to 5\n\n";
$selection = false;
} else {
echo "\n $inputFile is ready to convert \n\n";
echo "\n select your conversion \n\n";
echo " (1) spoon to twig template\n";
echo " (2) spoon to twig module\n";
echo " (3) twig to spoon template\n";
echo " (4) twig to spoon module\n";
echo " (5) manager to developer\n\n";
$selection = trim(fgets($fh, 1024)); // blinkie cursor wait.
}
}
/***** the functions ******/
// timestamp
define('TIME', microtime(true));
function timestamp($i = null) {
return (float)substr(microtime(true) - TIME ,0,(int)$i+5) * 1000;
}
// our preg_match_all_and_sprinf_replace function
function preg_replace_sprintf($regex, $format, $filedata) {
preg_match_all($regex, $filedata, $match);
if ($match) {
$values = array();
foreach ($match[1] as $value) {
$values[] = sprintf($format, $value);
}
return str_replace($match[0], $values , $filedata);
}
else echo 'no match found on the ' . $regex . ' line' . PHP_EOL;
}
/***** CONVERSION STRINGS START HERE ******/
require $method . '.conv';
/***** OUR OUTPUT CODE ******/
if ($outputFile) {
file_put_contents($outputFile, $filedata);
echo $outputFile . ' was saved in ' . timestamp(2) . ' milliseconds' . PHP_EOL;
}
// todo
// we might want to add version control example from 3.6 to 3.
<?php
$outputFile = false; //$inputFile . '.twig';
echo "''+ `'''+####;+''+###++###@##@@@+++@@@@;`.+++++..````` ````.,.\n";
echo "''; `.++#####;';'+##+##+@@@@@@@@@@@@@@@@+@@@@@@.`` ` ` ,:\n";
echo "'' `.+@####;''++####+#@@ ;. @@@# @'`` ` `,\n";
echo "'+ `,#####';;+++##+##@@ ; : +@@; @@``` ` ` ` ` \n";
echo "'.` `.+##','''##+;. @@ @. ;@ @ +;#@@ @@ ` ` ` ```\n";
echo "' ``` ` @@ @. ;@@@ +@@@ @@``` `` ```\n";
echo "; @@ @. ;@@@@; #@@ , @@ ` ``\n";
echo ". ```@@ @. ;@@@ + .@@ '@ ` ` ` ``` ` ``\n";
echo " ` ```@@ @. ;@@@ # .@@ . .@. `` ````` `\n";
echo " `` ````.@@ @. ;@+@, @@@ @ @; ` ````` \n";
echo " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@;@@@@@@@@@@# ``` ````\n";
echo " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@+@@@@@@@@@@@@.`` `` `\n";
echo " @; @ `@@.@' @@@ ,@@ ```` `\n";
echo " @; @ `@@@, @@@ ;@;`` ` ``\n";
echo " @; @ @@@ @@@ @#``` ``\n";
echo " @+;;` ;;;@ @+ @@@ #@@ @@ @@ ` ` ``\n";
echo " @@@@. @@@@ @@ @@@ , :@@ @@ @@ ``` ``\n";
echo " ` `@. @@`@ @@ @@@ + @@ @@ @@``` ``\n";
echo " @. @@ @ @# @@@ @ @@ @@ @@` `````\n";
echo " @. @@ @ @@@ @ @@ @: @@`` ` ``\n";
echo " @. @@ @ .@@@; @ @@ @@` ```\n";
echo " @. @@ @ +@@. @ @@ ;@;`` ````\n";
echo " @. @@ @ @; @@ @ '@ +@@` ```\n";
echo " @. @@ @ @@ @@ .@ .@ @@@@@@````` ```\n";
echo " @. @@ @ @@ @@ @ @@@#: ``` ```\n";
echo " @. @@ @ @@ @@ @ @@```` ` ` ```\n";
echo " @. @@ @ @@ @@ @ @@ `` ```\n";
echo " @. @@ @ @@ @' ,@ @ @@ ` ` ` ````\n";
echo " @. @@ @ @@ @: ;@. @ @@ ````````````\n";
echo " @. @@ @ @@ @ +@; ; @@ ` ```````\n";
echo " @, @@ @ @@ @ @@+ ` @@``` ````` ```\n";
echo " @@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ````````````\n";
echo "\n";
<?php
$outputFile = $inputFile . '.twig';
$filedata = preg_replace_sprintf('/{include:(.*)}/i', '{%% include %s %%}', $filedata); // for includes
$filedata = preg_replace_sprintf('/{\$(.*?)}/', '{{ %s }}', $filedata); // for variables {$variable }
$filedata = preg_replace_sprintf('/{\/option:(.*?)}/i', '{%% endif %%}', $filedata); // for {option: variable }
$filedata = preg_replace_sprintf('/{option:!(.*?)}/i', '{%% if %s is empty %%}', $filedata);
$filedata = preg_replace_sprintf('/{option:(.*?)}/i', '{%% if %s is defined %%}', $filedata);
$filedata = preg_replace_sprintf('/{\/iteration:(.*?)}/i', '{%% endif %%}', $filedata); // for {option: variable }
$filedata = preg_replace_sprintf('/{iteration:(.*?)}/i', '{%% for %s in xxx %%}', $filedata);
// this is probably not a block segment
$filedata = preg_replace_sprintf('/{\/form:(.*?)}/i', '{%% endblock %%}', $filedata); // for {form:add}
$filedata = preg_replace_sprintf('/{form:(.*?)}/i', '{%% block %s %%}', $filedata);
$filedata = preg_replace_sprintf('/{\*(.*)\*}/', '{#%s#}', $filedata); // comments
// string replacers in the last part
$filedata = str_replace('|ucfirst', '|capitalize' , $filedata);
//form values
$filedata = str_replace('{{ txt', '{{ txt.' , $filedata);
$filedata = str_replace('{{ lbl', '{{ lbl.' , $filedata);
$filedata = str_replace('{{ msg', '{{ msg.' , $filedata);
$filedata = str_replace('{{ file', '{{ file.' , $filedata);
$filedata = str_replace('{{ ddm', '{{ ddm.' , $filedata);
$filedata = str_replace('{{ chk', '{{ chk.' , $filedata);
$filedata = str_replace('{{ err', '{{ err.' , $filedata);
// some EOL code
$filedata .= '{# this file was generated with fork-converter on ' . date('Y-m-d') . ' #}';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment