我改了一下kiwi.php

0 views
Skip to first unread message

cv

unread,
Sep 14, 2009, 1:51:42 AM9/14/09
to kiwiphp
我改了一下KiwiSystemCache::getClassFileMapping函数,效率似乎有一点点提升,代码感觉稍微要清晰点吧。
static public function getClassFileMapping($cacheKey, $directories)
{
if ('dev' == Kiwi::$envMode || !self::$opcodeCacheInstance || !
($mapping = self::$opcodeCacheInstance->get($cacheKey)))
{
while ($directories)
{
$currentDir = array_shift($directories);
if (is_dir($currentDir))
{
$dir = new DirectoryIterator($currentDir);
foreach ($dir as $fileinfo)
{
$file = $fileinfo->getFilename();
if (!$fileinfo->isDot () && '.svn' != $file)
{
$currentFile = $fileinfo->getPathname();
$extension = pathinfo($file, PATHINFO_EXTENSION);
if ($fileinfo->isFile () && ($extension == 'php' || $extension
== 'inc'))// we will judge the file extension, we only accept 'php'
and 'inc' file.
{
$currentFileContent = trim(file_get_contents($currentFile));
if(preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|
interface)\s+(\w+)(\s+(extends|implements)\s+(\w+))?\s*{~mi',
$currentFileContent, $classes) > 0)
{
foreach($classes[1] as $key => $class)
{
$mapping[$class] = $currentFile;
}
}
else
{
include($currentFile);
}
}
// if $currentFile is a directory, pass through the next loop.
else if($fileinfo->isDir ())
{
$directories[] = $currentFile . DIRECTORY_SEPARATOR;
}
}
}
}
}
if ('dev' != Kiwi::$envMode && self::$opcodeCacheInstance)
{
self::$opcodeCacheInstance->add($cacheKey, $mapping);
}
}
return $mapping;
}

Qin Jianxiang

unread,
Sep 14, 2009, 3:39:39 AM9/14/09
to kiw...@googlegroups.com
很好!
原来的写法是php4 style
你用DirectoryIterator取代了

最新版本的getClassMapping()在这里:
http://code.google.com/p/lotusphp/source/browse/trunk/runtime/Autoloader/Autoloader.php

主要有两点改动:
1.php5的scandir取代php4的readdir / opendir
2.$currentDir = $directories[$i] 取代 $currentDir =
array_shift($directories); //array_shift肯定是要慢一些的

2009/9/14 cv <jac...@gmail.com>:
--
覃健祥
Qin Jianxiang

Yahoo! China
Bei Jing: +86-10-65986326 13718019218
Hang Zhou: +86-571-26880088-32168 15869189348
Reply all
Reply to author
Forward
0 new messages