初始化代码

This commit is contained in:
2025-12-22 14:34:25 +08:00
parent c2c5ae2fdd
commit a77dbc743f
1510 changed files with 213008 additions and 0 deletions

View File

@@ -0,0 +1,268 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\app;
use Closure;
use think\App;
use think\exception\HttpException;
use think\Request;
use think\Response;
/**
* 多应用模式支持
*/
class MultiApp
{
/** @var App */
protected $app;
/**
* 应用名称
* @var string
*/
protected $name;
/**
* 应用名称
* @var string
*/
protected $appName;
/**
* 应用路径
* @var string
*/
protected $path;
public function __construct(App $app)
{
$this->app = $app;
$this->name = $this->app->http->getName();
$this->path = $this->app->http->getPath();
}
/**
* 多应用解析
* @access public
* @param Request $request
* @param Closure $next
* @return Response
*/
public function handle($request, Closure $next)
{
if (!$this->parseMultiApp()) {
return $next($request);
}
return $this->app->middleware->pipeline('app')
->send($request)
->then(function ($request) use ($next) {
return $next($request);
});
}
/**
* 获取路由目录
* @access protected
* @return string
*/
protected function getRoutePath(): string
{
if (is_dir($this->app->getAppPath() . 'route')) {
return $this->app->getAppPath() . 'route' . DIRECTORY_SEPARATOR;
}
return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . $this->appName . DIRECTORY_SEPARATOR;
}
/**
* 解析多应用
* @return bool
*/
protected function parseMultiApp(): bool
{
$scriptName = $this->getScriptName();
$defaultApp = $this->app->config->get('app.default_app') ?: 'index';
if ($this->name || ($scriptName && !in_array($scriptName, ['index', 'router', 'think']))) {
$appName = $this->name ?: $scriptName;
$this->app->http->setBind();
} else {
// 自动多应用识别
$this->app->http->setBind(false);
$appName = null;
$this->appName = '';
$bind = $this->app->config->get('app.domain_bind', []);
if (!empty($bind)) {
// 获取当前子域名
$subDomain = $this->app->request->subDomain();
$domain = $this->app->request->host(true);
if (isset($bind[$domain])) {
$appName = $bind[$domain];
$this->app->http->setBind();
} elseif (isset($bind[$subDomain])) {
$appName = $bind[$subDomain];
$this->app->http->setBind();
} elseif (isset($bind['*'])) {
$appName = $bind['*'];
$this->app->http->setBind();
}
}
if (!$this->app->http->isBind()) {
$path = $this->app->request->pathinfo();
$map = $this->app->config->get('app.app_map', []);
$deny = $this->app->config->get('app.deny_app_list', []);
$name = current(explode('/', $path));
if (strpos($name, '.')) {
$name = strstr($name, '.', true);
}
if (isset($map[$name])) {
if ($map[$name] instanceof Closure) {
$result = call_user_func_array($map[$name], [$this->app]);
$appName = $result ?: $name;
} else {
$appName = $map[$name];
}
} elseif ($name && (false !== array_search($name, $map) || in_array($name, $deny))) {
throw new HttpException(404, 'app not exists:' . $name);
} elseif ($name && isset($map['*'])) {
$appName = $map['*'];
} else {
$appName = $name ?: $defaultApp;
$appPath = $this->path ?: $this->app->getBasePath() . $appName . DIRECTORY_SEPARATOR;
if (!is_dir($appPath)) {
$express = $this->app->config->get('app.app_express', false);
if ($express) {
$this->setApp($defaultApp);
return true;
} else {
return false;
}
}
}
if ($name) {
$this->app->request->setRoot('/' . $name);
$this->app->request->setPathinfo(strpos($path, '/') ? ltrim(strstr($path, '/'), '/') : '');
}
}
}
$this->setApp($appName ?: $defaultApp);
return true;
}
/**
* 获取当前运行入口名称
* @access protected
* @codeCoverageIgnore
* @return string
*/
protected function getScriptName(): string
{
if (isset($_SERVER['SCRIPT_FILENAME'])) {
$file = $_SERVER['SCRIPT_FILENAME'];
} elseif (isset($_SERVER['argv'][0])) {
$file = realpath($_SERVER['argv'][0]);
}
return isset($file) ? pathinfo($file, PATHINFO_FILENAME) : '';
}
/**
* 设置应用
* @param string $appName
*/
protected function setApp(string $appName): void
{
$this->appName = $appName;
$this->app->http->name($appName);
$appPath = $this->path ?: $this->app->getBasePath() . $appName . DIRECTORY_SEPARATOR;
$this->app->setAppPath($appPath);
// 设置应用命名空间
$this->app->setNamespace($this->app->config->get('app.app_namespace') ?: 'app\\' . $appName);
if (is_dir($appPath)) {
$this->app->setRuntimePath($this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . $appName . DIRECTORY_SEPARATOR);
$this->app->http->setRoutePath($this->getRoutePath());
//加载应用
$this->loadApp($appName, $appPath);
}
}
/**
* 加载应用文件
* @param string $appName 应用名
* @return void
*/
protected function loadApp(string $appName, string $appPath): void
{
if (is_file($appPath . 'common.php')) {
include_once $appPath . 'common.php';
}
$configPath = $this->app->getConfigPath();
$files = [];
if (is_dir($appPath . 'config')) {
$files = array_merge($files, glob($appPath . 'config' . DIRECTORY_SEPARATOR . '*' . $this->app->getConfigExt()));
} elseif (is_dir($configPath . $appName)) {
$files = array_merge($files, glob($configPath . $appName . DIRECTORY_SEPARATOR . '*' . $this->app->getConfigExt()));
}
foreach ($files as $file) {
$this->app->config->load($file, pathinfo($file, PATHINFO_FILENAME));
}
if (is_file($appPath . 'event.php')) {
$this->app->loadEvent(include $appPath . 'event.php');
}
if (is_file($appPath . 'middleware.php')) {
$this->app->middleware->import(include $appPath . 'middleware.php', 'app');
}
if (is_file($appPath . 'provider.php')) {
$this->app->bind(include $appPath . 'provider.php');
}
// 加载应用默认语言包
$this->app->loadLangPack($this->app->lang->defaultLangSet());
}
}

