replace (slower and ugly) create_function with closures

This commit is contained in:
Ralf Becker 2016-07-10 11:09:21 +02:00
parent b345ea6c31
commit 0b71dd2756
6 changed files with 54 additions and 26 deletions

View File

@ -703,8 +703,10 @@ abstract class Merge
} }
// alternative syntax using double curly brackets (eg. {{cat_id}} instead $$cat_id$$), // alternative syntax using double curly brackets (eg. {{cat_id}} instead $$cat_id$$),
// agressivly removing all xml-tags eg. Word adds within placeholders // agressivly removing all xml-tags eg. Word adds within placeholders
$content = preg_replace_callback('/{{[^}]+}}/i',create_function('$p','return \'$$\'.strip_tags(substr($p[0],2,-2)).\'$$\';'),$_content); $content = preg_replace_callback('/{{[^}]+}}/i', function($matches)
{
return '$$'.strip_tags(substr($matches[0], 2, -2)).'$$';
}, $_content);
// Handle escaped placeholder markers in RTF, they won't match when escaped // Handle escaped placeholder markers in RTF, they won't match when escaped
if($mimetype == 'application/rtf') if($mimetype == 'application/rtf')
{ {

View File

@ -570,14 +570,11 @@ class Vfs extends Vfs\StreamWrapper
self::_check_add($options,$path,$result); self::_check_add($options,$path,$result);
} }
} }
// sort code, to place directories before files, if $dirsontop enabled
$dirsfirst = $dirsontop ? '($a[mime]==\''.self::DIR_MIME_TYPE.'\')!==($b[mime]==\''.self::DIR_MIME_TYPE.'\')?'.
'($a[mime]==\''.self::DIR_MIME_TYPE.'\'?-1:1):' : '';
// ordering of the rows // ordering of the rows
if (isset($options['order'])) if (isset($options['order']))
{ {
$sort = strtolower($options['sort']) == 'desc' ? '-' : ''; $sort_desc = strtolower($options['sort']) == 'desc';
switch($options['order']) switch($order = $options['order'])
{ {
// sort numerical // sort numerical
case 'size': case 'size':
@ -586,32 +583,46 @@ class Vfs extends Vfs\StreamWrapper
case 'mode': case 'mode':
case 'ctime': case 'ctime':
case 'mtime': case 'mtime':
$code = $dirsfirst.$sort.'($a[\''.$options['order'].'\']-$b[\''.$options['order'].'\']);'; $ok = uasort($result, function($a, $b) use ($dirsontop, $sort_desc, $order)
// always use name as second sort criteria {
$code = '$cmp = '.$code.' return $cmp ? $cmp : strcasecmp($a[\'name\'],$b[\'name\']);'; $cmp = $a[$order] - $b[$order];
$ok = uasort($result,create_function('$a,$b',$code)); // sort code, to place directories before files, if $dirsontop enabled
if ($dirsontop && ($a['mime'] == self::DIR_MIME_TYPE) !== ($b['mime'] == self::DIR_MIME_TYPE))
{
$cmp = $a['mime' ] == self::DIR_MIME_TYPE ? -1 : 1;
$sort_desc = false;
}
// reverse sort for descending
if ($sort_desc) $cmp *= -1;
// always use name as second sort criteria
if (!$cmp) $cmp = strcasecmp($a['name'], $b['name']);
return $cmp;
});
break; break;
// sort alphanumerical // sort alphanumerical
default: default:
$options['order'] = 'name'; $order = 'name';
// fall throught // fall throught
case 'name': case 'name':
case 'mime': case 'mime':
$code = $dirsfirst.$sort.'strcasecmp($a[\''.$options['order'].'\'],$b[\''.$options['order'].'\']);'; $ok = uasort($result, function($a, $b) use ($dirsontop, $order, $sort_desc)
if ($options['order'] != 'name')
{ {
$cmp = strcasecmp($a[$order], $b[$order]);
// sort code, to place directories before files, if $dirsontop enabled
if ($dirsontop && ($a['mime'] == self::DIR_MIME_TYPE) !== ($b['mime'] == self::DIR_MIME_TYPE))
{
$cmp = $a['mime' ] == self::DIR_MIME_TYPE ? -1 : 1;
$sort_desc = false;
}
// reverse sort for descending
if ($sort_desc) $cmp *= -1;
// always use name as second sort criteria // always use name as second sort criteria
$code = '$cmp = '.$code.' return $cmp ? $cmp : strcasecmp($a[\'name\'],$b[\'name\']);'; if (!$cmp && $order != 'name') $cmp = strcasecmp($a['name'], $b['name']);
} return $cmp;
else });
{
$code = 'return '.$code;
}
$ok = uasort($result,create_function('$a,$b',$code));
break; break;
} }
//echo "<p>order='$options[order]', sort='$options[sort]' --> uasort($result,create_function(,'$code'))=".array2string($ok)."</p>>\n";
} }
// limit resultset // limit resultset
self::$find_total = count($result); self::$find_total = count($result);

