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. var_dump('aaa');
  357. $this->middleware->add(function (Request $request, $next) use ($dispatch, $data) {
  358. return is_null($data) ? $dispatch->run() : $data;
  359. });
  360. var_dump('bbb');
  361. $response = $this->middleware->dispatch($this->request);
  362. var_dump('ccc');
  363. // 监听app_end
  364. $this->hook->listen('app_end', $response);
  365. return $response;
  366. }
  367. protected function getRouteCacheKey()
  368. {
  369. if ($this->config->get('route_check_cache_key')) {
  370. $closure = $this->config->get('route_check_cache_key');
  371. $routeKey = $closure($this->request);
  372. } else {
  373. $routeKey = md5($this->request->baseUrl(true) . ':' . $this->request->method());
  374. }
  375. return $routeKey;
  376. }
  377. protected function loadLangPack()
  378. {
  379. // 读取默认语言
  380. $this->lang->range($this->config('app.default_lang'));
  381. if ($this->config('app.lang_switch_on')) {
  382. // 开启多语言机制 检测当前语言
  383. $this->lang->detect();
  384. }
  385. $this->request->setLangset($this->lang->range());
  386. // 加载系统语言包
  387. $this->lang->load([
  388. $this->thinkPath . 'lang' . DIRECTORY_SEPARATOR . $this->request->langset() . '.php',
  389. $this->appPath . 'lang' . DIRECTORY_SEPARATOR . $this->request->langset() . '.php',
  390. ]);
  391. }
  392. /**
  393. * 设置当前地址的请求缓存
  394. * @access public
  395. * @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
  396. * @param mixed $expire 缓存有效期
  397. * @param array $except 缓存排除
  398. * @param string $tag 缓存标签
  399. * @return void
  400. */
  401. public function checkRequestCache($key, $expire = null, $except = [], $tag = null)
  402. {
  403. $cache = $this->request->cache($key, $expire, $except, $tag);
  404. if ($cache) {
  405. $this->setResponseCache($cache);
  406. }
  407. }
  408. public function setResponseCache($cache)
  409. {
  410. list($key, $expire, $tag) = $cache;
  411. if (strtotime($this->request->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $this->request->server('REQUEST_TIME')) {
  412. // 读取缓存
  413. $response = Response::create()->code(304);
  414. throw new HttpResponseException($response);
  415. } elseif ($this->cache->has($key)) {
  416. list($content, $header) = $this->cache->get($key);
  417. $response = Response::create($content)->header($header);
  418. throw new HttpResponseException($response);
  419. }
  420. }
  421. /**
  422. * 设置当前请求的调度信息
  423. * @access public
  424. * @param Dispatch $dispatch 调度信息
  425. * @return $this
  426. */
  427. public function dispatch(Dispatch $dispatch)
  428. {
  429. $this->dispatch = $dispatch;
  430. return $this;
  431. }
  432. /**
  433. * 记录调试信息
  434. * @access public
  435. * @param mixed $msg 调试信息
  436. * @param string $type 信息类型
  437. * @return void
  438. */
  439. public function log($msg, $type = 'info')
  440. {
  441. $this->appDebug && $this->log->record($msg, $type);
  442. }
  443. /**
  444. * 获取配置参数 为空则获取所有配置
  445. * @access public
  446. * @param string $name 配置参数名(支持二级配置 .号分割)
  447. * @return mixed
  448. */
  449. public function config($name = '')
  450. {
  451. return $this->config->get($name);
  452. }
  453. /**
  454. * 路由初始化 导入路由定义规则
  455. * @access public
  456. * @return void
  457. */
  458. public function routeInit()
  459. {
  460. // 路由检测
  461. $files = scandir($this->routePath);
  462. foreach ($files as $file) {
  463. if (strpos($file, '.php')) {
  464. $filename = $this->routePath . $file;
  465. // 导入路由配置
  466. $rules = include $filename;
  467. if (is_array($rules)) {
  468. $this->route->import($rules);
  469. }
  470. }
  471. }
  472. if ($this->route->config('route_annotation')) {
  473. // 自动生成路由定义
  474. if ($this->appDebug) {
  475. $suffix = $this->route->config('controller_suffix') || $this->route->config('class_suffix');
  476. $this->build->buildRoute($suffix);
  477. }
  478. $filename = $this->runtimePath . 'build_route.php';
  479. if (is_file($filename)) {
  480. include $filename;
  481. }
  482. }
  483. }
  484. /**
  485. * URL路由检测(根据PATH_INFO)
  486. * @access public
  487. * @return Dispatch
  488. */
  489. public function routeCheck()
  490. {
  491. // 检测路由缓存
  492. if (!$this->appDebug && $this->config->get('route_check_cache')) {
  493. $routeKey = $this->getRouteCacheKey();
  494. $option = $this->config->get('route_cache_option');
  495. if ($option && $this->cache->connect($option)->has($routeKey)) {
  496. return $this->cache->connect($option)->get($routeKey);
  497. } elseif ($this->cache->has($routeKey)) {
  498. return $this->cache->get($routeKey);
  499. }
  500. }
  501. // 获取应用调度信息
  502. $path = $this->request->path();
  503. // 是否强制路由模式
  504. $must = !is_null($this->routeMust) ? $this->routeMust : $this->route->config('url_route_must');
  505. // 路由检测 返回一个Dispatch对象
  506. $dispatch = $this->route->check($path, $must);
  507. if (!empty($routeKey)) {
  508. try {
  509. if ($option) {
  510. $this->cache->connect($option)->tag('route_cache')->set($routeKey, $dispatch);
  511. } else {
  512. $this->cache->tag('route_cache')->set($routeKey, $dispatch);
  513. }
  514. } catch (\Exception $e) {
  515. // 存在闭包的时候缓存无效
  516. }
  517. }
  518. return $dispatch;
  519. }
  520. /**
  521. * 设置应用的路由检测机制
  522. * @access public
  523. * @param bool $must 是否强制检测路由
  524. * @return $this
  525. */
  526. public function routeMust($must = false)
  527. {
  528. $this->routeMust = $must;
  529. return $this;
  530. }
  531. /**
  532. * 解析模块和类名
  533. * @access protected
  534. * @param string $name 资源地址
  535. * @param string $layer 验证层名称
  536. * @param bool $appendSuffix 是否添加类名后缀
  537. * @return array
  538. */
  539. protected function parseModuleAndClass($name, $layer, $appendSuffix)
  540. {
  541. if (false !== strpos($name, '\\')) {
  542. $class = $name;
  543. $module = $this->request->module();
  544. } else {
  545. if (strpos($name, '/')) {
  546. list($module, $name) = explode('/', $name, 2);
  547. } else {
  548. $module = $this->request->module();
  549. }
  550. $class = $this->parseClass($module, $layer, $name, $appendSuffix);
  551. }
  552. return [$module, $class];
  553. }
  554. /**
  555. * 实例化应用类库
  556. * @access public
  557. * @param string $name 类名称
  558. * @param string $layer 业务层名称
  559. * @param bool $appendSuffix 是否添加类名后缀
  560. * @param string $common 公共模块名
  561. * @return object
  562. * @throws ClassNotFoundException
  563. */
  564. public function create($name, $layer, $appendSuffix = false, $common = 'common')
  565. {
  566. $guid = $name . $layer;
  567. if ($this->__isset($guid)) {
  568. return $this->__get($guid);
  569. }
  570. list($module, $class) = $this->parseModuleAndClass($name, $layer, $appendSuffix);
  571. if (class_exists($class)) {
  572. $object = $this->__get($class);
  573. } else {
  574. $class = str_replace('\\' . $module . '\\', '\\' . $common . '\\', $class);
  575. if (class_exists($class)) {
  576. $object = $this->__get($class);
  577. } else {
  578. throw new ClassNotFoundException('class not exists:' . $class, $class);
  579. }
  580. }
  581. $this->__set($guid, $class);
  582. return $object;
  583. }
  584. /**
  585. * 实例化(分层)模型
  586. * @access public
  587. * @param string $name Model名称
  588. * @param string $layer 业务层名称
  589. * @param bool $appendSuffix 是否添加类名后缀
  590. * @param string $common 公共模块名
  591. * @return Model
  592. * @throws ClassNotFoundException
  593. */
  594. public function model($name = '', $layer = 'model', $appendSuffix = false, $common = 'common')
  595. {
  596. return $this->create($name, $layer, $appendSuffix, $common);
  597. }
  598. /**
  599. * 实例化(分层)控制器 格式:[模块名/]控制器名
  600. * @access public
  601. * @param string $name 资源地址
  602. * @param string $layer 控制层名称
  603. * @param bool $appendSuffix 是否添加类名后缀
  604. * @param string $empty 空控制器名称
  605. * @return object
  606. * @throws ClassNotFoundException
  607. */
  608. public function controller($name, $layer = 'controller', $appendSuffix = false, $empty = '')
  609. {
  610. list($module, $class) = $this->parseModuleAndClass($name, $layer, $appendSuffix);
  611. if (class_exists($class)) {
  612. return $this->make($class, true);
  613. } elseif ($empty && class_exists($emptyClass = $this->parseClass($module, $layer, $empty, $appendSuffix))) {
  614. return $this->make($emptyClass, true);
  615. }
  616. throw new ClassNotFoundException('class not exists:' . $class, $class);
  617. }
  618. /**
  619. * 实例化验证类 格式:[模块名/]验证器名
  620. * @access public
  621. * @param string $name 资源地址
  622. * @param string $layer 验证层名称
  623. * @param bool $appendSuffix 是否添加类名后缀
  624. * @param string $common 公共模块名
  625. * @return Validate
  626. * @throws ClassNotFoundException
  627. */
  628. public function validate($name = '', $layer = 'validate', $appendSuffix = false, $common = 'common')
  629. {
  630. $name = $name ?: $this->config('default_validate');
  631. if (empty($name)) {
  632. return new Validate;
  633. }
  634. return $this->create($name, $layer, $appendSuffix, $common);
  635. }
  636. /**
  637. * 数据库初始化
  638. * @access public
  639. * @param mixed $config 数据库配置
  640. * @param bool|string $name 连接标识 true 强制重新连接
  641. * @return \think\db\Query
  642. */
  643. public function db($config = [], $name = false)
  644. {
  645. return Db::connect($config, $name);
  646. }
  647. /**
  648. * 远程调用模块的操作方法 参数格式 [模块/控制器/]操作
  649. * @access public
  650. * @param string $url 调用地址
  651. * @param string|array $vars 调用参数 支持字符串和数组
  652. * @param string $layer 要调用的控制层名称
  653. * @param bool $appendSuffix 是否添加类名后缀
  654. * @return mixed
  655. * @throws ClassNotFoundException
  656. */
  657. public function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
  658. {
  659. $info = pathinfo($url);
  660. $action = $info['basename'];
  661. $module = '.' != $info['dirname'] ? $info['dirname'] : $this->request->controller();
  662. $class = $this->controller($module, $layer, $appendSuffix);
  663. if (is_scalar($vars)) {
  664. if (strpos($vars, '=')) {
  665. parse_str($vars, $vars);
  666. } else {
  667. $vars = [$vars];
  668. }
  669. }
  670. return $this->invokeMethod([$class, $action . $this->config('action_suffix')], $vars);
  671. }
  672. /**
  673. * 解析应用类的类名
  674. * @access public
  675. * @param string $module 模块名
  676. * @param string $layer 层名 controller model ...
  677. * @param string $name 类名
  678. * @param bool $appendSuffix
  679. * @return string
  680. */
  681. public function parseClass($module, $layer, $name, $appendSuffix = false)
  682. {
  683. $name = str_replace(['/', '.'], '\\', $name);
  684. $array = explode('\\', $name);
  685. $class = Loader::parseName(array_pop($array), 1) . ($this->suffix || $appendSuffix ? ucfirst($layer) : '');
  686. $path = $array ? implode('\\', $array) . '\\' : '';
  687. return $this->namespace . '\\' . ($module ? $module . '\\' : '') . $layer . '\\' . $path . $class;
  688. }
  689. /**
  690. * 获取框架版本
  691. * @access public
  692. * @return string
  693. */
  694. public function version()
  695. {
  696. return static::VERSION;
  697. }
  698. /**
  699. * 是否为调试模式
  700. * @access public
  701. * @return bool
  702. */
  703. public function isDebug()
  704. {
  705. return $this->appDebug;
  706. }
  707. /**
  708. * 获取模块路径
  709. * @access public
  710. * @return string
  711. */
  712. public function getModulePath()
  713. {
  714. return $this->modulePath;
  715. }
  716. /**
  717. * 设置模块路径
  718. * @access public
  719. * @param string $path 路径
  720. * @return void
  721. */
  722. public function setModulePath($path)
  723. {
  724. $this->modulePath = $path;
  725. $this->env->set('module_path', $path);
  726. }
  727. /**
  728. * 获取应用根目录
  729. * @access public
  730. * @return string
  731. */
  732. public function getRootPath()
  733. {
  734. return $this->rootPath;
  735. }
  736. /**
  737. * 获取应用类库目录
  738. * @access public
  739. * @return string
  740. */
  741. public function getAppPath()
  742. {
  743. if (is_null($this->appPath)) {
  744. $this->appPath = Loader::getRootPath() . 'application' . DIRECTORY_SEPARATOR;
  745. }
  746. return $this->appPath;
  747. }
  748. /**
  749. * 获取应用运行时目录
  750. * @access public
  751. * @return string
  752. */
  753. public function getRuntimePath()
  754. {
  755. return $this->runtimePath;
  756. }
  757. /**
  758. * 获取核心框架目录
  759. * @access public
  760. * @return string
  761. */
  762. public function getThinkPath()
  763. {
  764. return $this->thinkPath;
  765. }
  766. /**
  767. * 获取路由目录
  768. * @access public
  769. * @return string
  770. */
  771. public function getRoutePath()
  772. {
  773. return $this->routePath;
  774. }
  775. /**
  776. * 获取应用配置目录
  777. * @access public
  778. * @return string
  779. */
  780. public function getConfigPath()
  781. {
  782. return $this->configPath;
  783. }
  784. /**
  785. * 获取配置后缀
  786. * @access public
  787. * @return string
  788. */
  789. public function getConfigExt()
  790. {
  791. return $this->configExt;
  792. }
  793. /**
  794. * 获取应用类库命名空间
  795. * @access public
  796. * @return string
  797. */
  798. public function getNamespace()
  799. {
  800. return $this->namespace;
  801. }
  802. /**
  803. * 设置应用类库命名空间
  804. * @access public
  805. * @param string $namespace 命名空间名称
  806. * @return $this
  807. */
  808. public function setNamespace($namespace)
  809. {
  810. $this->namespace = $namespace;
  811. return $this;
  812. }
  813. /**
  814. * 是否启用类库后缀
  815. * @access public
  816. * @return bool
  817. */
  818. public function getSuffix()
  819. {
  820. return $this->suffix;
  821. }
  822. /**
  823. * 获取应用开启时间
  824. * @access public
  825. * @return float
  826. */
  827. public function getBeginTime()
  828. {
  829. return $this->beginTime;
  830. }
  831. /**
  832. * 获取应用初始内存占用
  833. * @access public
  834. * @return integer
  835. */
  836. public function getBeginMem()
  837. {
  838. return $this->beginMem;
  839. }
  840. }