From 677bcad6488e220afffadf0512a7939db9dd32b3 Mon Sep 17 00:00:00 2001 From: Bastien Date: Fri, 25 Apr 2014 14:11:02 +0200 Subject: [PATCH] Camel Case and super methods --- src/sbp/Handler.php | 70 +++++++++++++++++++++++++ src/sbp/laravel/ClassLoader.php | 14 ++--- src/sbp/sbp.php | 72 +++++++++++++------------- tests/sbp/files/.src/regex.php | 2 +- tests/sbp/files/.src/super_methods.php | 17 ++++++ tests/sbp/files/regex.php | 2 +- tests/sbp/files/super_methods.php | 17 ++++++ 7 files changed, 150 insertions(+), 44 deletions(-) create mode 100644 src/sbp/Handler.php create mode 100644 tests/sbp/files/.src/super_methods.php create mode 100644 tests/sbp/files/super_methods.php diff --git a/src/sbp/Handler.php b/src/sbp/Handler.php new file mode 100644 index 0000000..85cf4b2 --- /dev/null +++ b/src/sbp/Handler.php @@ -0,0 +1,70 @@ +value = $value; + } + public function __call($method, $args) + { + if(is_object($this->value) && method_exists($this->value, $method)) + { + return call_user_func_array(array($this->value, $method), $args); + } + if(is_string($this->value)) + { + if(function_exists('preg_' . $method)) + { + $function = 'preg_' . $method; + switch($method) + { + case 'replace': + case 'filter': + case 'replace_callback': + $args = array_merge( + array_slice($args, 0, 2), + array($this->value), + array_slice($args, 2) + ); + break; + + case 'split': + case 'match': + case 'match_all': + $args = array_merge( + array_slice($args, 0, 1), + array($this->value), + array_slice($args, 1) + ); + break; + + default: + array_unshift($args, $this->value); + break; + } + return call_user_func_array($function, $args); + } + elseif(function_exists('str_' . $method)) + { + $function = 'str_' . $method; + array_unshift($args, $this->value); + return call_user_func_array($function, $args); + } + } + elseif(is_array($this->value)) + { + if(function_exists('array_' . $method)) + { + $function = 'array_' . $method; + array_unshift($args, $this->value); + return call_user_func_array($function, $args); + } + } + array_unshift($args, $this->value); + return call_user_func_array($method, $args); + } +} \ No newline at end of file diff --git a/src/sbp/laravel/ClassLoader.php b/src/sbp/laravel/ClassLoader.php index b057232..c1d8191 100644 --- a/src/sbp/laravel/ClassLoader.php +++ b/src/sbp/laravel/ClassLoader.php @@ -1,6 +1,6 @@ -[^\[\{\]\}]+)|(?-2))*[\]\}])?(?![a-zA-Z0-9_\x7f-\xff\$\[\{])'; + const VALIDVAR = '(?[^\[\{\]\}]+)|(?-2))*[\]\}])?(?![a-zA-Z0-9_\x7f-\xff\$\[\{])'; const BRACES = '(?:\{((?>[^\{\}]+)|(?-2))*\})'; const BRAKETS = '(?:\[((?>[^\[\]]+)|(?-2))*\])'; const PARENTHESES = '(?:\(((?>[^\(\)]+)|(?-2))*\))'; @@ -35,6 +35,7 @@ class sbp static protected $callbackWriteIn = null; static protected $lastParsedFile = null; static protected $plugins = array(); + static protected $validExpressionRegex = null; static public function prod($on = true) { @@ -149,11 +150,11 @@ static public function writeIn($directory = self::SAME_DIR, $callback = null) $directory = rtrim($directory, '/\\'); if( ! file_exists($directory)) { - throw new sbpException($directory . " : path not found"); + throw new SbpException($directory . " : path not found"); } if( ! is_writable($directory)) { - throw new sbpException($directory . " : persmission denied"); + throw new SbpException($directory . " : persmission denied"); } $directory .= DIRECTORY_SEPARATOR; } @@ -162,7 +163,7 @@ static public function writeIn($directory = self::SAME_DIR, $callback = null) { if( ! is_callable($callback)) { - throw new sbpException("Invalid callback"); + throw new SbpException("Invalid callback"); } self::$callbackWriteIn = $callback; } @@ -361,12 +362,12 @@ static public function fileParse($from, $to = null) } if(!is_readable($from)) { - throw new sbpException($from." is not readable, try :\nchmod ".static::fileMatchnigLetter($from)."+r ".$from, 1); + throw new SbpException($from." is not readable, try :\nchmod ".static::fileMatchnigLetter($from)."+r ".$from, 1); return false; } if(!is_writable($dir = dirname($to))) { - throw new sbpException($dir." is not writable, try :\nchmod ".static::fileMatchnigLetter($dir)."+w ".$dir, 1); + throw new SbpException($dir." is not writable, try :\nchmod ".static::fileMatchnigLetter($dir)."+w ".$dir, 1); return false; } static::$lastParsedFile = $from; @@ -431,7 +432,7 @@ static public function includeFile($file) } if(!static::fileExists($file, $phpFile)) { - throw new sbpException($file." not found", 1); + throw new SbpException($file." not found", 1); return false; } return include($phpFile); @@ -445,7 +446,7 @@ static public function includeOnceFile($file) } if(!static::fileExists($file, $phpFile)) { - throw new sbpException($file." not found", 1); + throw new SbpException($file." not found", 1); return false; } return include_once($phpFile); @@ -469,11 +470,11 @@ static protected function replace($content, $replace) catch(\Exception $e) { $catched = true; - throw new sbpException('ERREUR PREG : \''.$e->getMessage()."' in:\n".$search, 1); + throw new SbpException('ERREUR PREG : \''.$e->getMessage()."' in:\n".$search, 1); } if(!$catched && preg_last_error()) { - throw new sbpException('ERREUR PREG '.preg_last_error()." in:\n".$search, 1); + throw new SbpException('ERREUR PREG '.preg_last_error()." in:\n".$search, 1); } } return $content; @@ -500,6 +501,19 @@ static public function includeString($string) return static::replaceString(var_export(static::replaceStrings(trim($string)), true)); } + static protected function replaceSuperMethods($content) + { + $method = explode('::', __METHOD__); + return preg_replace_callback( + '#('.static::$validExpressionRegex.'|'.self::VALIDVAR.')-->#', + function ($match) use($method) + { + return '(new \\Sbp\\Handler(' . call_user_func($method, $match[1]) . '))->'; + }, + $content + ); + } + static public function parse($content) { $detect = (strpos($content, 'trois =') !== false); @@ -912,6 +926,7 @@ static public function parse($content) $valueRegex = preg_quote(self::SUBST.self::VALUE).'([0-9]+)'.preg_quote(self::VALUE.self::SUBST); $valueRegexNonCapturant = preg_quote(self::SUBST.self::VALUE).'[0-9]+'.preg_quote(self::VALUE.self::SUBST); $validExpressionRegex = '(? &$string) @@ -931,23 +946,10 @@ static public function parse($content) $filters = function ($content) use($restoreValues, &$values, $valueRegex, $valueRegexNonCapturant, $validSubst, $validExpressionRegex) { $keyWords = self::PHP_WORDS.'|'.self::OPERATORS.'|'.self::BLOKCS; - return static::replace($content,array( + return static::replaceSuperMethods(static::replace($content, array( /*********/ /* Regex */ /*********/ - '#('.$validExpressionRegex.')\s*->(replace|replace_callback|match|match_all|quote|split)\s*('.$valueRegexNonCapturant.')#U' - => function ($match) use(&$values) - { - list($all, $string, $function, $parentheses) = $match; - $key = intval(trim($parentheses, self::SUBST.self::VALUE)); - if(substr(trim($values[$key]), 0, 1) !== '(') - { - return $all; - } - $values[$key] = substr(rtrim($values[$key]), 0, -1).', '.$string.')'; - return 'preg_'.$function.$parentheses; - }, - '#(? function ($match) use($restoreValues) { @@ -965,7 +967,7 @@ static public function parse($content) $values[$id] = $restoreValues('('.$left.', '.$right.')'); return '__sbp_'.$keyWord.self::SUBST.self::VALUE.$id.self::VALUE.self::SUBST; }, - )); + ))); }; $substituteValues = function ($match) use($restoreValues, &$values, $filters) { @@ -1038,13 +1040,13 @@ static public function parse($content) function sbp_include($file, $once = false) { $method = $once ? 'includeOnceFile' : 'includeFile'; - return sbp\sbp::$method($file); + return Sbp\Sbp::$method($file); } function sbp_include_once($file) { - return sbp\sbp::includeOnceFile($file); + return Sbp\Sbp::includeOnceFile($file); } @@ -1060,7 +1062,7 @@ function sbp_include_if_exists($file, $once = false) { return sbp_include($file, $once); } - catch(sbp\sbpException $e) + catch(Sbp\SbpException $e) { return false; } @@ -1068,22 +1070,22 @@ function sbp_include_if_exists($file, $once = false) function sbp_benchmark($title = '') { - sbp\sbp::benchmark($title); + Sbp\Sbp::benchmark($title); } function sbp_benchmark_end() { - sbp\sbp::benchmarkEnd(); + Sbp\Sbp::benchmarkEnd(); } function sbp_add_plugin($plugin, $from, $to = null) { - sbp\sbp::addPlugin($plugin, $from, $to); + Sbp\Sbp::addPlugin($plugin, $from, $to); } function sbp_remove_plugin($plugin) { - sbp\sbp::removePlugin($plugin); + Sbp\Sbp::removePlugin($plugin); } } diff --git a/tests/sbp/files/.src/regex.php b/tests/sbp/files/.src/regex.php index 150f4e7..5b6078a 100644 --- a/tests/sbp/files/.src/regex.php +++ b/tests/sbp/files/.src/regex.php @@ -1,3 +1,3 @@ replace(/[a-z]/, '#') +$return = "AbcdEF"-->replace(/[a-z]/, '#') diff --git a/tests/sbp/files/.src/super_methods.php b/tests/sbp/files/.src/super_methods.php new file mode 100644 index 0000000..42ce024 --- /dev/null +++ b/tests/sbp/files/.src/super_methods.php @@ -0,0 +1,17 @@ +sqrt() . "\n" +echo 5-->pow(4) . "\n" + +$array = { + toto = 4 + tata = 5 + lulu = 6 +} +echo $array-->keys()-->implode(', ') . "\n" + +echo ({ + toto = 4 + tata = 5 + lulu = 6 +}-->keys())-->implode(', ') . "\n" \ No newline at end of file diff --git a/tests/sbp/files/regex.php b/tests/sbp/files/regex.php index caf86ac..d767f2e 100644 --- a/tests/sbp/files/regex.php +++ b/tests/sbp/files/regex.php @@ -1,3 +1,3 @@ replace('/[a-z]/', '#'); diff --git a/tests/sbp/files/super_methods.php b/tests/sbp/files/super_methods.php new file mode 100644 index 0000000..d914f89 --- /dev/null +++ b/tests/sbp/files/super_methods.php @@ -0,0 +1,17 @@ +sqrt() . "\n"; +echo (new \Sbp\Handler(5))->pow(4) . "\n"; + +$array = array( + 'toto' => 4, + 'tata' => 5, + 'lulu' => 6 +); +echo (new \Sbp\Handler((new \Sbp\Handler($array))->keys()))->implode(', ') . "\n"; + +echo (new \Sbp\Handler((new \Sbp\Handler(array( + 'toto' => 4, + 'tata' => 5, + 'lulu' => 6 +)))->keys()))->implode(', ') . "\n"; \ No newline at end of file