<?php<liu21st@gmail.com>
namespace think\route\dispatch;
use ReflectionMethod;
use think\Container;
use think\exception\ClassNotFoundException;
use think\exception\HttpException;
use think\Loader;
use think\route\Dispatch;
class Module extends Dispatch
{
protected $controller;
protected $actionName;
public function __construct($dispatch, $param = [], $convert = null)
{
$this->app = Container::get('app');
$this->dispatch = $dispatch;
$this->param = $param;
$this->convert = $convert;
$this->init();
}
protected function init()
{
$result = $this->dispatch;
if (is_string($result)) {
$result = explode('/', $result);
}
if ($this->app->config('app.app_multi_module')) {
$module = strip_tags(strtolower($result[0] ?: $this->app->config('app.default_module')));
$bind = $this->app['route']->getBind();
$available = false;
if ($bind && preg_match('/^[a-z]/is', $bind)) {
list($bindModule) = explode('/', $bind);
if (empty($result[0])) {
$module = $bindModule;
}
$available = true;
} elseif (!in_array($module, $this->app->config('app.deny_module_list')) && is_dir($this->app->getAppPath() . $module)) {
$available = true;
} elseif ($this->app->config('app.empty_module')) {
$module = $this->app->config('app.empty_module');
$available = true;
}
if ($module && $available) {
$this->app['request']->module($module);
$this->app->init($module);
$this->app['lang']->load($this->app->getAppPath() . $module . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $this->app['request']->langset() . '.php');
$this->app['request']->cache(
$this->app->config('app.request_cache'),
$this->app->config('app.request_cache_expire'),
$this->app->config('app.request_cache_except')
);
} else {
throw new HttpException(404, 'module not exists:' . $module);
}
}
$convert = is_bool($this->convert) ? $this->convert : $this->app->config('app.url_convert');
$controller = strip_tags($result[1] ?: $this->app->config('app.default_controller'));
$this->controller = $convert ? strtolower($controller) : $controller;
$this->actionName = strip_tags($result[2] ?: $this->app->config('app.default_action'));
$this->app['request']->controller(Loader::parseName($this->controller, 1))->action($this->actionName);
}
public function run()
{
$this->app['hook']->listen('module_init');
try {
$instance = $this->app->controller($this->controller,
$this->app->config('app.url_controller_layer'),
$this->app->config('app.controller_suffix'),
$this->app->config('app.empty_controller'));
} catch (ClassNotFoundException $e) {
throw new HttpException(404, 'controller not exists:' . $e->getClass());
}
$action = $this->actionName . $this->app->config('app.action_suffix');
if (is_callable([$instance, $action])) {
$call = [$instance, $action];
$reflect = new ReflectionMethod($instance, $action);
$methodName = $reflect->getName();
$suffix = $this->app->config('app.action_suffix');
$actionName = $suffix ? substr($methodName, 0, -strlen($suffix)) : $methodName;
$this->app['request']->action($actionName);
$vars = $this->app->config('app.url_param_type')
? $this->app['request']->route()
: $this->app['request']->param();
} elseif (is_callable([$instance, '_empty'])) {
$call = [$instance, '_empty'];
$vars = [$this->actionName];
$reflect = new ReflectionMethod($instance, '_empty');
} else {
throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()');
}
$this->app['hook']->listen('action_begin', $call);
return Container::getInstance()->invokeReflectMethod($instance, $reflect, $vars);
}
}