php-vn-lib/vn-autoload.php

36 lines
811 B
PHP
Raw Normal View History

<?php
$vnAutoloadReplace = function($matches) {
$match = strtolower($matches[0]);
if (strlen($match) == 1)
return $match;
if ($match[0] == '\\')
return '/'. $match[1];
return $match[0] .'-'. $match[1];
};
spl_autoload_register(function($className) {
global $vnAutoloadReplace, $vnAutoloadMap;
$classPath = preg_replace_callback('/.?[A-Z]/', $vnAutoloadReplace, $className);
$classPath .= '.php';
if (isset($vnAutoloadMap))
foreach ($vnAutoloadMap as $prefix => $location) {
$prefixLen = strlen($prefix);
if (strncmp($classPath, $prefix, $prefixLen) == 0) {
$classPath = "$location/". substr($classPath, $prefixLen);
break;
}
}
if ($classPath[0] != '/')
$classPath = stream_resolve_include_path($classPath);
if (file_exists($classPath))
require_once $classPath;
});