From 5251c5a235f7655543b5362da69f9520d458d3c1 Mon Sep 17 00:00:00 2001 From: Bastien Date: Fri, 28 Mar 2014 21:03:24 +0100 Subject: [PATCH] Allow to pass a callback function to rename on fly the PHP compiled files + Complete lines with semi-colons when needed --- src/sbp/laravel/ClassLoader.php | 4 +- src/sbp/sbp.php | 66 +++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/sbp/laravel/ClassLoader.php b/src/sbp/laravel/ClassLoader.php index eb783d8..b057232 100644 --- a/src/sbp/laravel/ClassLoader.php +++ b/src/sbp/laravel/ClassLoader.php @@ -30,7 +30,7 @@ public static function load($class) * * @return void */ - public static function register($prepend = true) + public static function register($prepend = true, $callback = null) { if ( ! static::$registered) { @@ -44,7 +44,7 @@ public static function register($prepend = true) mkdir($storage, 0777); file_put_contents($storage . '/.gitignore', "*\n!.gitignore"); } - sbp::writeIn($storage); + sbp::writeIn($storage, $callback); } } } \ No newline at end of file diff --git a/src/sbp/sbp.php b/src/sbp/sbp.php index 3a0ad8a..ecd48b7 100644 --- a/src/sbp/sbp.php +++ b/src/sbp/sbp.php @@ -10,8 +10,8 @@ class sbp const COMMENT = '/* Generated By SBP */'; const VALIDNAME = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'; const CONSTNAME = '[A-Z_]+'; - const SUBST = '`'; - const COMP = '÷'; + const SUBST = '÷'; + const COMP = '`'; const COMMENTS = '\/\/.*(?=\n)|\/\*(?:.|\n)*\*\/'; const OPERATORS = '\|\||\&\&|or|and|xor|is|not|<>|lt|gt|<=|>=|\!==|===|\?\:'; const BLOKCS = 'if|else|elseif|try|catch|function|class|trait|switch|while|for|foreach|do'; @@ -23,9 +23,10 @@ class sbp const SAME_DIR = 0x01; static protected $destination = self::SAME_DIR; + static protected $callbackWriteIn = null; static protected $lastParsedFile = null; - static public function writeIn($directory = self::SAME_DIR) + static public function writeIn($directory = self::SAME_DIR, $callback = null) { if($directory !== self::SAME_DIR) { @@ -41,6 +42,14 @@ static public function writeIn($directory = self::SAME_DIR) $directory .= DIRECTORY_SEPARATOR; } self::$destination = $directory; + if( ! is_null($callback)) + { + if( ! is_callable($callback)) + { + throw new sbpException("Invalid callback"); + } + self::$callbackWriteIn = $callback; + } } static public function isSbp($file) @@ -182,6 +191,10 @@ static public function replaceString($match) { $GLOBALS['commentStrings'][] = $id; } + else + { + $GLOBALS['quotedStrings'][] = $id; + } return self::COMP.self::SUBST.$id.self::SUBST.self::COMP; } @@ -235,9 +248,13 @@ static public function fileExists($file, &$phpFile = null) { $file = preg_replace('#(\.sbp)?(\.php)?$#', '', $file); $sbpFile = $file.'.sbp.php'; + $callback = (is_null(static::$callbackWriteIn) ? + 'sha1' : + static::$callbackWriteIn + ); $phpFile = (self::$destination === self::SAME_DIR ? $file.'.php' : - self::$destination.sha1($file).'.php' + self::$destination.$callback($file).'.php' ); if(!file_exists($phpFile)) { @@ -304,6 +321,7 @@ static protected function replace($content, $replace) static public function parse($content) { $GLOBALS['replaceStrings'] = array(); + $GLOBALS['quotedStrings'] = array(); $GLOBALS['commentStrings'] = array(); $content = static::replace($content, array( self::SUBST => self::SUBST.self::SUBST, @@ -333,7 +351,7 @@ static public function parse($content) )); $content = preg_replace_callback('#'.self::COMMENTS.'|'.self::stringRegex().'|\?'.'>.*<\?php#sU', array(get_class(), 'replaceString'), $content); - //$validsubst = self::validSubst(); + $validSubst = self::validSubst('(?:'.implode('|', $GLOBALS['quotedStrings']).')'); $validComments = self::validSubst('(?:'.implode('|', $GLOBALS['commentStrings']).')'); $__file = is_null(static::$lastParsedFile) ? null : realpath(static::$lastParsedFile); @@ -553,7 +571,7 @@ static public function parse($content) { if(trim($line) !== '') { - $espaces = strlen(str_replace("\t", ' ', $line))-strlen(ltrim($line)); + $espaces = strlen(str_replace("\t", ' ', $line))-strlen(ltrim($line)); $c = empty($curind) ? -1 : end($curind); if($espaces > $c) { @@ -623,25 +641,35 @@ static public function parse($content) $content .= "\n" . str_repeat('}', count($curind)); } } + $content = static::replace($content, array( + '#(' . $validSubst . '|[a-zA-Z0-9_\x7f-\xff\)])(? '$1;$2', + '#(' . $validSubst . '|[a-zA-Z0-9_\x7f-\xff\)])(? '$1;$2', + )); foreach($GLOBALS['replaceStrings'] as $id => $string) { $content = str_replace(self::COMP.self::SUBST.$id.self::SUBST.self::COMP, $string, $content); } - $content = strtr($content, array( + $content = static::replace($content, array( "\r" => ' ', - self::SUBST.self::SUBST => self::SUBST, + self::SUBST.self::SUBST + => self::SUBST, + '#(? '$1 ($2) {', + '#(? '$1 ($2) {', + '#(? 'function ($1) {', + '#(? 'function () use', + '#(? '$1 ) use ( $2', + '#\((\([^\(\)]+\))\)#' + => '$1', + '#(catch\s*\([^\)]+\)\s*)([^\s\{])#' + => '$1{} $2', )); - foreach(array( - '#(? '$1 ($2) {', - '#(? '$1 ($2) {', - '#(? 'function ($1) {', - '#(? 'function () use', - '#(? '$1 ) use ( $2', - '#\((\([^\(\)]+\))\)#' => '$1', - '#(catch\s*\([^\)]+\)\s*)([^\s\{])#' => '$1{} $2', - ) as $from => $to) { - $content = preg_replace($from, $to, $content); - } return $content; } }