From 18a4eee00f1d070a3c2855245af694411aa770b5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 22 Jun 2016 16:00:03 +0800 Subject: [PATCH] =?UTF-8?q?Loader=E7=B1=BB=E7=9A=84composer=E5=8C=85?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E6=96=B9=E6=B3=95=E6=94=B9=E4=B8=BApublic=20?= =?UTF-8?q?=E4=BE=9B=E5=91=BD=E4=BB=A4=E8=A1=8C=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Loader.php | 66 ++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 43 deletions(-) diff --git a/library/think/Loader.php b/library/think/Loader.php index f9aa28e1..334f2a1e 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -160,64 +160,44 @@ class Loader include $file; } } - } elseif (AUTO_SCAN_PACKAGE && is_dir(VENDOR_PATH)) { - self::scanComposerPackage(VENDOR_PATH); } } // 扫描composer package - private static function scanComposerPackage($path) + public static function scanComposerPackage($path) { // 自动扫描下载Composer安装类库 - $dirs = scandir($path, 1); + $dirs = scandir($path, 1); + $namespace = $files = $classmap = []; foreach ($dirs as $dir) { if ('.' != $dir && '..' != $dir && is_dir($path . $dir) && is_file($path . $dir . DS . 'composer.json')) { // 解析Composer 包 - self::parseComposerPackage($path . $dir . DS); - } - } + $content = file_get_contents($path . $dir . DS . 'composer.json'); + $result = json_decode($content, true); - $content = " $path) { + $namespace[rtrim($ns, '\\')] = realpath($package . $path . DS . str_replace('\\', DS, $ns)) . DS; + } + } - if (!empty(self::$namespace)) { - $content .= "return " . var_export(self::$namespace, true) . ';' . PHP_EOL; - } - // 生成缓存 - file_put_contents(RUNTIME_PATH . 'autoload_composer.php', $content); - } + if (isset($autoload['psr-4'])) { + foreach ($autoload['psr-4'] as $ns => $path) { + $namespace[rtrim($ns, '\\')] = realpath($package . $path) . DS; + } + } - // 解析Composer Package - private static function parseComposerPackage($package) - { - $content = file_get_contents($package . 'composer.json'); - $result = json_decode($content, true); - - if (!empty($result['autoload'])) { - $autoload = $result['autoload']; - if (isset($autoload['psr-0'])) { - foreach ($autoload['psr-0'] as $ns => $path) { - self::$namespace[rtrim($ns, '\\')] = realpath($package . $path . DS . str_replace('\\', DS, $ns)) . DS; - } - } - - if (isset($autoload['psr-4'])) { - foreach ($autoload['psr-4'] as $ns => $path) { - self::$namespace[rtrim($ns, '\\')] = realpath($package . $path) . DS; - } - } - - if (isset($autoload['files'])) { - foreach ($autoload['files'] as $file) { - self::$load[] = realpath($package . $file); - require $package . $file; + if (isset($autoload['files'])) { + foreach ($autoload['files'] as $file) { + $files[] = realpath($package . $file); + } + } } } } + return ['namespace' => $namespace, 'files' => $files, 'classmap' => $classmap]; } // 注册composer自动加载