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