View File

@ -1854,7 +1854,12 @@ class calendar_bo
if ($bdays) if ($bdays)
{ {
// sort by month and day only // sort by month and day only
usort($bdays,create_function('$a,$b','return (int) $a[\'bday\'] == (int) $b[\'bday\'] ? strcmp($a[\'bday\'],$b[\'bday\']) : (int) $a[\'bday\'] - (int) $b[\'bday\'];')); usort($bdays, function($a, $b)
{
return (int) $a['bday'] == (int) $b['bday'] ?
strcmp($a['bday'], $b['bday']) :
(int) $a['bday'] - (int) $b['bday'];
});
foreach($bdays as $pers) foreach($bdays as $pers)
{ {
if (empty($pers['bday']) || $pers['bday']=='0000-00-00 0' || $pers['bday']=='0000-00-00' || $pers['bday']=='0.0.00') if (empty($pers['bday']) || $pers['bday']=='0000-00-00 0' || $pers['bday']=='0000-00-00' || $pers['bday']=='0.0.00')

View File

@ -603,7 +603,11 @@ class calendar_so
if ($cat_id) if ($cat_id)
{ {
$cats = $GLOBALS['egw']->categories->return_all_children($cat_id); $cats = $GLOBALS['egw']->categories->return_all_children($cat_id);
array_walk($cats,create_function('&$val,$key','$val = (int) $val;')); array_walk($cats, function(&$val, $key)
{
unset($key); // not used, but required by function signature
$val = (int) $val;
});
if (is_array($cat_id) && count($cat_id)==1) $cat_id = $cat_id[0]; if (is_array($cat_id) && count($cat_id)==1) $cat_id = $cat_id[0];
$sql = '(cal_category'.(count($cats) > 1 ? " IN ('".implode("','",$cats)."')" : '='.$this->db->quote((int)$cat_id)); $sql = '(cal_category'.(count($cats) > 1 ? " IN ('".implode("','",$cats)."')" : '='.$this->db->quote((int)$cat_id));
foreach($cats as $cat) foreach($cats as $cat)

View File

@ -850,7 +850,10 @@ class mail_compose
if ($contacts && $accounts) if ($contacts && $accounts)
{ {
$contacts = array_merge($contacts,$accounts); $contacts = array_merge($contacts,$accounts);
usort($contacts,create_function('$a,$b','return strcasecmp($a["n_fn"],$b["n_fn"]);')); usort($contacts, function($a, $b)
{
return strcasecmp($a['n_fn'], $b['n_fn']);
});
} }
elseif($accounts) elseif($accounts)
{ {

View File

@ -141,7 +141,10 @@ class setup_translation
} }
} }
$d->close(); $d->close();
uasort($languages,create_function('$a,$b','return strcmp(@$a[\'descr\'],@$b[\'descr\']);')); uasort($languages, function($a, $b)
{
return strcmp(@$a['descr'], @$b['descr']);
});
} }
else else
{ {