diff --git a/classmap.php b/classmap.php deleted file mode 100644 index 7f3bc7fb..00000000 --- a/classmap.php +++ /dev/null @@ -1,112 +0,0 @@ - -// +---------------------------------------------------------------------- - -/** - * ThinkPHP 类库映射定义 - */ -return [ - 'think\App' => CORE_PATH . 'App' . EXT, - 'think\Build' => CORE_PATH . 'Build' . EXT, - 'think\Cache' => CORE_PATH . 'Cache' . EXT, - 'think\cache\driver\Apc' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'Apc' . EXT, - 'think\cache\driver\File' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'File' . EXT, - 'think\cache\driver\Lite' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'Lite' . EXT, - 'think\cache\driver\Memcache' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'Memcache' . EXT, - 'think\cache\driver\Memcached' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'Memcached' . EXT, - 'think\cache\driver\Redis' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'Redis' . EXT, - 'think\cache\driver\Redisd' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'Redisd' . EXT, - 'think\cache\driver\Sae' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'Sae' . EXT, - 'think\cache\driver\Secache' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'Secache' . EXT, - 'think\cache\driver\Sqlite' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'Sqlite' . EXT, - 'think\cache\driver\Wincache' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'Wincache' . EXT, - 'think\cache\driver\Xcache' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'Xcache' . EXT, - 'think\Collection' => CORE_PATH . 'Collection' . EXT, - 'think\Config' => CORE_PATH . 'Config' . EXT, - 'think\config\Ini' => CORE_PATH . 'config' . DS . 'driver' . DS . 'Ini' . EXT, - 'think\config\Json' => CORE_PATH . 'config' . DS . 'driver' . DS . 'Json' . EXT, - 'think\config\Xml' => CORE_PATH . 'config' . DS . 'driver' . DS . 'Xml' . EXT, - 'think\Console' => CORE_PATH . 'Console' . EXT, - 'think\Controller' => CORE_PATH . 'Controller' . EXT, - 'think\controller\Hprose' => CORE_PATH . 'controller' . DS . 'Hprose' . EXT, - 'think\controller\Jsonrpc' => CORE_PATH . 'controller' . DS . 'Jsonrpc' . EXT, - 'think\controller\Rest' => CORE_PATH . 'controller' . DS . 'Rest' . EXT, - 'think\controller\Rpc' => CORE_PATH . 'controller' . DS . 'Rpc' . EXT, - 'think\controller\Yar' => CORE_PATH . 'controller' . DS . 'Yar' . EXT, - 'think\Cookie' => CORE_PATH . 'Cookie' . EXT, - 'think\Db' => CORE_PATH . 'Db' . EXT, - 'think\db\Connection' => CORE_PATH . 'db' . DS . 'Connection' . EXT, - 'think\db\connector\Mysql' => CORE_PATH . 'db' . DS . 'connector' . DS . 'Mysql' . EXT, - 'think\db\connector\Oracle' => CORE_PATH . 'db' . DS . 'connector' . DS . 'Oracle' . EXT, - 'think\db\connector\Pgsql' => CORE_PATH . 'db' . DS . 'connector' . DS . 'Pgsql' . EXT, - 'think\db\connector\Sqlite' => CORE_PATH . 'db' . DS . 'connector' . DS . 'Sqlite' . EXT, - 'think\db\connector\Sqlsrv' => CORE_PATH . 'db' . DS . 'connector' . DS . 'Sqlsrv' . EXT, - 'think\db\Builder' => CORE_PATH . 'db' . DS . 'Builder' . EXT, - 'think\db\builder\Mysql' => CORE_PATH . 'db' . DS . 'builder' . DS . 'Mysql' . EXT, - 'think\db\builder\Oracle' => CORE_PATH . 'db' . DS . 'builder' . DS . 'Oracle' . EXT, - 'think\db\builder\Pgsql' => CORE_PATH . 'db' . DS . 'builder' . DS . 'Pgsql' . EXT, - 'think\db\builder\Sqlite' => CORE_PATH . 'db' . DS . 'builder' . DS . 'Sqlite' . EXT, - 'think\db\builder\Sqlsrv' => CORE_PATH . 'db' . DS . 'builder' . DS . 'Sqlsrv' . EXT, - 'think\db\Query' => CORE_PATH . 'db' . DS . 'Query' . EXT, - 'think\Debug' => CORE_PATH . 'Debug' . EXT, - 'think\debug\Trace' => CORE_PATH . 'debug' . DS . 'Trace' . EXT, - 'think\debug\trace\Html' => CORE_PATH . 'debug' . DS . 'trace' . DS . 'Html' . EXT, - 'think\debug\trace\Console' => CORE_PATH . 'debug' . DS . 'trace' . DS . 'Console' . EXT, - 'think\Error' => CORE_PATH . 'Error' . EXT, - 'think\Exception' => CORE_PATH . 'Exception' . EXT, - 'think\exception\ClassNotFoundException' => CORE_PATH . 'exception' . DS . 'ClassNotFoundException' . EXT, - 'think\exception\DbException' => CORE_PATH . 'exception' . DS . 'DbException' . EXT, - 'think\exception\ErrorException' => CORE_PATH . 'exception' . DS . 'ErrorException' . EXT, - 'think\exception\Handle' => CORE_PATH . 'exception' . DS . 'Handle' . EXT, - 'think\exception\HttpException' => CORE_PATH . 'exception' . DS . 'HttpException' . EXT, - 'think\exception\HttpResponseException' => CORE_PATH . 'exception' . DS . 'HttpResponseException' . EXT, - 'think\exception\PDOException' => CORE_PATH . 'exception' . DS . 'PDOException' . EXT, - 'think\exception\TemplateNotFoundException' => CORE_PATH . 'exception' . DS . 'TemplateNotFoundException' . EXT, - 'think\exception\ThrowableError' => CORE_PATH . 'exception' . DS . 'ThrowableError' . EXT, - 'think\exception\ValidateException' => CORE_PATH . 'exception' . DS . 'ValidateException' . EXT, - 'think\File' => CORE_PATH . 'File' . EXT, - 'think\Hook' => CORE_PATH . 'Hook' . EXT, - 'think\Lang' => CORE_PATH . 'Lang' . EXT, - 'think\Log' => CORE_PATH . 'Log' . EXT, - 'think\log\driver\File' => CORE_PATH . 'log' . DS . 'driver' . DS . 'File' . EXT, - 'think\log\driver\Sae' => CORE_PATH . 'log' . DS . 'driver' . DS . 'Sae' . EXT, - 'think\log\driver\Socket' => CORE_PATH . 'log' . DS . 'driver' . DS . 'Socket' . EXT, - 'think\Model' => CORE_PATH . 'Model' . EXT, - 'think\model\Merge' => CORE_PATH . 'model' . DS . 'Merge' . EXT, - 'think\model\Pivot' => CORE_PATH . 'model' . DS . 'Pivot' . EXT, - 'think\model\Relation' => CORE_PATH . 'model' . DS . 'Relation' . EXT, - 'think\Process' => CORE_PATH . 'Process' . EXT, - 'think\Paginator' => CORE_PATH . 'Paginator' . EXT, - 'think\paginator\Collection' => CORE_PATH . 'paginator' . DS . 'Collection' . EXT, - 'think\paginator\driver\Bootstrap' => CORE_PATH . 'paginator' . DS . 'driver' . DS . 'Bootstrap' . EXT, - 'think\Request' => CORE_PATH . 'Request' . EXT, - 'think\Response' => CORE_PATH . 'Response' . EXT, - 'think\response\Json' => CORE_PATH . 'response' . DS . 'Json' . EXT, - 'think\response\Jsonp' => CORE_PATH . 'response' . DS . 'Jsonp' . EXT, - 'think\response\Redirect' => CORE_PATH . 'response' . DS . 'Redirect' . EXT, - 'think\response\View' => CORE_PATH . 'response' . DS . 'View' . EXT, - 'think\response\Xml' => CORE_PATH . 'response' . DS . 'Xml' . EXT, - 'think\Route' => CORE_PATH . 'Route' . EXT, - 'think\Session' => CORE_PATH . 'Session' . EXT, - 'think\session\driver\Memcache' => CORE_PATH . 'session' . DS . 'driver' . DS . 'Memcache' . EXT, - 'think\session\driver\Memcached' => CORE_PATH . 'session' . DS . 'driver' . DS . 'Memcached' . EXT, - 'think\session\driver\Redis' => CORE_PATH . 'session' . DS . 'driver' . DS . 'Redis' . EXT, - 'think\Template' => CORE_PATH . 'Template' . EXT, - 'think\template\Taglib' => CORE_PATH . 'template' . DS . 'Taglib' . EXT, - 'think\template\taglib\Cx' => CORE_PATH . 'template' . DS . 'taglib' . DS . 'Cx' . EXT, - 'think\template\driver\File' => CORE_PATH . 'template' . DS . 'driver' . DS . 'File' . EXT, - 'think\template\driver\Sae' => CORE_PATH . 'template' . DS . 'driver' . DS . 'Sae' . EXT, - 'think\Url' => CORE_PATH . 'Url' . EXT, - 'think\Validate' => CORE_PATH . 'Validate' . EXT, - 'think\View' => CORE_PATH . 'View' . EXT, - 'think\view\driver\Think' => CORE_PATH . 'view' . DS . 'driver' . DS . 'Think' . EXT, - 'think\view\driver\Php' => CORE_PATH . 'view' . DS . 'driver' . DS . 'Php' . EXT, - 'traits\controller\Jump' => TRAIT_PATH . 'controller' . DS . 'Jump' . EXT, -]; diff --git a/library/think/Console.php b/library/think/Console.php index a28845d4..e123cbd3 100644 --- a/library/think/Console.php +++ b/library/think/Console.php @@ -51,6 +51,7 @@ class Console "think\\console\\command\\Build", "think\\console\\command\\make\\Controller", "think\\console\\command\\make\\Model", + "think\\console\\command\\optimize\\Autoload" ]; public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') diff --git a/library/think/Loader.php b/library/think/Loader.php index 4c5e2293..a6332c70 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -55,7 +55,7 @@ class Loader return false; } - includeFile($file); + __include_file($file); return true; } } @@ -197,7 +197,9 @@ class Loader 'traits' => LIB_PATH . 'traits' . DS, ]); // 加载类库映射文件 - self::addClassMap(includeFile(THINK_PATH . 'classmap' . EXT)); + if (is_file(RUNTIME_PATH . 'classmap' . EXT)) { + self::addClassMap(__include_file(RUNTIME_PATH . 'classmap' . EXT)); + } // Composer自动加载支持 if (is_dir(VENDOR_PATH . 'composer')) { @@ -236,7 +238,7 @@ class Loader $includeFiles = require VENDOR_PATH . 'composer/autoload_files.php'; foreach ($includeFiles as $fileIdentifier => $file) { if (empty(self::$autoloadFiles[$fileIdentifier])) { - requireFile($file); + __require_file($file); self::$autoloadFiles[$fileIdentifier] = true; } } @@ -294,7 +296,7 @@ class Loader if (IS_WIN && pathinfo($filename, PATHINFO_FILENAME) != pathinfo(realpath($filename), PATHINFO_FILENAME)) { return false; } - includeFile($filename); + __include_file($filename); $_file[$key] = true; return true; } @@ -485,12 +487,12 @@ class Loader * @param $file * @return mixed */ -function includeFile($file) +function __include_file($file) { return include $file; } -function requireFile($file) +function __require_file($file) { return require $file; } \ No newline at end of file diff --git a/library/think/console/command/optimize/Autoload.php b/library/think/console/command/optimize/Autoload.php new file mode 100644 index 00000000..dfedaa5a --- /dev/null +++ b/library/think/console/command/optimize/Autoload.php @@ -0,0 +1,281 @@ + +// +---------------------------------------------------------------------- +namespace think\console\command\optimize; + +use think\App; +use think\console\command\Command; +use think\console\Input; +use think\console\Output; + +class Autoload extends Command +{ + /** @var Output */ + protected $output; + + protected function configure() + { + $this->setName('optimize:autoload') + ->setDescription('Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.'); + } + + protected function execute(Input $input, Output $output) + { + $this->output = $output; + + $classmapFile = << realpath(rtrim(APP_PATH)), + 'think\\' => LIB_PATH . 'think', + 'behavior\\' => LIB_PATH . 'behavior', + 'traits\\' => LIB_PATH . 'traits', + ]; + + krsort($namespacesToScan); + $classMap = []; + foreach ($namespacesToScan as $namespace => $dir) { + + if (!is_dir($dir)) { + continue; + } + + $namespaceFilter = $namespace === '' ? null : $namespace; + $classMap = $this->addClassMapCode($dir, $namespaceFilter, $classMap); + } + + ksort($classMap); + foreach ($classMap as $class => $code) { + $classmapFile .= ' ' . var_export($class, true) . ' => ' . $code; + } + $classmapFile .= "];\n"; + + if (!is_dir(RUNTIME_PATH)) { + @mkdir(RUNTIME_PATH, 0755, true); + } + + file_put_contents(RUNTIME_PATH . 'classmap' . EXT, $classmapFile); + + $output->writeln('Succeed!'); + } + + protected function addClassMapCode($dir, $namespace, $classMap) + { + foreach ($this->createMap($dir, $namespace) as $class => $path) { + + $pathCode = $this->getPathCode($path) . ",\n"; + // dump($pathCode); + if (!isset($classMap[$class])) { + $classMap[$class] = $pathCode; + } elseif ($this->io && $classMap[$class] !== $pathCode && !preg_match('{/(test|fixture|example|stub)s?/}i', strtr($classMap[$class] . ' ' . $path, '\\', '/'))) { + $this->output->writeln( + 'Warning: Ambiguous class resolution, "' . $class . '"' . + ' was found in both "' . str_replace(["',\n"], [ + '' + ], $classMap[$class]) . '" and "' . $path . '", the first will be used.' + ); + } + } + return $classMap; + } + + protected function getPathCode($path) + { + + $baseDir = ''; + $appPath = $this->normalizePath(realpath(APP_PATH)); + $libPath = $this->normalizePath(realpath(LIB_PATH)); + $path = $this->normalizePath($path); + + if (strpos($path, $libPath . '/') === 0) { + $path = substr($path, strlen(LIB_PATH)); + $baseDir = 'LIB_PATH'; + } elseif (strpos($path, $appPath . '/') === 0) { + $path = substr($path, strlen($appPath) + 1); + $baseDir = 'APP_PATH'; + } + + if ($path !== false) { + $baseDir .= " . "; + } + + return $baseDir . (($path !== false) ? var_export($path, true) : ""); + } + + + protected function normalizePath($path) + { + $parts = []; + $path = strtr($path, '\\', '/'); + $prefix = ''; + $absolute = false; + + if (preg_match('{^([0-9a-z]+:(?://(?:[a-z]:)?)?)}i', $path, $match)) { + $prefix = $match[1]; + $path = substr($path, strlen($prefix)); + } + + if (substr($path, 0, 1) === '/') { + $absolute = true; + $path = substr($path, 1); + } + + $up = false; + foreach (explode('/', $path) as $chunk) { + if ('..' === $chunk && ($absolute || $up)) { + array_pop($parts); + $up = !(empty($parts) || '..' === end($parts)); + } elseif ('.' !== $chunk && '' !== $chunk) { + $parts[] = $chunk; + $up = '..' !== $chunk; + } + } + + return $prefix . ($absolute ? '/' : '') . implode('/', $parts); + } + + protected function createMap($path, $namespace = null) + { + if (is_string($path)) { + if (is_file($path)) { + $path = [new \SplFileInfo($path)]; + } elseif (is_dir($path)) { + + $objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST); + + $path = []; + + /** @var \SplFileInfo $object */ + foreach ($objects as $object) { + if ($object->isFile() && $object->getExtension() == 'php') { + $path[] = $object; + } + } + } else { + throw new \RuntimeException( + 'Could not scan for classes inside "' . $path . + '" which does not appear to be a file nor a folder' + ); + } + } + + $map = []; + + /** @var \SplFileInfo $file */ + foreach ($path as $file) { + $filePath = $file->getRealPath(); + + if (pathinfo($filePath, PATHINFO_EXTENSION) != 'php') { + continue; + } + + $classes = $this->findClasses($filePath); + + foreach ($classes as $class) { + if (null !== $namespace && 0 !== strpos($class, $namespace)) { + continue; + } + + if (!isset($map[$class])) { + $map[$class] = $filePath; + } elseif ($map[$class] !== $filePath && !preg_match('{/(test|fixture|example|stub)s?/}i', strtr($map[$class] . ' ' . $filePath, '\\', '/'))) { + $this->output->writeln( + 'Warning: Ambiguous class resolution, "' . $class . '"' . + ' was found in both "' . $map[$class] . '" and "' . $filePath . '", the first will be used.' + ); + } + } + } + + return $map; + } + + + protected function findClasses($path) + { + $extraTypes = '|trait'; + + $contents = @php_strip_whitespace($path); + if (!$contents) { + if (!file_exists($path)) { + $message = 'File at "%s" does not exist, check your classmap definitions'; + } elseif (!is_readable($path)) { + $message = 'File at "%s" is not readable, check its permissions'; + } elseif ('' === trim(file_get_contents($path))) { + return []; + } else { + $message = 'File at "%s" could not be parsed as PHP, it may be binary or corrupted'; + } + $error = error_get_last(); + if (isset($error['message'])) { + $message .= PHP_EOL . 'The following message may be helpful:' . PHP_EOL . $error['message']; + } + throw new \RuntimeException(sprintf($message, $path)); + } + + if (!preg_match('{\b(?:class|interface' . $extraTypes . ')\s}i', $contents)) { + return []; + } + + // strip heredocs/nowdocs + $contents = preg_replace('{<<<\s*(\'?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\2(?=\r\n|\n|\r|;)}s', 'null', $contents); + // strip strings + $contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents); + // strip leading non-php code if needed + if (substr($contents, 0, 2) !== '.+<\?}s', '?>'); + if (false !== $pos && false === strpos(substr($contents, $pos), '])(?Pclass|interface' . $extraTypes . ') \s++ (?P[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+) + | \b(?])(?Pnamespace) (?P\s++[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\s*+\\\\\s*+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+)? \s*+ [\{;] + ) + }ix', $contents, $matches); + + $classes = []; + $namespace = ''; + + for ($i = 0, $len = count($matches['type']); $i < $len; $i++) { + if (!empty($matches['ns'][$i])) { + $namespace = str_replace([' ', "\t", "\r", "\n"], '', $matches['nsname'][$i]) . '\\'; + } else { + $name = $matches['name'][$i]; + if ($name[0] === ':') { + $name = 'xhp' . substr(str_replace(['-', ':'], ['_', '__'], $name), 1); + } elseif ($matches['type'][$i] === 'enum') { + $name = rtrim($name, ':'); + } + $classes[] = ltrim($namespace . $name, '\\'); + } + } + + return $classes; + } + +} \ No newline at end of file