This commit is contained in:
2020-09-15 20:11:41 +08:00
parent 3c86f6e3be
commit f97676347a
2 changed files with 41 additions and 10 deletions
+28 -5
View File
@@ -326,18 +326,41 @@ class Snowflake
} }
private static $_autoload = [];
/**
* @param $class
* @param $file
*/
public static function setAutoload($class, $file)
{
static::$_autoload[$class] = $file;
}
/** /**
* @param $className * @param $className
*/ */
public static function autoload($className) public static function autoload($className)
{ {
if (!isset(static::$_autoload[$className])) {
var_dump($className); return;
}
$file = static::$_autoload[$className];
include_once "$file";
} }
} }
//scandir();
//spl_autoload_register([Snowflake::class, 'autoload'], true, true); $content = json_decode(file_get_contents(__DIR__ . '/../composer.json'));
if (isset($content['autoload']) && isset($content['autoload']['psr-4'])) {
$psr4 = $content['autoload']['psr-4'];
foreach ($psr4 as $namespace => $dirname) {
classAutoload($namespace, __DIR__ . '/' . $dirname);
}
}
spl_autoload_register([Snowflake::class, 'autoload'], true, true);
Snowflake::$container = new Container(); Snowflake::$container = new Container();
+13 -5
View File
@@ -38,21 +38,29 @@ if (!function_exists('loadByDir')) {
/** /**
* @param $path * @param $namespace
* @param $dirname
*/ */
function loadByDir($path) function classAutoload($namespace, $dirname)
{ {
$path = rtrim($path, '/'); $path = rtrim(__DIR__ . '/' . $dirname, '/');
foreach (glob($path . '/*') as $value) { foreach (glob($path . '/*') as $value) {
$value = realpath($value); $value = realpath($value);
if (is_dir($value)) { if (is_dir($value)) {
loadByDir($value); classAutoload($namespace . '\\' . implode('\\', $first), $value);
} else { } else {
$pos = strpos($value, '.php'); $pos = strpos($value, '.php');
if ($pos === false || strlen($value) - 4 != $pos) { if ($pos === false || strlen($value) - 4 != $pos) {
continue; continue;
} }
include_once "$value";
$replace = ltrim(str_replace(__DIR__, '', $value), '/');
$replace = str_replace('.php', '', $replace);
$first = explode(DIRECTORY_SEPARATOR, $replace);
array_shift($first);
Snowflake::setAutoload($namespace . '\\' . implode('\\', $first), $value);
} }
} }
} }