Browse Source

create_function est deprecie en PHP 7.2, on reecrit donc l'optimisation des subwheels via une fonction anonyme de type Closure

Cela a pas mal d'impact sur la fonction compile() qu'on revise en consequence, meme si elle ne sert pas en vrai

Il reste le cas de la rule de type create_function qu'on ne peut pas traiter autrement, mais qui du coup devient depreciee aussi et disparaitra prochainement
(Elle n'est toutefois utilisee nulle part sur la zone)
svn/root/tags/plugins/textwheel/1.6.2 v1.6.2
cedric@yterium.com 3 years ago
parent
commit
71245a0321
  1. 74
      engine/textwheel.php
  2. 2
      engine/textwheelrule.php
  3. 2
      paquet.xml

74
engine/textwheel.php

@ -28,6 +28,8 @@ class TextWheel {
protected $ruleset;
protected static $subwheel = array();
// Experimental : projet de compilation PHP d'une wheel
// pour generation d'un fichier php execute a la place de ->text()
protected $compiled = array();
/**
@ -83,13 +85,14 @@ class TextWheel {
foreach ($rules as $name => $rule) {
$rule->name = $name;
$this->initRule($rule);
if (is_string($rule->replace)
and isset($this->compiled[$rule->replace])
and $fun = $this->compiled[$rule->replace]
if ($rule->replace
and $compiledEntry = $this->ruleCompiledEntryName($rule->replace)
and isset($this->compiled[$compiledEntry])
and $fun = $this->compiled[$compiledEntry]
) {
$pre[] = "\n###\n## $name\n###\n" . $fun;
preg_match(',function (\w+), ', $fun, $r);
$rule->compilereplace = $r[1]; # ne pas modifier ->replace sinon on casse l'execution...
$rule->compilereplace = "'".$r[1]."'"; # ne pas modifier ->replace sinon on casse l'execution...
}
$r = "\t/* $name */\n";
@ -109,9 +112,18 @@ class TextWheel {
if ($rule->func_replace !== 'replace_identity') {
$fun = 'TextWheel::' . $rule->func_replace;
$call = '';
switch ($fun) {
case 'TextWheel::replace_all_cb':
$fun = $rule->replace; # trim()...
if (is_string($rule->replace)) {
$fun = $rule->replace;
}
elseif ($rule->compilereplace) {
$fun = trim($rule->compilereplace, "'");
};
if ($fun) {
$call = "\$t = $fun(\$t);";
}
break;
case 'TextWheel::replace_preg':
$fun = 'preg_replace';
@ -125,7 +137,13 @@ class TextWheel {
default:
break;
}
$r .= "\t" . '$t = ' . $fun . '(' . TextWheel::export($rule->match) . ', ' . TextWheel::export($rule->replace) . ', $t);' . "\n";
if (!$call) {
if (empty($rule->compilereplace)) {
$rule->compilereplace = TextWheel::export($rule->replace);
}
$call = '$t = ' . $fun . '(' . TextWheel::export($rule->match) . ', ' . $rule->compilereplace . ', $t);';
}
$r .= "\t$call\n";
}
$comp[] = $r;
@ -161,6 +179,20 @@ class TextWheel {
return $tw;
}
/**
* @param $replace
* @return string
*/
protected function ruleCompiledEntryName($replace) {
if (is_array($replace)) {
return serialize($replace);
}
elseif (is_object($replace)) {
return get_class($replace) . ':' . spl_object_hash($replace);
}
return $replace;
}
/**
* Initializing a rule a first call
* including file, creating function or wheel
@ -183,25 +215,35 @@ class TextWheel {
}
if ($rule->create_replace) {
// DEPRECATED : rule->create_replace, on ne peut rien faire de mieux ici
// mais c'est voue a disparaitre
$compile = $rule->replace . '($t)';
$rule->replace = create_function('$m', $rule->replace);
$this->compiled[$rule->replace] = $compile;
$this->compiled[$this->ruleCompiledEntryName($rule->replace)] = $compile;
$rule->create_replace = false;
$rule->is_callback = true;
} elseif ($rule->is_wheel) {
$n = count(TextWheel::$subwheel);
}
elseif ($rule->is_wheel) {
$rule_number = count(TextWheel::$subwheel);
TextWheel::$subwheel[] = $this->createSubWheel($rule->replace);
$var = '$m[' . intval($rule->pick_match) . ']';
$cname = 'compiled_' . str_replace('-', '_', $rule->name) . '_' . substr(md5(spl_object_hash($rule)),0,7);
if ($rule->type == 'all' or $rule->type == 'str' or $rule->type == 'split' or !isset($rule->match)) {
$var = '$m';
$rule->replace = function ($m) use ($rule_number) {
return TextWheel::getSubWheel($rule_number)->text($m);
};
$rule->compilereplace = "'$cname'";
}
else {
$pick_match = intval($rule->pick_match);
$rule->replace = function ($m) use ($rule_number, $pick_match) {
return TextWheel::getSubWheel($rule_number)->text($m[$pick_match]);
};
$rule->compilereplace = 'function ($m) { return '.$cname.'($m['.$pick_match.']) }';
}
$code = 'return TextWheel::getSubWheel(' . $n . ')->text(' . $var . ');';
$rule->replace = create_function('$m', $code);
$cname = 'compiled_' . str_replace('-', '_', $rule->name);
$compile = TextWheel::getSubWheel($n)->compile($cname);
$this->compiled[$rule->replace] = $compile;
$rule->is_wheel = false;
$rule->is_callback = true;
$compile = TextWheel::getSubWheel($rule_number)->compile($cname);
$this->compiled[$this->ruleCompiledEntryName($rule->replace)] = $compile;
}
# optimization

2
engine/textwheelrule.php

@ -65,10 +65,12 @@ class TextWheelRule {
# optional
# language specific
public $require; # file to require_once
# DEPRECATED : create_function deprecated a partir de PHP 7.2, ne plus utiliser
public $create_replace; # do create_function('$m', %) on $this->replace, $m is the matched array
# optimizations
public $func_replace;
public $compilereplace;
/**
* Rule constructor

2
paquet.xml

@ -1,7 +1,7 @@
<paquet
prefix="tw"
categorie="edition"
version="1.6.1"
version="1.6.2"
etat="stable"
compatibilite="[3.3.0-dev;3.3.*]"
logo="textwheel-32.png"

Loading…
Cancel
Save