Skip to content

Commit

Permalink
Allow to pass a callback function to rename on fly the PHP compiled f…
Browse files Browse the repository at this point in the history
…iles + Complete lines with semi-colons when needed
  • Loading branch information
Bastien committed Mar 28, 2014
1 parent 686a85a commit 5251c5a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/sbp/laravel/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}
}
}
66 changes: 47 additions & 19 deletions src/sbp/sbp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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\)])(?<!<\?php|<\?)(\s*(?:' . $validComments . '\s*)*[\n\r]+\s*(?:' . $validComments . '\s*)*)(?=[a-zA-Z0-9_\x7f-\xff\$\}]|$)#U'
=> '$1;$2',
'#(' . $validSubst . '|[a-zA-Z0-9_\x7f-\xff\)])(?<!<\?php|<\?)(\s*(?:' . $validComments . '\s*)*)$#U'
=> '$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,
'#(?<![a-zA-Z0-9_\x7f-\xff\$])('.self::IF_BLOKCS.')(?:\s+(\S.*))?\s*\{#U'
=> '$1 ($2) {',
'#(?<![a-zA-Z0-9_\x7f-\xff\$])(function\s+'.self::VALIDNAME.')(?:\s+(array\s.+|[A-Z\$\&].+))?\s*\{#U'
=> '$1 ($2) {',
'#(?<![a-zA-Z0-9_\x7f-\xff\$])function\s*(array\s.+|[A-Z\$\&].+)?\s*\{#U'
=> 'function ($1) {',
'#(?<![a-zA-Z0-9_\x7f-\xff\$])function\s+use(?![a-zA-Z0-9_\x7f-\xff])#U'
=> 'function () use',
'#(?<![a-zA-Z0-9_\x7f-\xff\$])(function.*[^a-zA-Z0-9_\x7f-\xff\$])use\s*((array\s.+|[A-Z\$\&].+)\{)#U'
=> '$1 ) use ( $2',
'#\((\([^\(\)]+\))\)#'
=> '$1',
'#(catch\s*\([^\)]+\)\s*)([^\s\{])#'
=> '$1{} $2',
));
foreach(array(
'#(?<![a-zA-Z0-9_\x7f-\xff\$])('.self::IF_BLOKCS.')(?:\s+(\S.*))?\s*\{#U' => '$1 ($2) {',
'#(?<![a-zA-Z0-9_\x7f-\xff\$])(function\s+'.self::VALIDNAME.')(?:\s+(array\s.+|[A-Z\$\&].+))?\s*\{#U' => '$1 ($2) {',
'#(?<![a-zA-Z0-9_\x7f-\xff\$])function\s*(array\s.+|[A-Z\$\&].+)?\s*\{#U' => 'function ($1) {',
'#(?<![a-zA-Z0-9_\x7f-\xff\$])function\s+use(?![a-zA-Z0-9_\x7f-\xff])#U' => 'function () use',
'#(?<![a-zA-Z0-9_\x7f-\xff\$])(function.*[^a-zA-Z0-9_\x7f-\xff\$])use\s*((array\s.+|[A-Z\$\&].+)\{)#U' => '$1 ) use ( $2',
'#\((\([^\(\)]+\))\)#' => '$1',
'#(catch\s*\([^\)]+\)\s*)([^\s\{])#' => '$1{} $2',
) as $from => $to) {
$content = preg_replace($from, $to, $content);
}
return $content;
}
}
Expand Down

0 comments on commit 5251c5a

Please sign in to comment.