View File

@@ -0,0 +1,29 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\app;
use think\Service as BaseService;
class Service extends BaseService
{
public function register()
{
$this->app->middleware->unshift(MultiApp::class);
$this->commands([
'build' => command\Build::class,
]);
$this->app->bind([
'think\route\Url' => Url::class,
]);
}
}

View File

@@ -0,0 +1,224 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\app;
use think\App;
use think\Route;
use think\route\Url as UrlBuild;
/**
* 路由地址生成
*/
class Url extends UrlBuild
{
/**
* 直接解析URL地址
* @access protected
* @param string $url URL
* @param string|bool $domain Domain
* @return string
*/
protected function parseUrl(string $url, &$domain): string
{
$request = $this->app->request;
if (0 === strpos($url, '/')) {
// 直接作为路由地址解析
$url = substr($url, 1);
} elseif (false !== strpos($url, '\\')) {
// 解析到类
$url = ltrim(str_replace('\\', '/', $url), '/');
} elseif (0 === strpos($url, '@')) {
// 解析到控制器
$url = substr($url, 1);
} elseif ('' === $url) {
$url = $this->app->http->getName() . '/' . $request->controller() . '/' . $request->action();
} else {
// 解析到 应用/控制器/操作
$controller = $request->controller();
$app = $this->app->http->getName();
$path = explode('/', $url);
$action = array_pop($path);
$controller = empty($path) ? $controller : array_pop($path);
$app = empty($path) ? $app : array_pop($path);
$url = $controller . '/' . $action;
$bind = $this->app->config->get('app.domain_bind', []);
if ($key = array_search($app, $bind)) {
$domain = is_bool($domain) ? $key : $domain;
} else {
$map = $this->app->config->get('app.app_map', []);
if ($key = array_search($app, $map)) {
$url = $key . '/' . $url;
} else {
$url = $app . '/' . $url;
}
}
}
return $url;
}
public function build()
{
// 解析URL
$url = $this->url;
$suffix = $this->suffix;
$domain = $this->domain;
$request = $this->app->request;
$vars = $this->vars;
if (0 === strpos($url, '[') && $pos = strpos($url, ']')) {
// [name] 表示使用路由命名标识生成URL
$name = substr($url, 1, $pos - 1);
$url = 'name' . substr($url, $pos + 1);
}
if (false === strpos($url, '://') && 0 !== strpos($url, '/')) {
$info = parse_url($url);
$url = !empty($info['path']) ? $info['path'] : '';
if (isset($info['fragment'])) {
// 解析锚点
$anchor = $info['fragment'];
if (false !== strpos($anchor, '?')) {
// 解析参数
list($anchor, $info['query']) = explode('?', $anchor, 2);
}
if (false !== strpos($anchor, '@')) {
// 解析域名
list($anchor, $domain) = explode('@', $anchor, 2);
}
} elseif (strpos($url, '@') && false === strpos($url, '\\')) {
// 解析域名
list($url, $domain) = explode('@', $url, 2);
}
}
if ($url) {
$checkName = isset($name) ? $name : $url . (isset($info['query']) ? '?' . $info['query'] : '');
$checkDomain = $domain && is_string($domain) ? $domain : null;
$rule = $this->route->getName($checkName, $checkDomain);
if (empty($rule) && isset($info['query'])) {
$rule = $this->route->getName($url, $checkDomain);
// 解析地址里面参数 合并到vars
parse_str($info['query'], $params);
$vars = array_merge($params, $vars);
unset($info['query']);
}
}
if (!empty($rule) && $match = $this->getRuleUrl($rule, $vars, $domain)) {
// 匹配路由命名标识
$url = $match[0];
if ($domain && !empty($match[1])) {
$domain = $match[1];
}
if (!is_null($match[2])) {
$suffix = $match[2];
}
if (!$this->app->http->isBind()) {
$url = $this->app->http->getName() . '/' . $url;
}
} elseif (!empty($rule) && isset($name)) {
throw new \InvalidArgumentException('route name not exists:' . $name);
} else {
// 检测URL绑定
$bind = $this->route->getDomainBind($domain && is_string($domain) ? $domain : null);
if ($bind && 0 === strpos($url, $bind)) {
$url = substr($url, strlen($bind) + 1);
} else {
$binds = $this->route->getBind();
foreach ($binds as $key => $val) {
if (is_string($val) && 0 === strpos($url, $val) && substr_count($val, '/') > 1) {
$url = substr($url, strlen($val) + 1);
$domain = $key;
break;
}
}
}
// 路由标识不存在 直接解析
$url = $this->parseUrl($url, $domain);
if (isset($info['query'])) {
// 解析地址里面参数 合并到vars
parse_str($info['query'], $params);
$vars = array_merge($params, $vars);
}
}
// 还原URL分隔符
$depr = $this->route->config('pathinfo_depr');
$url = str_replace('/', $depr, $url);
$file = $request->baseFile();
if ($file && 0 !== strpos($request->url(), $file)) {
$file = str_replace('\\', '/', dirname($file));
}
$url = rtrim($file, '/') . '/' . ltrim($url, '/');
// URL后缀
if ('/' == substr($url, -1) || '' == $url) {
$suffix = '';
} else {
$suffix = $this->parseSuffix($suffix);
}
// 锚点
$anchor = !empty($anchor) ? '#' . $anchor : '';
// 参数组装
if (!empty($vars)) {
// 添加参数
if ($this->route->config('url_common_param')) {
$vars = http_build_query($vars);
$url .= $suffix . '?' . $vars . $anchor;
} else {
foreach ($vars as $var => $val) {
$val = (string) $val;
if ('' !== $val) {
$url .= $depr . $var . $depr . urlencode($val);
}
}
$url .= $suffix . $anchor;
}
} else {
$url .= $suffix . $anchor;
}
// 检测域名
$domain = $this->parseDomain($url, $domain);
// URL组装
return $domain . rtrim($this->root, '/') . '/' . ltrim($url, '/');
}
}

