feat: 本次提交更新内容如下

php有问题,覆盖下
This commit is contained in:
笔记本里的永平
2025-07-07 14:52:56 +08:00
parent c29fbd1bbb
commit c9a3aaab58
338 changed files with 70315 additions and 12737 deletions

View File

@@ -10,15 +10,13 @@
// +----------------------------------------------------------------------
namespace think\console\command\optimize;
use think\App;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Container;
class Autoload extends Command
{
protected function configure()
{
$this->setName('optimize:autoload')
@@ -37,20 +35,14 @@ class Autoload extends Command
return [
EOF;
$app = Container::get('app');
$namespacesToScan = [
App::$namespace . '\\' => realpath(rtrim(APP_PATH)),
'think\\' => LIB_PATH . 'think',
'behavior\\' => LIB_PATH . 'behavior',
'traits\\' => LIB_PATH . 'traits',
'' => realpath(rtrim(EXTEND_PATH)),
$app->getNamespace() . '\\' => realpath(rtrim($app->getAppPath())),
'think\\' => $app->getThinkPath() . 'library/think',
'traits\\' => $app->getThinkPath() . 'library/traits',
'' => realpath(rtrim($app->getRootPath() . 'extend')),
];
$root_namespace = Config::get('root_namespace');
foreach ($root_namespace as $namespace => $dir) {
$namespacesToScan[$namespace . '\\'] = realpath($dir);
}
krsort($namespacesToScan);
$classMap = [];
foreach ($namespacesToScan as $namespace => $dir) {
@@ -59,7 +51,7 @@ EOF;
continue;
}
$namespaceFilter = $namespace === '' ? null : $namespace;
$namespaceFilter = '' === $namespace ? null : $namespace;
$classMap = $this->addClassMapCode($dir, $namespaceFilter, $classMap);
}
@@ -68,12 +60,12 @@ EOF;
$classmapFile .= ' ' . var_export($class, true) . ' => ' . $code;
}
$classmapFile .= "];\n";
if (!is_dir(RUNTIME_PATH)) {
@mkdir(RUNTIME_PATH, 0755, true);
$runtimePath = $app->getRuntimePath();
if (!is_dir($runtimePath)) {
@mkdir($runtimePath, 0755, true);
}
file_put_contents(RUNTIME_PATH . 'classmap' . EXT, $classmapFile);
file_put_contents($runtimePath . 'classmap.php', $classmapFile);
$output->writeln('<info>Succeed!</info>');
}
@@ -100,40 +92,33 @@ EOF;
protected function getPathCode($path)
{
$baseDir = '';
$libPath = $this->normalizePath(realpath(LIB_PATH));
$appPath = $this->normalizePath(realpath(APP_PATH));
$extendPath = $this->normalizePath(realpath(EXTEND_PATH));
$rootPath = $this->normalizePath(realpath(ROOT_PATH));
$app = Container::get('app');
$appPath = $this->normalizePath(realpath($app->getAppPath()));
$libPath = $this->normalizePath(realpath($app->getThinkPath() . 'library'));
$extendPath = $this->normalizePath(realpath($app->getRootPath() . 'extend'));
$path = $this->normalizePath($path);
if ($libPath !== null && strpos($path, $libPath . '/') === 0) {
$path = substr($path, strlen(LIB_PATH));
$baseDir = 'LIB_PATH';
} elseif ($appPath !== null && strpos($path, $appPath . '/') === 0) {
if (strpos($path, $libPath . '/') === 0) {
$path = substr($path, strlen($app->getThinkPath() . 'library'));
$baseDir = "'" . $libPath . "/'";
} elseif (strpos($path, $appPath . '/') === 0) {
$path = substr($path, strlen($appPath) + 1);
$baseDir = 'APP_PATH';
} elseif ($extendPath !== null && strpos($path, $extendPath . '/') === 0) {
$baseDir = "'" . $appPath . "/'";
} elseif (strpos($path, $extendPath . '/') === 0) {
$path = substr($path, strlen($extendPath) + 1);
$baseDir = 'EXTEND_PATH';
} elseif ($rootPath !== null && strpos($path, $rootPath . '/') === 0) {
$path = substr($path, strlen($rootPath) + 1);
$baseDir = 'ROOT_PATH';
$baseDir = "'" . $extendPath . "/'";
}
if ($path !== false) {
if (false !== $path) {
$baseDir .= " . ";
}
return $baseDir . (($path !== false) ? var_export($path, true) : "");
return $baseDir . ((false !== $path) ? var_export($path, true) : "");
}
protected function normalizePath($path)
{
if ($path === false) {
return;
}
$parts = [];
$path = strtr($path, '\\', '/');
$prefix = '';
@@ -252,7 +237,7 @@ EOF;
// strip leading non-php code if needed
if (substr($contents, 0, 2) !== '<?') {
$contents = preg_replace('{^.+?<\?}s', '<?', $contents, 1, $replacements);
if ($replacements === 0) {
if (0 === $replacements) {
return [];
}
}
@@ -279,9 +264,9 @@ EOF;
$namespace = str_replace([' ', "\t", "\r", "\n"], '', $matches['nsname'][$i]) . '\\';
} else {
$name = $matches['name'][$i];
if ($name[0] === ':') {
if (':' === $name[0]) {
$name = 'xhp' . substr(str_replace(['-', ':'], ['_', '__'], $name), 1);
} elseif ($matches['type'][$i] === 'enum') {
} elseif ('enum' === $matches['type'][$i]) {
$name = rtrim($name, ':');
}
$classes[] = ltrim($namespace . $name, '\\');

View File

@@ -2,7 +2,7 @@
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// | Copyright (c) 2006-2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
@@ -10,17 +10,15 @@
// +----------------------------------------------------------------------
namespace think\console\command\optimize;
use think\Config as ThinkConfig;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\Container;
use think\facade\App;
class Config extends Command
{
/** @var Output */
protected $output;
protected function configure()
{
$this->setName('optimize:config')
@@ -31,63 +29,79 @@ class Config extends Command
protected function execute(Input $input, Output $output)
{
if ($input->getArgument('module')) {
$module = $input->getArgument('module') . DS;
$module = $input->getArgument('module') . DIRECTORY_SEPARATOR;
} else {
$module = '';
}
$content = '<?php ' . PHP_EOL . $this->buildCacheContent($module);
if (!is_dir(RUNTIME_PATH . $module)) {
@mkdir(RUNTIME_PATH . $module, 0755, true);
$content = '<?php ' . PHP_EOL . $this->buildCacheContent($module);
$runtimePath = App::getRuntimePath();
if (!is_dir($runtimePath . $module)) {
@mkdir($runtimePath . $module, 0755, true);
}
file_put_contents(RUNTIME_PATH . $module . 'init' . EXT, $content);
file_put_contents($runtimePath . $module . 'init.php', $content);
$output->writeln('<info>Succeed!</info>');
}
protected function buildCacheContent($module)
{
$content = '';
$path = realpath(APP_PATH . $module) . DS;
$content = '// This cache file is automatically generated at:' . date('Y-m-d H:i:s') . PHP_EOL;
$path = realpath(App::getAppPath() . $module) . DIRECTORY_SEPARATOR;
if ($module) {
// 加载模块配置
$config = ThinkConfig::load(CONF_PATH . $module . 'config' . CONF_EXT);
$configPath = is_dir($path . 'config') ? $path . 'config' : App::getConfigPath() . $module;
} else {
$configPath = App::getConfigPath();
}
$ext = App::getConfigExt();
$config = Container::get('config');
// 读取数据库配置文件
$filename = CONF_PATH . $module . 'database' . CONF_EXT;
ThinkConfig::load($filename, 'database');
$files = is_dir($configPath) ? scandir($configPath) : [];
// 加载应用状态配置
if ($config['app_status']) {
$config = ThinkConfig::load(CONF_PATH . $module . $config['app_status'] . CONF_EXT);
}
// 读取扩展配置文件
if (is_dir(CONF_PATH . $module . 'extra')) {
$dir = CONF_PATH . $module . 'extra';
$files = scandir($dir);
foreach ($files as $file) {
if (strpos($file, CONF_EXT)) {
$filename = $dir . DS . $file;
ThinkConfig::load($filename, pathinfo($file, PATHINFO_FILENAME));
}
}
foreach ($files as $file) {
if ('.' . pathinfo($file, PATHINFO_EXTENSION) === $ext) {
$filename = $configPath . DIRECTORY_SEPARATOR . $file;
$config->load($filename, pathinfo($file, PATHINFO_FILENAME));
}
}
// 加载行为扩展文件
if (is_file(CONF_PATH . $module . 'tags' . EXT)) {
$content .= '\think\Hook::import(' . (var_export(include CONF_PATH . $module . 'tags' . EXT, true)) . ');' . PHP_EOL;
if (is_file($path . 'tags.php')) {
$tags = include $path . 'tags.php';
if (is_array($tags)) {
$content .= PHP_EOL . '\think\facade\Hook::import(' . (var_export($tags, true)) . ');' . PHP_EOL;
}
}
// 加载公共文件
if (is_file($path . 'common' . EXT)) {
$content .= substr(php_strip_whitespace($path . 'common' . EXT), 5) . PHP_EOL;
if (is_file($path . 'common.php')) {
$common = substr(php_strip_whitespace($path . 'common.php'), 6);
if ($common) {
$content .= PHP_EOL . $common . PHP_EOL;
}
}
$content .= '\think\Config::set(' . var_export(ThinkConfig::get(), true) . ');';
if ('' == $module) {
$content .= PHP_EOL . substr(php_strip_whitespace(App::getThinkPath() . 'helper.php'), 6) . PHP_EOL;
if (is_file($path . 'middleware.php')) {
$middleware = include $path . 'middleware.php';
if (is_array($middleware)) {
$content .= PHP_EOL . '\think\Container::get("middleware")->import(' . var_export($middleware, true) . ');' . PHP_EOL;
}
}
}
if (is_file($path . 'provider.php')) {
$provider = include $path . 'provider.php';
if (is_array($provider)) {
$content .= PHP_EOL . '\think\Container::getInstance()->bindTo(' . var_export($provider, true) . ');' . PHP_EOL;
}
}
$content .= PHP_EOL . '\think\facade\Config::set(' . var_export($config->get(), true) . ');' . PHP_EOL;
return $content;
}
}

View File

@@ -6,19 +6,17 @@
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\console\command\optimize;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Container;
class Route extends Command
{
/** @var Output */
protected $output;
protected function configure()
{
$this->setName('optimize:route')
@@ -27,49 +25,42 @@ class Route extends Command
protected function execute(Input $input, Output $output)
{
if (!is_dir(RUNTIME_PATH)) {
@mkdir(RUNTIME_PATH, 0755, true);
$filename = Container::get('app')->getRuntimePath() . 'route.php';
if (is_file($filename)) {
unlink($filename);
}
file_put_contents(RUNTIME_PATH . 'route.php', $this->buildRouteCache());
file_put_contents($filename, $this->buildRouteCache());
$output->writeln('<info>Succeed!</info>');
}
protected function buildRouteCache()
{
$files = \think\Config::get('route_config_file');
Container::get('route')->setName([]);
Container::get('route')->setTestMode(true);
// 路由检测
$path = Container::get('app')->getRoutePath();
$files = is_dir($path) ? scandir($path) : [];
foreach ($files as $file) {
if (is_file(CONF_PATH . $file . CONF_EXT)) {
$config = include CONF_PATH . $file . CONF_EXT;
if (is_array($config)) {
\think\Route::import($config);
if (strpos($file, '.php')) {
$filename = $path . DIRECTORY_SEPARATOR . $file;
// 导入路由配置
$rules = include $filename;
if (is_array($rules)) {
Container::get('route')->import($rules);
}
}
}
$rules = \think\Route::rules(true);
array_walk_recursive($rules, [$this, 'buildClosure']);
if (Container::get('config')->get('route_annotation')) {
$suffix = Container::get('config')->get('controller_suffix') || Container::get('config')->get('class_suffix');
include Container::get('build')->buildRoute($suffix);
}
$content = '<?php ' . PHP_EOL . 'return ';
$content .= var_export($rules, true) . ';';
$content = str_replace(['\'[__start__', '__end__]\''], '', stripcslashes($content));
$content .= var_export(Container::get('route')->getName(), true) . ';';
return $content;
}
protected function buildClosure(&$value)
{
if ($value instanceof \Closure) {
$reflection = new \ReflectionFunction($value);
$startLine = $reflection->getStartLine();
$endLine = $reflection->getEndLine();
$file = $reflection->getFileName();
$item = file($file);
$content = '';
for ($i = $startLine - 1; $i <= $endLine - 1; $i++) {
$content .= $item[$i];
}
$start = strpos($content, 'function');
$end = strrpos($content, '}');
$value = '[__start__' . substr($content, $start, $end - $start + 1) . '__end__]';
}
}
}

View File

@@ -10,22 +10,18 @@
// +----------------------------------------------------------------------
namespace think\console\command\optimize;
use think\App;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\facade\App;
class Schema extends Command
{
/** @var Output */
protected $output;
protected function configure()
{
$this->setName('optimize:schema')
->addOption('config', null, Option::VALUE_REQUIRED, 'db config .')
->addOption('db', null, Option::VALUE_REQUIRED, 'db name .')
->addOption('table', null, Option::VALUE_REQUIRED, 'table name .')
->addOption('module', null, Option::VALUE_REQUIRED, 'module name .')
@@ -34,56 +30,58 @@ class Schema extends Command
protected function execute(Input $input, Output $output)
{
if (!is_dir(RUNTIME_PATH . 'schema')) {
@mkdir(RUNTIME_PATH . 'schema', 0755, true);
}
$config = [];
if ($input->hasOption('config')) {
$config = $input->getOption('config');
if (!is_dir(App::getRuntimePath() . 'schema')) {
@mkdir(App::getRuntimePath() . 'schema', 0755, true);
}
if ($input->hasOption('module')) {
$module = $input->getOption('module');
// 读取模型
$path = APP_PATH . $module . DS . 'model';
$list = is_dir($path) ? scandir($path) : [];
$app = App::$namespace;
$path = App::getAppPath() . $module . DIRECTORY_SEPARATOR . 'model';
$list = is_dir($path) ? scandir($path) : [];
$namespace = App::getNamespace();
foreach ($list as $file) {
if (0 === strpos($file, '.')) {
continue;
}
$class = '\\' . $app . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
$class = '\\' . $namespace . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
$this->buildModelSchema($class);
}
$output->writeln('<info>Succeed!</info>');
return;
} elseif ($input->hasOption('table')) {
$table = $input->getOption('table');
if (!strpos($table, '.')) {
$dbName = Db::connect($config)->getConfig('database');
if (false === strpos($table, '.')) {
$dbName = Db::getConfig('database');
}
$tables[] = $table;
} elseif ($input->hasOption('db')) {
$dbName = $input->getOption('db');
$tables = Db::connect($config)->getTables($dbName);
} elseif (!\think\Config::get('app_multi_module')) {
$app = App::$namespace;
$path = APP_PATH . 'model';
$list = is_dir($path) ? scandir($path) : [];
$tables = Db::getConnection()->getTables($dbName);
} elseif (!\think\facade\Config::get('app_multi_module')) {
$namespace = App::getNamespace();
$path = App::getAppPath() . 'model';
$list = is_dir($path) ? scandir($path) : [];
foreach ($list as $file) {
if (0 === strpos($file, '.')) {
continue;
}
$class = '\\' . $app . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
$class = '\\' . $namespace . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
$this->buildModelSchema($class);
}
$output->writeln('<info>Succeed!</info>');
return;
} else {
$tables = Db::connect($config)->getTables();
$tables = Db::getConnection()->getTables();
}
$db = isset($dbName) ? $dbName . '.' : '';
$this->buildDataBaseSchema($tables, $db, $config);
$this->buildDataBaseSchema($tables, $db);
$output->writeln('<info>Succeed!</info>');
}
@@ -97,22 +95,24 @@ class Schema extends Command
$content = '<?php ' . PHP_EOL . 'return ';
$info = $class::getConnection()->getFields($table);
$content .= var_export($info, true) . ';';
file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . '.' . $table . EXT, $content);
file_put_contents(App::getRuntimePath() . 'schema' . DIRECTORY_SEPARATOR . $dbName . '.' . $table . '.php', $content);
}
}
protected function buildDataBaseSchema($tables, $db, $config)
protected function buildDataBaseSchema($tables, $db)
{
if ('' == $db) {
$dbName = Db::connect($config)->getConfig('database') . '.';
$dbName = Db::getConfig('database') . '.';
} else {
$dbName = $db;
}
foreach ($tables as $table) {
$content = '<?php ' . PHP_EOL . 'return ';
$info = Db::connect($config)->getFields($db . $table);
$info = Db::getConnection()->getFields($db . $table);
$content .= var_export($info, true) . ';';
file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . $table . EXT, $content);
file_put_contents(App::getRuntimePath() . 'schema' . DIRECTORY_SEPARATOR . $dbName . $table . '.php', $content);
}
}
}