Skip to content

Commit

Permalink
Camel Case and super methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien committed Apr 25, 2014
1 parent 9c56b7f commit 677bcad
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 44 deletions.
70 changes: 70 additions & 0 deletions src/sbp/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Sbp;

class Handler
{
protected $value;
public function __construct($value)
{
$this->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);
}
}
14 changes: 7 additions & 7 deletions src/sbp/laravel/ClassLoader.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace sbp\laravel;
<?php namespace Sbp\Laravel;

use \sbp\sbp;
use \Sbp\Sbp;

class ClassLoader extends \Illuminate\Support\ClassLoader
{
Expand All @@ -16,7 +16,7 @@ public static function load($class)

foreach (static::$directories as $directory)
{
if (sbp::fileExists($directory.DIRECTORY_SEPARATOR.$class, $path))
if (Sbp::fileExists($directory.DIRECTORY_SEPARATOR.$class, $path))
{
require_once $path;

Expand All @@ -34,17 +34,17 @@ public static function register($prepend = true, $callback = null)
{
if ( ! static::$registered)
{
static::$registered = spl_autoload_register(array('\sbp\laravel\ClassLoader', 'load'), true, $prepend);
static::$registered = spl_autoload_register(array('\\Sbp\\Laravel\\ClassLoader', 'load'), true, $prepend);
$app = $storage = __DIR__.'/../../../../../../app/';
sbp::writeIn(sbp::SAME_DIR);
sbp::fileExists($app.'routes');
Sbp::writeIn(Sbp::SAME_DIR);
Sbp::fileExists($app.'routes');
$storage = $app . 'storage/sbp';
if( ! file_exists($storage))
{
mkdir($storage, 0777);
file_put_contents($storage . '/.gitignore', "*\n!.gitignore");
}
sbp::writeIn($storage, $callback);
Sbp::writeIn($storage, $callback);
}
}
}
72 changes: 37 additions & 35 deletions src/sbp/sbp.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

namespace sbp
namespace Sbp
{

class sbpException extends \Exception {}
class SbpException extends \Exception {}

class sbp
class Sbp
{
const COMMENT = '/* Generated By SBP */';
const VALIDNAME = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
const NUMBER = '(?:0[xbXB])?[0-9]*\.?[0-9]+(?:[eE](?:0[xbXB])?[0-9]*\.?[0-9]+)?';
const VALIDVAR = '(?<!\$)\$+[^\$\n\r]+([\[\{]((?>[^\[\{\]\}]+)|(?-2))*[\]\}])?(?![a-zA-Z0-9_\x7f-\xff\$\[\{])';
const VALIDVAR = '(?<!\$)\$+[^\$\n\r=]+([\[\{]((?>[^\[\{\]\}]+)|(?-2))*[\]\}])?(?![a-zA-Z0-9_\x7f-\xff\$\[\{])';
const BRACES = '(?:\{((?>[^\{\}]+)|(?-2))*\})';
const BRAKETS = '(?:\[((?>[^\[\]]+)|(?-2))*\])';
const PARENTHESES = '(?:\(((?>[^\(\)]+)|(?-2))*\))';
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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 = '(?<![a-zA-Z0-9_\x7f-\xff\$\\\\])(?:\$*[a-zA-Z0-9_\x7f-\xff\\\\]+(?:'.$valueRegexNonCapturant.')+|'.$valueRegexNonCapturant.'|'.$validSubst.'|[\\\\a-zA-Z_][\\\\a-zA-Z0-9_]*|'.self::NUMBER.')';
static::$validExpressionRegex = $validExpressionRegex;
$restoreValues = function ($content) use(&$values)
{
foreach($values as $id => &$string)
Expand All @@ -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;
},

'#(?<!\/)\/[^\/\n][^\n]*\/[Usimxe]*(?!\/)#'
=> function ($match) use($restoreValues)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
}


Expand All @@ -1060,30 +1062,30 @@ function sbp_include_if_exists($file, $once = false)
{
return sbp_include($file, $once);
}
catch(sbp\sbpException $e)
catch(Sbp\SbpException $e)
{
return 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);
}

}
Expand Down
2 changes: 1 addition & 1 deletion tests/sbp/files/.src/regex.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?

$return = "AbcdEF"->replace(/[a-z]/, '#')
$return = "AbcdEF"-->replace(/[a-z]/, '#')
17 changes: 17 additions & 0 deletions tests/sbp/files/.src/super_methods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?

echo 5-->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"
2 changes: 1 addition & 1 deletion tests/sbp/files/regex.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php /* Generated By SBP */

$return = preg_replace('/[a-z]/', '#', "AbcdEF");
$return = (new \Sbp\Handler("AbcdEF"))->replace('/[a-z]/', '#');
17 changes: 17 additions & 0 deletions tests/sbp/files/super_methods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php /* Generated By SBP */

echo (new \Sbp\Handler(5))->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";

0 comments on commit 677bcad

Please sign in to comment.