From 8a2eead4a034a0ded5f70c18067d528cc4129923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D0=B5=D0=B4=D0=BE=D1=80=20=D0=9F=D0=BE=D0=B4=D0=BB?= =?UTF-8?q?=D0=B5=D1=81=D0=BD=D0=BE=D0=B2?= Date: Tue, 1 Dec 2015 16:28:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=B0=D0=BB=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=B8=D0=BB=D1=8F=D1=86?= =?UTF-8?q?=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- klein.php | 57 +++++++++++++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/klein.php b/klein.php index 9924a26..14be0c5 100644 --- a/klein.php +++ b/klein.php @@ -10,47 +10,38 @@ class Klein { return "\$$first" . implode("", array_map(function($x) { return "['$x']"; }, $list)); }; - $var = "(\w+(\s*\.\s*\w+)*)"; + $rx = function ($grammar) use ($arg) { + list($pattern, $result) = $grammar; + $body = strtr($pattern, [" " => "\s*", "+" => "\s+", ":v" => "(\w+(\s*\.\s*\w+)*)"]); + return ["/$body/", + function ($x) use ($result, $arg) { + $body = preg_replace_callback("/#(\d+)/", function ($s) use($x, $arg) { + return $arg($x[$s[1]]); + }, $result); + return ""; + }]; + }; + + $pattern = array_map($rx, [ + ["{% for+:v+in+:v %}", "foreach(#3 as #1):"], + ["{% endfor %}", "endforeach;"], + ["{% if+:v %}", "if(isset(#1)):"], + ["{% endif %}", "endif;"], + ["{{ :v }}", "echo #1;"] + ]); - list($for, $endfor, $if, $endif) = array_map( - function ($args) { - $body = implode($args, "\s*"); - return "/{%\s*$body\s*%}/"; - },[ - ['for', $var, 'in', $var], - ['endfor'], - ['if', $var], - ['endif'] - ]); - - $pattern = [ - $for => function ($x) use($arg) { - return ""; - }, - $endfor => function ($x) { - return ""; - }, - $if => function ($x) use ($arg) { - return ""; - }, - $endif => function ($x) { - return ""; - }, - "/{{\s*$var\s*}}/" => function ($x) use ($arg) { - return ""; - }, - ]; - + $result = file_get_contents($html); - foreach($pattern as $key => $value) { + foreach($pattern as $arg) { + list($key, $value) = $arg; $result = preg_replace_callback($key, $value, $result); } $this->code = $result; } - function render($vars) { - extract($vars); + function render($vars) { + extract($vars); ob_start(); eval(" ?>".$this->code."