App.php 27 KB

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