View File

@@ -0,0 +1,180 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
class Build extends Command
{
/**
* 应用基础目录
* @var string
*/
protected $basePath;
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('build')
->addArgument('app', Argument::OPTIONAL, 'app name .')
->setDescription('Build App Dirs');
}
protected function execute(Input $input, Output $output)
{
$this->basePath = $this->app->getBasePath();
$app = $input->getArgument('app') ?: '';
if (is_file($this->basePath . 'build.php')) {
$list = include $this->basePath . 'build.php';
} else {
$list = [
'__dir__' => ['controller', 'model', 'view'],
];
}
$this->buildApp($app, $list);
$output->writeln("<info>Successed</info>");
}
/**
* 创建应用
* @access protected
* @param string $app 应用名
* @param array $list 目录结构
* @return void
*/
protected function buildApp(string $app, array $list = []): void
{
if (!is_dir($this->basePath . $app)) {
// 创建应用目录
mkdir($this->basePath . $app);
}
$appPath = $this->basePath . ($app ? $app . DIRECTORY_SEPARATOR : '');
$namespace = 'app' . ($app ? '\\' . $app : '');
// 创建配置文件和公共文件
$this->buildCommon($app);
// 创建模块的默认页面
$this->buildHello($app, $namespace);
foreach ($list as $path => $file) {
if ('__dir__' == $path) {
// 生成子目录
foreach ($file as $dir) {
$this->checkDirBuild($appPath . $dir);
}
} elseif ('__file__' == $path) {
// 生成(空白)文件
foreach ($file as $name) {
if (!is_file($appPath . $name)) {
file_put_contents($appPath . $name, 'php' == pathinfo($name, PATHINFO_EXTENSION) ? '<?php' . PHP_EOL : '');
}
}
} else {
// 生成相关MVC文件
foreach ($file as $val) {
$val = trim($val);
$filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . '.php';
$space = $namespace . '\\' . $path;
$class = $val;
switch ($path) {
case 'controller': // 控制器
if ($this->app->config->get('route.controller_suffix')) {
$filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . 'Controller.php';
$class = $val . 'Controller';
}
$content = "<?php" . PHP_EOL . "namespace {$space};" . PHP_EOL . PHP_EOL . "class {$class}" . PHP_EOL . "{" . PHP_EOL . PHP_EOL . "}";
break;
case 'model': // 模型
$content = "<?php" . PHP_EOL . "namespace {$space};" . PHP_EOL . PHP_EOL . "use think\Model;" . PHP_EOL . PHP_EOL . "class {$class} extends Model" . PHP_EOL . "{" . PHP_EOL . PHP_EOL . "}";
break;
case 'view': // 视图
$filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . '.html';
$this->checkDirBuild(dirname($filename));
$content = '';
break;
default:
// 其他文件
$content = "<?php" . PHP_EOL . "namespace {$space};" . PHP_EOL . PHP_EOL . "class {$class}" . PHP_EOL . "{" . PHP_EOL . PHP_EOL . "}";
}
if (!is_file($filename)) {
file_put_contents($filename, $content);
}
}
}
}
}
/**
* 创建应用的欢迎页面
* @access protected
* @param string $app 目录
* @param string $namespace 类库命名空间
* @return void
*/
protected function buildHello(string $app, string $namespace): void
{
$suffix = $this->app->config->get('route.controller_suffix') ? 'Controller' : '';
$filename = $this->basePath . ($app ? $app . DIRECTORY_SEPARATOR : '') . 'controller' . DIRECTORY_SEPARATOR . 'Index' . $suffix . '.php';
if (!is_file($filename)) {
$content = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'controller.stub');
$content = str_replace(['{%name%}', '{%app%}', '{%layer%}', '{%suffix%}'], [$app, $namespace, 'controller', $suffix], $content);
$this->checkDirBuild(dirname($filename));
file_put_contents($filename, $content);
}
}
/**
* 创建应用的公共文件
* @access protected
* @param string $app 目录
* @return void
*/
protected function buildCommon(string $app): void
{
$appPath = $this->basePath . ($app ? $app . DIRECTORY_SEPARATOR : '');
if (!is_file($appPath . 'common.php')) {
file_put_contents($appPath . 'common.php', "<?php" . PHP_EOL . "// 这是系统自动生成的公共文件" . PHP_EOL);
}
foreach (['event', 'middleware', 'provider'] as $name) {
if (!is_file($appPath . $name . '.php')) {
file_put_contents($appPath . $name . '.php', "<?php" . PHP_EOL . "// 这是系统自动生成的{$name}定义文件" . PHP_EOL . "return [" . PHP_EOL . PHP_EOL . "];" . PHP_EOL);
}
}
}
/**
* 创建目录
* @access protected
* @param string $dirname 目录名称
* @return void
*/
protected function checkDirBuild(string $dirname): void
{
if (!is_dir($dirname)) {
mkdir($dirname, 0755, true);
}
}
}

View File

@@ -0,0 +1,12 @@
<?php
declare (strict_types = 1);
namespace {%app%}\{%layer%};
class Index{%suffix%}
{
public function index()
{
return '您好!这是一个[{%name%}]示例应用';
}
}