App.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use think\exception\ClassNotFoundException;
  13. use think\exception\HttpResponseException;
  14. use think\route\Dispatch;
  15. /**
  16. * App 应用管理
  17. */
  18. class App extends Container
  19. {
  20. const VERSION = '5.1.37 LTS';
  21. /**
  22. * 当前模块路径
  23. * @var string
  24. */
  25. protected $modulePath;
  26. /**
  27. * 应用调试模式
  28. * @var bool
  29. */
  30. protected $appDebug = true;
  31. /**
  32. * 应用开始时间
  33. * @var float
  34. */
  35. protected $beginTime;
  36. /**
  37. * 应用内存初始占用
  38. * @var integer
  39. */
  40. protected $beginMem;
  41. /**
  42. * 应用类库命名空间
  43. * @var string
  44. */
  45. protected $namespace = 'app';
  46. /**
  47. * 应用类库后缀
  48. * @var bool
  49. */
  50. protected $suffix = false;
  51. /**
  52. * 严格路由检测
  53. * @var bool
  54. */
  55. protected $routeMust;
  56. /**
  57. * 应用类库目录
  58. * @var string
  59. */
  60. protected $appPath;
  61. /**
  62. * 框架目录
  63. * @var string
  64. */
  65. protected $thinkPath;
  66. /**
  67. * 应用根目录
  68. * @var string
  69. */
  70. protected $rootPath;
  71. /**
  72. * 运行时目录
  73. * @var string
  74. */
  75. protected $runtimePath;
  76. /**
  77. * 配置目录
  78. * @var string
  79. */
  80. protected $configPath;
  81. /**
  82. * 路由目录
  83. * @var string
  84. */
  85. protected $routePath;
  86. /**
  87. * 配置后缀
  88. * @var string
  89. */
  90. protected $configExt;
  91. /**
  92. * 应用调度实例
  93. * @var Dispatch
  94. */
  95. protected $dispatch;
  96. /**
  97. * 绑定模块(控制器)
  98. * @var string
  99. */
  100. protected $bindModule;
  101. /**
  102. * 初始化
  103. * @var bool
  104. */
  105. protected $initialized = false;
  106. public function __construct($appPath = '')
  107. {
  108. $this->thinkPath = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR;
  109. $this->path($appPath);
  110. }
  111. /**
  112. * 绑定模块或者控制器
  113. * @access public
  114. * @param string $bind
  115. * @return $this
  116. */
  117. public function bind($bind)
  118. {
  119. $this->bindModule = $bind;
  120. return $this;
  121. }
  122. public function runWeb()
  123. {
  124. try {
  125. // 初始化应用
  126. $this->initialize();
  127. // 监听app_init
  128. $this->hook->listen('app_init');
  129. /*if ($this->bindModule) {
  130. // 模块/控制器绑定
  131. $this->route->bind($this->bindModule);
  132. } elseif ($this->config('app.auto_bind_module')) {
  133. // 入口自动绑定
  134. $name = pathinfo($this->request->baseFile(), PATHINFO_FILENAME);
  135. if ($name && 'index' != $name && is_dir($this->appPath . $name)) {
  136. $this->route->bind($name);
  137. }
  138. }*/
  139. // 监听app_dispatch
  140. $this->hook->listen('app_dispatch');
  141. $dispatch = $this->dispatch;
  142. if (empty($dispatch)) {
  143. // 路由检测
  144. $dispatch = $this->routeCheck()->init();
  145. }
  146. // 记录当前调度信息
  147. $this->request->dispatch($dispatch);
  148. // 记录路由和请求信息
  149. if ($this->appDebug) {
  150. $this->log('[ ROUTE ] ' . var_export($this->request->routeInfo(), true));
  151. $this->log('[ HEADER ] ' . var_export($this->request->header(), true));
  152. $this->log('[ PARAM ] ' . var_export($this->request->param(), true));
  153. }
  154. // 监听app_begin
  155. $this->hook->listen('app_begin');
  156. // 请求缓存检查
  157. $this->checkRequestCache(
  158. $this->config('request_cache'),
  159. $this->config('request_cache_expire'),
  160. $this->config('request_cache_except')
  161. );
  162. $data = null;
  163. } catch (HttpResponseException $exception) {
  164. $dispatch = null;
  165. $data = $exception->getResponse();
  166. }
  167. $this->middleware->add(function (Request $request, $next) use ($dispatch, $data) {
  168. return is_null($data) ? $dispatch->run() : $data;
  169. });
  170. $response = $this->middleware->dispatch($this->request);
  171. // 监听app_end
  172. $this->hook->listen('app_end', $response);
  173. return $response;
  174. }
  175. /**
  176. * 设置应用类库目录
  177. * @access public
  178. * @param string $path 路径
  179. * @return $this
  180. */
  181. public function path($path)
  182. {
  183. $this->appPath = $path ? realpath($path) . DIRECTORY_SEPARATOR : $this->getAppPath();
  184. return $this;
  185. }
  186. /**
  187. * 初始化应用
  188. * @access public
  189. * @return void
  190. */
  191. public function initialize()
  192. {
  193. if ($this->initialized) {
  194. return;
  195. }
  196. $this->initialized = true;
  197. $this->beginTime = microtime(true);
  198. $this->beginMem = memory_get_usage();
  199. $this->rootPath = dirname($this->appPath) . DIRECTORY_SEPARATOR;
  200. $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR;
  201. $this->routePath = $this->rootPath . 'route' . DIRECTORY_SEPARATOR;
  202. $this->configPath = $this->rootPath . 'config' . DIRECTORY_SEPARATOR;
  203. static::setInstance($this);
  204. $this->instance('app', $this);
  205. // 加载环境变量配置文件
  206. if (is_file($this->rootPath . '.env')) {
  207. $this->env->load($this->rootPath . '.env');
  208. }
  209. $this->configExt = $this->env->get('config_ext', '.php');
  210. // 加载惯例配置文件
  211. $this->config->set(include $this->thinkPath . 'convention.php');
  212. // 设置路径环境变量
  213. $this->env->set([
  214. 'think_path' => $this->thinkPath,
  215. 'root_path' => $this->rootPath,
  216. 'app_path' => $this->appPath,
  217. 'config_path' => $this->configPath,
  218. 'route_path' => $this->routePath,
  219. 'runtime_path' => $this->runtimePath,
  220. 'extend_path' => $this->rootPath . 'extend' . DIRECTORY_SEPARATOR,
  221. 'vendor_path' => $this->rootPath . 'vendor' . DIRECTORY_SEPARATOR,
  222. ]);
  223. $this->namespace = $this->env->get('app_namespace', $this->namespace);
  224. $this->env->set('app_namespace', $this->namespace);
  225. // 注册应用命名空间
  226. Loader::addNamespace($this->namespace, $this->appPath);
  227. // 初始化应用
  228. $this->init();
  229. // 开启类名后缀
  230. $this->suffix = $this->config('app.class_suffix');
  231. // 应用调试模式
  232. $this->appDebug = $this->env->get('app_debug', $this->config('app.app_debug'));
  233. $this->env->set('app_debug', $this->appDebug);
  234. if (!$this->appDebug) {
  235. ini_set('display_errors', 'Off');
  236. } elseif (PHP_SAPI != 'cli') {
  237. //重新申请一块比较大的buffer
  238. if (ob_get_level() > 0) {
  239. $output = ob_get_clean();
  240. }
  241. ob_start();
  242. if (!empty($output)) {
  243. echo $output;
  244. }
  245. }
  246. // 注册异常处理类
  247. if ($this->config('app.exception_handle')) {
  248. Error::setExceptionHandler($this->config('app.exception_handle'));
  249. }
  250. // 注册根命名空间
  251. if (!empty($this->config('app.root_namespace'))) {
  252. Loader::addNamespace($this->config('app.root_namespace'));
  253. }
  254. // 加载composer autofile文件
  255. Loader::loadComposerAutoloadFiles();
  256. // 注册类库别名
  257. Loader::addClassAlias($this->config->pull('alias'));
  258. // 数据库配置初始化
  259. Db::init($this->config->pull('database'));
  260. // 设置系统时区
  261. date_default_timezone_set($this->config('app.default_timezone'));
  262. // 读取语言包
  263. $this->loadLangPack();
  264. // 路由初始化
  265. $this->routeInit();
  266. }
  267. /**
  268. * 初始化应用或模块
  269. * @access public
  270. * @param string $module 模块名
  271. * @return void
  272. */
  273. public function init($module = '')
  274. {
  275. // 定位模块目录
  276. $module = $module ? $module . DIRECTORY_SEPARATOR : '';
  277. $path = $this->appPath . $module;
  278. // 加载初始化文件
  279. if (is_file($path . 'init.php')) {
  280. include $path . 'init.php';
  281. } elseif (is_file($this->runtimePath . $module . 'init.php')) {
  282. include $this->runtimePath . $module . 'init.php';
  283. } else {
  284. // 加载行为扩展文件
  285. if (is_file($path . 'tags.php')) {
  286. $tags = include $path . 'tags.php';
  287. if (is_array($tags)) {
  288. $this->hook->import($tags);
  289. }
  290. }
  291. // 加载公共文件
  292. if (is_file($path . 'common.php')) {
  293. include_once $path . 'common.php';
  294. }
  295. if ('' == $module) {
  296. // 加载系统助手函数
  297. include $this->thinkPath . 'helper.php';
  298. }
  299. // 加载中间件
  300. if (is_file($path . 'middleware.php')) {
  301. $middleware = include $path . 'middleware.php';
  302. if (is_array($middleware)) {
  303. $this->middleware->import($middleware);
  304. }
  305. }
  306. // 注册服务的容器对象实例
  307. if (is_file($path . 'provider.php')) {
  308. $provider = include $path . 'provider.php';
  309. if (is_array($provider)) {
  310. $this->bindTo($provider);
  311. }
  312. }
  313. // 自动读取配置文件
  314. if (is_dir($path . 'config')) {
  315. $dir = $path . 'config' . DIRECTORY_SEPARATOR;
  316. } elseif (is_dir($this->configPath . $module)) {
  317. $dir = $this->configPath . $module;
  318. }
  319. $files = isset($dir) ? scandir($dir) : [];
  320. foreach ($files as $file) {
  321. if ('.' . pathinfo($file, PATHINFO_EXTENSION) === $this->configExt) {
  322. $this->config->load($dir . $file, pathinfo($file, PATHINFO_FILENAME));
  323. }
  324. }
  325. }
  326. $this->setModulePath($path);
  327. if ($module) {
  328. // 对容器中的对象实例进行配置更新
  329. $this->containerConfigUpdate($module);
  330. }
  331. }
  332. protected function containerConfigUpdate($module)
  333. {
  334. $config = $this->config->get();
  335. // 注册异常处理类
  336. if ($config['app']['exception_handle']) {
  337. Error::setExceptionHandler($config['app']['exception_handle']);
  338. }
  339. Db::init($config['database']);
  340. $this->middleware->setConfig($config['middleware']);
  341. $this->route->setConfig($config['app']);
  342. $this->request->init($config['app']);
  343. $this->cookie->init($config['cookie']);
  344. $this->view->init($config['template']);
  345. $this->log->init($config['log']);
  346. $this->session->setConfig($config['session']);
  347. $this->debug->setConfig($config['trace']);
  348. $this->cache->init($config['cache'], true);
  349. // 加载当前模块语言包
  350. $this->lang->load($this->appPath . $module . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $this->request->langset() . '.php');
  351. // 模块请求缓存检查
  352. $this->checkRequestCache(
  353. $config['app']['request_cache'],
  354. $config['app']['request_cache_expire'],
  355. $config['app']['request_cache_except']
  356. );
  357. }
  358. /**
  359. * 执行应用程序
  360. * @access public
  361. * @return Response
  362. * @throws Exception
  363. */
  364. public function run()
  365. {
  366. try {
  367. // 初始化应用
  368. $this->initialize();
  369. // 监听app_init
  370. $this->hook->listen('app_init');
  371. if ($this->bindModule) {
  372. // 模块/控制器绑定
  373. $this->route->bind($this->bindModule);
  374. } elseif ($this->config('app.auto_bind_module')) {
  375. // 入口自动绑定
  376. $name = pathinfo($this->request->baseFile(), PATHINFO_FILENAME);
  377. if ($name && 'index' != $name && is_dir($this->appPath . $name)) {
  378. $this->route->bind($name);
  379. }
  380. }
  381. // 监听app_dispatch
  382. $this->hook->listen('app_dispatch');
  383. $dispatch = $this->dispatch;
  384. if (empty($dispatch)) {
  385. // 路由检测
  386. $dispatch = $this->routeCheck()->init();
  387. }
  388. // 记录当前调度信息
  389. $this->request->dispatch($dispatch);
  390. // 记录路由和请求信息
  391. if ($this->appDebug) {
  392. $this->log('[ ROUTE ] ' . var_export($this->request->routeInfo(), true));
  393. $this->log('[ HEADER ] ' . var_export($this->request->header(), true));
  394. $this->log('[ PARAM ] ' . var_export($this->request->param(), true));
  395. }
  396. // 监听app_begin
  397. $this->hook->listen('app_begin');
  398. // 请求缓存检查
  399. $this->checkRequestCache(
  400. $this->config('request_cache'),
  401. $this->config('request_cache_expire'),
  402. $this->config('request_cache_except')
  403. );
  404. $data = null;
  405. } catch (HttpResponseException $exception) {
  406. $dispatch = null;
  407. $data = $exception->getResponse();
  408. }
  409. $this->middleware->add(function (Request $request, $next) use ($dispatch, $data) {
  410. return is_null($data) ? $dispatch->run() : $data;
  411. });
  412. $response = $this->middleware->dispatch($this->request);
  413. // 监听app_end
  414. $this->hook->listen('app_end', $response);
  415. return $response;
  416. }
  417. protected function getRouteCacheKey()
  418. {
  419. if ($this->config->get('route_check_cache_key')) {
  420. $closure = $this->config->get('route_check_cache_key');
  421. $routeKey = $closure($this->request);
  422. } else {
  423. $routeKey = md5($this->request->baseUrl(true) . ':' . $this->request->method());
  424. }
  425. return $routeKey;
  426. }
  427. protected function loadLangPack()
  428. {
  429. // 读取默认语言
  430. $this->lang->range($this->config('app.default_lang'));
  431. if ($this->config('app.lang_switch_on')) {
  432. // 开启多语言机制 检测当前语言
  433. $this->lang->detect();
  434. }
  435. $this->request->setLangset($this->lang->range());
  436. // 加载系统语言包
  437. $this->lang->load([
  438. $this->thinkPath . 'lang' . DIRECTORY_SEPARATOR . $this->request->langset() . '.php',
  439. $this->appPath . 'lang' . DIRECTORY_SEPARATOR . $this->request->langset() . '.php',
  440. ]);
  441. }
  442. /**
  443. * 设置当前地址的请求缓存
  444. * @access public
  445. * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
  446. * @param mixed $expire 缓存有效期
  447. * @param array $except 缓存排除
  448. * @param string $tag 缓存标签
  449. * @return void
  450. */
  451. public function checkRequestCache($key, $expire = null, $except = [], $tag = null)
  452. {
  453. $cache = $this->request->cache($key, $expire, $except, $tag);
  454. if ($cache) {
  455. $this->setResponseCache($cache);
  456. }
  457. }
  458. public function setResponseCache($cache)
  459. {
  460. list($key, $expire, $tag) = $cache;
  461. if (strtotime($this->request->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $this->request->server('REQUEST_TIME')) {
  462. // 读取缓存
  463. $response = Response::create()->code(304);
  464. throw new HttpResponseException($response);
  465. } elseif ($this->cache->has($key)) {
  466. list($content, $header) = $this->cache->get($key);
  467. $response = Response::create($content)->header($header);
  468. throw new HttpResponseException($response);
  469. }
  470. }
  471. /**
  472. * 设置当前请求的调度信息
  473. * @access public
  474. * @param Dispatch $dispatch 调度信息
  475. * @return $this
  476. */
  477. public function dispatch(Dispatch $dispatch)
  478. {
  479. $this->dispatch = $dispatch;
  480. return $this;
  481. }
  482. /**
  483. * 记录调试信息
  484. * @access public
  485. * @param mixed $msg 调试信息
  486. * @param string $type 信息类型
  487. * @return void
  488. */
  489. public function log($msg, $type = 'info')
  490. {
  491. $this->appDebug && $this->log->record($msg, $type);
  492. }
  493. /**
  494. * 获取配置参数 为空则获取所有配置
  495. * @access public
  496. * @param string $name 配置参数名(支持二级配置 .号分割)
  497. * @return mixed
  498. */
  499. public function config($name = '')
  500. {
  501. return $this->config->get($name);
  502. }
  503. /**
  504. * 路由初始化 导入路由定义规则
  505. * @access public
  506. * @return void
  507. */
  508. public function routeInit()
  509. {
  510. // 路由检测
  511. $files = scandir($this->routePath);
  512. foreach ($files as $file) {
  513. if (strpos($file, '.php')) {
  514. $filename = $this->routePath . $file;
  515. // 导入路由配置
  516. $rules = include $filename;
  517. if (is_array($rules)) {
  518. $this->route->import($rules);
  519. }
  520. }
  521. }
  522. if ($this->route->config('route_annotation')) {
  523. // 自动生成路由定义
  524. if ($this->appDebug) {
  525. $suffix = $this->route->config('controller_suffix') || $this->route->config('class_suffix');
  526. $this->build->buildRoute($suffix);
  527. }
  528. $filename = $this->runtimePath . 'build_route.php';
  529. if (is_file($filename)) {
  530. include $filename;
  531. }
  532. }
  533. }
  534. /**
  535. * URL路由检测(根据PATH_INFO)
  536. * @access public
  537. * @return Dispatch
  538. */
  539. public function routeCheck()
  540. {
  541. // 检测路由缓存
  542. if (!$this->appDebug && $this->config->get('route_check_cache')) {
  543. $routeKey = $this->getRouteCacheKey();
  544. $option = $this->config->get('route_cache_option');
  545. if ($option && $this->cache->connect($option)->has($routeKey)) {
  546. return $this->cache->connect($option)->get($routeKey);
  547. } elseif ($this->cache->has($routeKey)) {
  548. return $this->cache->get($routeKey);
  549. }
  550. }
  551. // 获取应用调度信息
  552. $path = $this->request->path();
  553. // 是否强制路由模式
  554. $must = !is_null($this->routeMust) ? $this->routeMust : $this->route->config('url_route_must');
  555. // 路由检测 返回一个Dispatch对象
  556. $dispatch = $this->route->check($path, $must);
  557. if (!empty($routeKey)) {
  558. try {
  559. if ($option) {
  560. $this->cache->connect($option)->tag('route_cache')->set($routeKey, $dispatch);
  561. } else {
  562. $this->cache->tag('route_cache')->set($routeKey, $dispatch);
  563. }
  564. } catch (\Exception $e) {
  565. // 存在闭包的时候缓存无效
  566. }
  567. }
  568. return $dispatch;
  569. }
  570. /**
  571. * 设置应用的路由检测机制
  572. * @access public
  573. * @param bool $must 是否强制检测路由
  574. * @return $this
  575. */
  576. public function routeMust($must = false)
  577. {
  578. $this->routeMust = $must;
  579. return $this;
  580. }
  581. /**
  582. * 解析模块和类名
  583. * @access protected
  584. * @param string $name 资源地址
  585. * @param string $layer 验证层名称
  586. * @param bool $appendSuffix 是否添加类名后缀
  587. * @return array
  588. */
  589. protected function parseModuleAndClass($name, $layer, $appendSuffix)
  590. {
  591. if (false !== strpos($name, '\\')) {
  592. $class = $name;
  593. $module = $this->request->module();
  594. } else {
  595. if (strpos($name, '/')) {
  596. list($module, $name) = explode('/', $name, 2);
  597. } else {
  598. $module = $this->request->module();
  599. }
  600. $class = $this->parseClass($module, $layer, $name, $appendSuffix);
  601. }
  602. return [$module, $class];
  603. }
  604. /**
  605. * 实例化应用类库
  606. * @access public
  607. * @param string $name 类名称
  608. * @param string $layer 业务层名称
  609. * @param bool $appendSuffix 是否添加类名后缀
  610. * @param string $common 公共模块名
  611. * @return object
  612. * @throws ClassNotFoundException
  613. */
  614. public function create($name, $layer, $appendSuffix = false, $common = 'common')
  615. {
  616. $guid = $name . $layer;
  617. if ($this->__isset($guid)) {
  618. return $this->__get($guid);
  619. }
  620. list($module, $class) = $this->parseModuleAndClass($name, $layer, $appendSuffix);
  621. if (class_exists($class)) {
  622. $object = $this->__get($class);
  623. } else {
  624. $class = str_replace('\\' . $module . '\\', '\\' . $common . '\\', $class);
  625. if (class_exists($class)) {
  626. $object = $this->__get($class);
  627. } else {
  628. throw new ClassNotFoundException('class not exists:' . $class, $class);
  629. }
  630. }
  631. $this->__set($guid, $class);
  632. return $object;
  633. }
  634. /**
  635. * 实例化(分层)模型
  636. * @access public
  637. * @param string $name Model名称
  638. * @param string $layer 业务层名称
  639. * @param bool $appendSuffix 是否添加类名后缀
  640. * @param string $common 公共模块名
  641. * @return Model
  642. * @throws ClassNotFoundException
  643. */
  644. public function model($name = '', $layer = 'model', $appendSuffix = false, $common = 'common')
  645. {
  646. return $this->create($name, $layer, $appendSuffix, $common);
  647. }
  648. /**
  649. * 实例化(分层)控制器 格式:[模块名/]控制器名
  650. * @access public
  651. * @param string $name 资源地址
  652. * @param string $layer 控制层名称
  653. * @param bool $appendSuffix 是否添加类名后缀
  654. * @param string $empty 空控制器名称
  655. * @return object
  656. * @throws ClassNotFoundException
  657. */
  658. public function controller($name, $layer = 'controller', $appendSuffix = false, $empty = '')
  659. {
  660. list($module, $class) = $this->parseModuleAndClass($name, $layer, $appendSuffix);
  661. if (class_exists($class)) {
  662. return $this->make($class, true);
  663. } elseif ($empty && class_exists($emptyClass = $this->parseClass($module, $layer, $empty, $appendSuffix))) {
  664. return $this->make($emptyClass, true);
  665. }
  666. throw new ClassNotFoundException('class not exists:' . $class, $class);
  667. }
  668. /**
  669. * 实例化验证类 格式:[模块名/]验证器名
  670. * @access public
  671. * @param string $name 资源地址
  672. * @param string $layer 验证层名称
  673. * @param bool $appendSuffix 是否添加类名后缀
  674. * @param string $common 公共模块名
  675. * @return Validate
  676. * @throws ClassNotFoundException
  677. */
  678. public function validate($name = '', $layer = 'validate', $appendSuffix = false, $common = 'common')
  679. {
  680. $name = $name ?: $this->config('default_validate');
  681. if (empty($name)) {
  682. return new Validate;
  683. }
  684. return $this->create($name, $layer, $appendSuffix, $common);
  685. }
  686. /**
  687. * 数据库初始化
  688. * @access public
  689. * @param mixed $config 数据库配置
  690. * @param bool|string $name 连接标识 true 强制重新连接
  691. * @return \think\db\Query
  692. */
  693. public function db($config = [], $name = false)
  694. {
  695. return Db::connect($config, $name);
  696. }
  697. /**
  698. * 远程调用模块的操作方法 参数格式 [模块/控制器/]操作
  699. * @access public
  700. * @param string $url 调用地址
  701. * @param string|array $vars 调用参数 支持字符串和数组
  702. * @param string $layer 要调用的控制层名称
  703. * @param bool $appendSuffix 是否添加类名后缀
  704. * @return mixed
  705. * @throws ClassNotFoundException
  706. */
  707. public function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
  708. {
  709. $info = pathinfo($url);
  710. $action = $info['basename'];
  711. $module = '.' != $info['dirname'] ? $info['dirname'] : $this->request->controller();
  712. $class = $this->controller($module, $layer, $appendSuffix);
  713. if (is_scalar($vars)) {
  714. if (strpos($vars, '=')) {
  715. parse_str($vars, $vars);
  716. } else {
  717. $vars = [$vars];
  718. }
  719. }
  720. return $this->invokeMethod([$class, $action . $this->config('action_suffix')], $vars);
  721. }
  722. /**
  723. * 解析应用类的类名
  724. * @access public
  725. * @param string $module 模块名
  726. * @param string $layer 层名 controller model ...
  727. * @param string $name 类名
  728. * @param bool $appendSuffix
  729. * @return string
  730. */
  731. public function parseClass($module, $layer, $name, $appendSuffix = false)
  732. {
  733. $name = str_replace(['/', '.'], '\\', $name);
  734. $array = explode('\\', $name);
  735. $class = Loader::parseName(array_pop($array), 1) . ($this->suffix || $appendSuffix ? ucfirst($layer) : '');
  736. $path = $array ? implode('\\', $array) . '\\' : '';
  737. return $this->namespace . '\\' . ($module ? $module . '\\' : '') . $layer . '\\' . $path . $class;
  738. }
  739. /**
  740. * 获取框架版本
  741. * @access public
  742. * @return string
  743. */
  744. public function version()
  745. {
  746. return static::VERSION;
  747. }
  748. /**
  749. * 是否为调试模式
  750. * @access public
  751. * @return bool
  752. */
  753. public function isDebug()
  754. {
  755. return $this->appDebug;
  756. }
  757. /**
  758. * 获取模块路径
  759. * @access public
  760. * @return string
  761. */
  762. public function getModulePath()
  763. {
  764. return $this->modulePath;
  765. }
  766. /**
  767. * 设置模块路径
  768. * @access public
  769. * @param string $path 路径
  770. * @return void
  771. */
  772. public function setModulePath($path)
  773. {
  774. $this->modulePath = $path;
  775. $this->env->set('module_path', $path);
  776. }
  777. /**
  778. * 获取应用根目录
  779. * @access public
  780. * @return string
  781. */
  782. public function getRootPath()
  783. {
  784. return $this->rootPath;
  785. }
  786. /**
  787. * 获取应用类库目录
  788. * @access public
  789. * @return string
  790. */
  791. public function getAppPath()
  792. {
  793. if (is_null($this->appPath)) {
  794. $this->appPath = Loader::getRootPath() . 'application' . DIRECTORY_SEPARATOR;
  795. }
  796. return $this->appPath;
  797. }
  798. /**
  799. * 获取应用运行时目录
  800. * @access public
  801. * @return string
  802. */
  803. public function getRuntimePath()
  804. {
  805. return $this->runtimePath;
  806. }
  807. /**
  808. * 获取核心框架目录
  809. * @access public
  810. * @return string
  811. */
  812. public function getThinkPath()
  813. {
  814. return $this->thinkPath;
  815. }
  816. /**
  817. * 获取路由目录
  818. * @access public
  819. * @return string
  820. */
  821. public function getRoutePath()
  822. {
  823. return $this->routePath;
  824. }
  825. /**
  826. * 获取应用配置目录
  827. * @access public
  828. * @return string
  829. */
  830. public function getConfigPath()
  831. {
  832. return $this->configPath;
  833. }
  834. /**
  835. * 获取配置后缀
  836. * @access public
  837. * @return string
  838. */
  839. public function getConfigExt()
  840. {
  841. return $this->configExt;
  842. }
  843. /**
  844. * 获取应用类库命名空间
  845. * @access public
  846. * @return string
  847. */
  848. public function getNamespace()
  849. {
  850. return $this->namespace;
  851. }
  852. /**
  853. * 设置应用类库命名空间
  854. * @access public
  855. * @param string $namespace 命名空间名称
  856. * @return $this
  857. */
  858. public function setNamespace($namespace)
  859. {
  860. $this->namespace = $namespace;
  861. return $this;
  862. }
  863. /**
  864. * 是否启用类库后缀
  865. * @access public
  866. * @return bool
  867. */
  868. public function getSuffix()
  869. {
  870. return $this->suffix;
  871. }
  872. /**
  873. * 获取应用开启时间
  874. * @access public
  875. * @return float
  876. */
  877. public function getBeginTime()
  878. {
  879. return $this->beginTime;
  880. }
  881. /**
  882. * 获取应用初始内存占用
  883. * @access public
  884. * @return integer
  885. */
  886. public function getBeginMem()
  887. {
  888. return $this->beginMem;
  889. }
  890. }