App.php 27 KB

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