Skip to content

Commit

Permalink
add cache of searching type classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nishimura committed Jan 27, 2016
1 parent 5e44c1f commit 2868cdf
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Laiz/Func/functions.php
Expand Up @@ -44,17 +44,28 @@ function _typeToInstance($method){
}
function _classToInstance($type, $method)
{
static $cache;
if ($cache === null)
$cache = [];
if (isset($cache[$type][$method]))
return $cache[$type][$method];

$prefix = _typeToInstance($method);
$class = preg_replace('/^(.*?)(\\\\[[:alnum:]_]+)$/',
'\\1\\' . $prefix . '\\2', $type);
if (!class_exists($class)){
$old = $class;
$oldType = $type;
$type = preg_replace('/\\\\[[:alnum:]_]+$/', '', $type);
$class = preg_replace('/^(.*?)(\\\\[[:alnum:]_]+)$/',
'\\1\\' . $prefix . '\\2', $type);
assert(class_exists($class),
"Class [$old] and [$class] not exists.");

$cache[$oldType][$method] = $class;
}

$cache[$type][$method] = $class;
return $class;
}

Expand Down

0 comments on commit 2868cdf

Please sign in to comment.