App.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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.41 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. if (is_dir($this->routePath)) {
  459. $files = glob($this->routePath . '*.php');
  460. foreach ($files as $file) {
  461. $rules = include $file;
  462. if (is_array($rules)) {
  463. $this->route->import($rules);
  464. }
  465. }
  466. }
  467. if ($this->route->config('route_annotation')) {
  468. // 自动生成路由定义
  469. if ($this->appDebug) {
  470. $suffix = $this->route->config('controller_suffix') || $this->route->config('class_suffix');
  471. $this->build->buildRoute($suffix);
  472. }
  473. $filename = $this->runtimePath . 'build_route.php';
  474. if (is_file($filename)) {
  475. include $filename;
  476. }
  477. }
  478. }
  479. /**
  480. * URL路由检测(根据PATH_INFO)
  481. * @access public
  482. * @return Dispatch
  483. */
  484. public function routeCheck()
  485. {
  486. // 检测路由缓存
  487. if (!$this->appDebug && $this->config->get('route_check_cache')) {
  488. $routeKey = $this->getRouteCacheKey();
  489. $option = $this->config->get('route_cache_option');
  490. if ($option && $this->cache->connect($option)->has($routeKey)) {
  491. return $this->cache->connect($option)->get($routeKey);
  492. } elseif ($this->cache->has($routeKey)) {
  493. return $this->cache->get($routeKey);
  494. }
  495. }
  496. // 获取应用调度信息
  497. $path = $this->request->path();
  498. // 是否强制路由模式
  499. $must = !is_null($this->routeMust) ? $this->routeMust : $this->route->config('url_route_must');
  500. // 路由检测 返回一个Dispatch对象
  501. $dispatch = $this->route->check($path, $must);
  502. if (!empty($routeKey)) {
  503. try {
  504. if ($option) {
  505. $this->cache->connect($option)->tag('route_cache')->set($routeKey, $dispatch);
  506. } else {
  507. $this->cache->tag('route_cache')->set($routeKey, $dispatch);
  508. }
  509. } catch (\Exception $e) {
  510. // 存在闭包的时候缓存无效
  511. }
  512. }
  513. return $dispatch;
  514. }
  515. /**
  516. * 设置应用的路由检测机制
  517. * @access public
  518. * @param bool $must 是否强制检测路由
  519. * @return $this
  520. */
  521. public function routeMust($must = false)
  522. {
  523. $this->routeMust = $must;
  524. return $this;
  525. }
  526. /**
  527. * 解析模块和类名
  528. * @access protected
  529. * @param string $name 资源地址
  530. * @param string $layer 验证层名称
  531. * @param bool $appendSuffix 是否添加类名后缀
  532. * @return array
  533. */
  534. protected function parseModuleAndClass($name, $layer, $appendSuffix)
  535. {
  536. if (false !== strpos($name, '\\')) {
  537. $class = $name;
  538. $module = $this->request->module();
  539. } else {
  540. if (strpos($name, '/')) {
  541. list($module, $name) = explode('/', $name, 2);
  542. } else {
  543. $module = $this->request->module();
  544. }
  545. $class = $this->parseClass($module, $layer, $name, $appendSuffix);
  546. }
  547. return [$module, $class];
  548. }
  549. /**
  550. * 实例化应用类库
  551. * @access public
  552. * @param string $name 类名称
  553. * @param string $layer 业务层名称
  554. * @param bool $appendSuffix 是否添加类名后缀
  555. * @param string $common 公共模块名
  556. * @return object
  557. * @throws ClassNotFoundException
  558. */
  559. public function create($name, $layer, $appendSuffix = false, $common = 'common')
  560. {
  561. $guid = $name . $layer;
  562. if ($this->__isset($guid)) {
  563. return $this->__get($guid);
  564. }
  565. list($module, $class) = $this->parseModuleAndClass($name, $layer, $appendSuffix);
  566. if (class_exists($class)) {
  567. $object = $this->__get($class);
  568. } else {
  569. $class = str_replace('\\' . $module . '\\', '\\' . $common . '\\', $class);
  570. if (class_exists($class)) {
  571. $object = $this->__get($class);
  572. } else {
  573. throw new ClassNotFoundException('class not exists:' . $class, $class);
  574. }
  575. }
  576. $this->__set($guid, $class);
  577. return $object;
  578. }
  579. /**
  580. * 实例化(分层)模型
  581. * @access public
  582. * @param string $name Model名称
  583. * @param string $layer 业务层名称
  584. * @param bool $appendSuffix 是否添加类名后缀
  585. * @param string $common 公共模块名
  586. * @return Model
  587. * @throws ClassNotFoundException
  588. */
  589. public function model($name = '', $layer = 'model', $appendSuffix = false, $common = 'common')
  590. {
  591. return $this->create($name, $layer, $appendSuffix, $common);
  592. }
  593. /**
  594. * 实例化(分层)控制器 格式:[模块名/]控制器名
  595. * @access public
  596. * @param string $name 资源地址
  597. * @param string $layer 控制层名称
  598. * @param bool $appendSuffix 是否添加类名后缀
  599. * @param string $empty 空控制器名称
  600. * @return object
  601. * @throws ClassNotFoundException
  602. */
  603. public function controller($name, $layer = 'controller', $appendSuffix = false, $empty = '')
  604. {
  605. list($module, $class) = $this->parseModuleAndClass($name, $layer, $appendSuffix);
  606. if (class_exists($class)) {
  607. return $this->make($class, true);
  608. } elseif ($empty && class_exists($emptyClass = $this->parseClass($module, $layer, $empty, $appendSuffix))) {
  609. return $this->make($emptyClass, true);
  610. }
  611. throw new ClassNotFoundException('class not exists:' . $class, $class);
  612. }
  613. /**
  614. * 实例化验证类 格式:[模块名/]验证器名
  615. * @access public
  616. * @param string $name 资源地址
  617. * @param string $layer 验证层名称
  618. * @param bool $appendSuffix 是否添加类名后缀
  619. * @param string $common 公共模块名
  620. * @return Validate
  621. * @throws ClassNotFoundException
  622. */
  623. public function validate($name = '', $layer = 'validate', $appendSuffix = false, $common = 'common')
  624. {
  625. $name = $name ?: $this->config('default_validate');
  626. if (empty($name)) {
  627. return new Validate;
  628. }
  629. return $this->create($name, $layer, $appendSuffix, $common);
  630. }
  631. /**
  632. * 数据库初始化
  633. * @access public
  634. * @param mixed $config 数据库配置
  635. * @param bool|string $name 连接标识 true 强制重新连接
  636. * @return \think\db\Query
  637. */
  638. public function db($config = [], $name = false)
  639. {
  640. return Db::connect($config, $name);
  641. }
  642. /**
  643. * 远程调用模块的操作方法 参数格式 [模块/控制器/]操作
  644. * @access public
  645. * @param string $url 调用地址
  646. * @param string|array $vars 调用参数 支持字符串和数组
  647. * @param string $layer 要调用的控制层名称
  648. * @param bool $appendSuffix 是否添加类名后缀
  649. * @return mixed
  650. * @throws ClassNotFoundException
  651. */
  652. public function action($url, $vars = [], $layer = 'controller', $appendSuffix = false)
  653. {
  654. $info = pathinfo($url);
  655. $action = $info['basename'];
  656. $module = '.' != $info['dirname'] ? $info['dirname'] : $this->request->controller();
  657. $class = $this->controller($module, $layer, $appendSuffix);
  658. if (is_scalar($vars)) {
  659. if (strpos($vars, '=')) {
  660. parse_str($vars, $vars);
  661. } else {
  662. $vars = [$vars];
  663. }
  664. }
  665. return $this->invokeMethod([$class, $action . $this->config('action_suffix')], $vars);
  666. }
  667. /**
  668. * 解析应用类的类名
  669. * @access public
  670. * @param string $module 模块名
  671. * @param string $layer 层名 controller model ...
  672. * @param string $name 类名
  673. * @param bool $appendSuffix
  674. * @return string
  675. */
  676. public function parseClass($module, $layer, $name, $appendSuffix = false)
  677. {
  678. $name = str_replace(['/', '.'], '\\', $name);
  679. $array = explode('\\', $name);
  680. $class = Loader::parseName(array_pop($array), 1) . ($this->suffix || $appendSuffix ? ucfirst($layer) : '');
  681. $path = $array ? implode('\\', $array) . '\\' : '';
  682. return $this->namespace . '\\' . ($module ? $module . '\\' : '') . $layer . '\\' . $path . $class;
  683. }
  684. /**
  685. * 获取框架版本
  686. * @access public
  687. * @return string
  688. */
  689. public function version()
  690. {
  691. return static::VERSION;
  692. }
  693. /**
  694. * 是否为调试模式
  695. * @access public
  696. * @return bool
  697. */
  698. public function isDebug()
  699. {
  700. return $this->appDebug;
  701. }
  702. /**
  703. * 获取模块路径
  704. * @access public
  705. * @return string
  706. */
  707. public function getModulePath()
  708. {
  709. return $this->modulePath;
  710. }
  711. /**
  712. * 设置模块路径
  713. * @access public
  714. * @param string $path 路径
  715. * @return void
  716. */
  717. public function setModulePath($path)
  718. {
  719. $this->modulePath = $path;
  720. $this->env->set('module_path', $path);
  721. }
  722. /**
  723. * 获取应用根目录
  724. * @access public
  725. * @return string
  726. */
  727. public function getRootPath()
  728. {
  729. return $this->rootPath;
  730. }
  731. /**
  732. * 获取应用类库目录
  733. * @access public
  734. * @return string
  735. */
  736. public function getAppPath()
  737. {
  738. if (is_null($this->appPath)) {
  739. $this->appPath = Loader::getRootPath() . 'application' . DIRECTORY_SEPARATOR;
  740. }
  741. return $this->appPath;
  742. }
  743. /**
  744. * 获取应用运行时目录
  745. * @access public
  746. * @return string
  747. */
  748. public function getRuntimePath()
  749. {
  750. return $this->runtimePath;
  751. }
  752. /**
  753. * 获取核心框架目录
  754. * @access public
  755. * @return string
  756. */
  757. public function getThinkPath()
  758. {
  759. return $this->thinkPath;
  760. }
  761. /**
  762. * 获取路由目录
  763. * @access public
  764. * @return string
  765. */
  766. public function getRoutePath()
  767. {
  768. return $this->routePath;
  769. }
  770. /**
  771. * 获取应用配置目录
  772. * @access public
  773. * @return string
  774. */
  775. public function getConfigPath()
  776. {
  777. return $this->configPath;
  778. }
  779. /**
  780. * 获取配置后缀
  781. * @access public
  782. * @return string
  783. */
  784. public function getConfigExt()
  785. {
  786. return $this->configExt;
  787. }
  788. /**
  789. * 获取应用类库命名空间
  790. * @access public
  791. * @return string
  792. */
  793. public function getNamespace()
  794. {
  795. return $this->namespace;
  796. }
  797. /**
  798. * 设置应用类库命名空间
  799. * @access public
  800. * @param string $namespace 命名空间名称
  801. * @return $this
  802. */
  803. public function setNamespace($namespace)
  804. {
  805. $this->namespace = $namespace;
  806. return $this;
  807. }
  808. /**
  809. * 是否启用类库后缀
  810. * @access public
  811. * @return bool
  812. */
  813. public function getSuffix()
  814. {
  815. return $this->suffix;
  816. }
  817. /**
  818. * 获取应用开启时间
  819. * @access public
  820. * @return float
  821. */
  822. public function getBeginTime()
  823. {
  824. return $this->beginTime;
  825. }
  826. /**
  827. * 获取应用初始内存占用
  828. * @access public
  829. * @return integer
  830. */
  831. public function getBeginMem()
  832. {
  833. return $this->beginMem;
  834. }
  835. }