diff --git a/System/Snowflake.php b/System/Snowflake.php index 5f967980..3139e3d9 100644 --- a/System/Snowflake.php +++ b/System/Snowflake.php @@ -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 */ public static function autoload($className) { - - var_dump($className); - + if (!isset(static::$_autoload[$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(); diff --git a/function.php b/function.php index 966a1409..b0b6a174 100644 --- a/function.php +++ b/function.php @@ -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) { $value = realpath($value); if (is_dir($value)) { - loadByDir($value); + classAutoload($namespace . '\\' . implode('\\', $first), $value); } else { $pos = strpos($value, '.php'); if ($pos === false || strlen($value) - 4 != $pos) { 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); } } }