Arguments
->
miucms
\
classify
\
route
\
dispatch
\
{closure}
()
source
/miucms
/App.class.php
:833
public function flush(): void{
$this->instances = [];
$this->bind = [];
$this->alias = [];
}
/**
* 执行函数或者闭包方法 支持参数调用
* @access public
* @param mixed $function 函数或者闭包
* @param array $vars 参数
* @return mixed
*/
public function invokeFunction($function, array $vars = []) {
try {
$reflect = new ReflectionFunction($function);
$args = $this->bindParams($reflect, $vars);
return call_user_func_array($function, $args);
} catch (ReflectionException $e) {
throw new Exception('function not exists: ' . $function . '()');
}
}
/**
* 调用反射执行类的方法 支持参数绑定
* @access public
* @param mixed $method 方法
* @param array $vars 参数
* @return mixed
*/
public function invokeMethod($method, array $vars = []) {
try {
if (is_array($method)) {
$class = is_object($method[0]) ? $method[0] : $this->invokeClass($method[0]);
$reflect = new ReflectionMethod($class, $method[1]);
} else {
// 静态方法
$reflect = new ReflectionMethod($method);
Arguments
source
/miucms
/App.class.php
:893
* @param object $instance 对象实例
* @param mixed $reflect 反射类
* @param array $vars 参数
* @return mixed
*/
public function invokeReflectMethod($instance, $reflect, array $vars = []) {
$args = $this->bindParams($reflect, $vars);
return $reflect->invokeArgs($instance, $args);
}
/**
* 调用反射执行callable 支持参数绑定
* @access public
* @param mixed $callable
* @param array $vars 参数
* @return mixed
*/
public function invoke($callable, array $vars = []) {
if ($callable instanceof Closure) {
return $this->invokeFunction($callable, $vars);
}
return $this->invokeMethod($callable, $vars);
}
/**
* 调用反射执行类的实例化 支持依赖注入
* @access public
* @param string $class 类名
* @param array $vars 参数
* @return mixed
*/
public function invokeClass(string $class, array $vars = []) {
try {
$reflect = new ReflectionClass($class);
if ($reflect->hasMethod('__make')) {
$method = new ReflectionMethod($class, '__make');
if ($method->isPublic() && $method->isStatic()) {
$args = $this->bindParams($method, $vars);
return $method->invokeArgs(null, $args);
Arguments
source
/miucms
/Middleware.class.php
:191
}
return [[$middleware, 'handle'], $param ?? null];
}
protected function resolve(string $type = 'route') {
return function (Request $request) use ($type) {
$middleware = array_shift($this->queue[$type]);
if (null === $middleware) {
return false;
throw new InvalidArgumentException('The queue was exhausted, with no response returned');
}
list($call, $param) = $middleware;
if (is_array($call) && is_string($call[0])) {
$call = [$this->app->make($call[0]), $call[1]];
}
try {
$response = $this->app->invoke($call, [$request, $this->resolve($type), $param]);
} catch (HttpResponseException $exception) {
$response = $exception->getResponse();
}
if (!$response instanceof Response) {
throw new LogicException('The middleware must return Response instance');
}
return $response;
};
}
}
Arguments
source
/miucms
/Middleware.class.php
:141
}
/**
* 获取注册的中间件
* @access public
* @param string $type 中间件类型
*/
public function all(string $type = 'route'): array
{
return $this->queue[$type] ?? [];
}
/**
* 中间件调度
* @access public
* @param Request $request
* @param string $type 中间件类型
*/
public function dispatch(Request $request, string $type = 'route') {
return call_user_func($this->resolve($type), $request);
}
/**
* 解析中间件
* @access protected
* @param mixed $middleware
* @param string $type 中间件类型
* @return array
*/
protected function buildMiddleware($middleware, string $type = 'route'): array
{
if (is_array($middleware)) {
list($middleware, $param) = $middleware;
}
if ($middleware instanceof \Closure) {
return [$middleware, $param ?? null];
}
if (!is_string($middleware)) {
Arguments
source
/miucms
/classify
/route
/dispatch
/Controller.class.php
:115
$this->request->setAction($actionName);
} catch (ReflectionException $e) {
$reflect = new ReflectionMethod($instance, '__call');
$vars = [$empty_action, $vars];
$this->request->setAction($empty_action);
}
} else {
// 操作不存在
throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()');
}
}
$data = $this->app->invokeReflectMethod($instance, $reflect, $vars);
return $this->autoResponse($data);
});
return $this->app->middleware->dispatch($this->request, 'controller');
}
/**
* 使用反射机制注册控制器中间件
* @access public
* @param object $controller 控制器实例
* @return void
*/
protected function registerControllerMiddleware($controller): void{
$class = new ReflectionClass($controller);
if ($class->hasProperty('middleware')) {
$reflectionProperty = $class->getProperty('middleware');
$reflectionProperty->setAccessible(true);
$middlewares = $reflectionProperty->getValue($controller);
foreach ($middlewares as $key => $val) {
if (!is_int($key)) {
if (isset($val['only']) && !in_array($this->request->action(true), array_map(function ($item) {
Arguments
source
/miucms
/classify
/route
/Dispatch.class.php
:101
$this->request->setRule($this->rule);
// 执行路由后置操作
$this->doRouteAfter();
}
/**
* 执行路由调度
* @access public
* @return mixed
*/
public function run(): Response{
$option = $this->rule->getOption();
// 数据自动验证
if (isset($option['validate'])) {
$this->autoValidate($option['validate']);
}
$data = $this->exec();
return $this->autoResponse($data);
}
protected function autoResponse($data): Response {
if ($data instanceof Response) {
$response = $data;
} elseif (!is_null($data)) {
// 默认自动识别响应输出类型
$type = $this->request->isJson() ? 'json' : 'html';
$response = App::pull('response')->create($data, $type);
} else {
$data = ob_get_clean();
$content = false === $data ? '' : $data;
$status = false === $data ? 204 : 200;
$response = App::pull('response')->create($content, '', $status);
}
return $response;
}
Arguments
source
/miucms
/Route.class.php
:716
$checkCallback = function () use ($request, $withRoute) {
//加载路由
$withRoute();
return $this->check();
};
if ($this->config['route_check_cache']) {
$dispatch = $this->cache->tag('route_cache')->remember($this->getRouteCacheKey($request), $checkCallback);
} else {
$dispatch = $checkCallback();
}
} else {
$dispatch = $this->url($this->path());
}
$dispatch->init($this->app);
$this->app->middleware->add(function () use ($dispatch) {
try {
$response = $dispatch->run();
} catch (HttpResponseException $exception) {
$response = $exception->getResponse();
}
return $response;
});
return $this->app->middleware->dispatch($request);
}
/**
* 获取路由缓存Key
* @access protected
* @param Request $request
* @return string
*/
protected function getRouteCacheKey(Request $request): string {
if (!empty($this->config['route_check_cache_key'])) {
$closure = $this->config['route_check_cache_key'];
$routeKey = $closure($request);
} else {
Arguments
source
/miucms
/App.class.php
:833
public function flush(): void{
$this->instances = [];
$this->bind = [];
$this->alias = [];
}
/**
* 执行函数或者闭包方法 支持参数调用
* @access public
* @param mixed $function 函数或者闭包
* @param array $vars 参数
* @return mixed
*/
public function invokeFunction($function, array $vars = []) {
try {
$reflect = new ReflectionFunction($function);
$args = $this->bindParams($reflect, $vars);
return call_user_func_array($function, $args);
} catch (ReflectionException $e) {
throw new Exception('function not exists: ' . $function . '()');
}
}
/**
* 调用反射执行类的方法 支持参数绑定
* @access public
* @param mixed $method 方法
* @param array $vars 参数
* @return mixed
*/
public function invokeMethod($method, array $vars = []) {
try {
if (is_array($method)) {
$class = is_object($method[0]) ? $method[0] : $this->invokeClass($method[0]);
$reflect = new ReflectionMethod($class, $method[1]);
} else {
// 静态方法
$reflect = new ReflectionMethod($method);
Arguments
source
/miucms
/App.class.php
:893
* @param object $instance 对象实例
* @param mixed $reflect 反射类
* @param array $vars 参数
* @return mixed
*/
public function invokeReflectMethod($instance, $reflect, array $vars = []) {
$args = $this->bindParams($reflect, $vars);
return $reflect->invokeArgs($instance, $args);
}
/**
* 调用反射执行callable 支持参数绑定
* @access public
* @param mixed $callable
* @param array $vars 参数
* @return mixed
*/
public function invoke($callable, array $vars = []) {
if ($callable instanceof Closure) {
return $this->invokeFunction($callable, $vars);
}
return $this->invokeMethod($callable, $vars);
}
/**
* 调用反射执行类的实例化 支持依赖注入
* @access public
* @param string $class 类名
* @param array $vars 参数
* @return mixed
*/
public function invokeClass(string $class, array $vars = []) {
try {
$reflect = new ReflectionClass($class);
if ($reflect->hasMethod('__make')) {
$method = new ReflectionMethod($class, '__make');
if ($method->isPublic() && $method->isStatic()) {
$args = $this->bindParams($method, $vars);
return $method->invokeArgs(null, $args);
Arguments
source
/miucms
/Middleware.class.php
:191
}
return [[$middleware, 'handle'], $param ?? null];
}
protected function resolve(string $type = 'route') {
return function (Request $request) use ($type) {
$middleware = array_shift($this->queue[$type]);
if (null === $middleware) {
return false;
throw new InvalidArgumentException('The queue was exhausted, with no response returned');
}
list($call, $param) = $middleware;
if (is_array($call) && is_string($call[0])) {
$call = [$this->app->make($call[0]), $call[1]];
}
try {
$response = $this->app->invoke($call, [$request, $this->resolve($type), $param]);
} catch (HttpResponseException $exception) {
$response = $exception->getResponse();
}
if (!$response instanceof Response) {
throw new LogicException('The middleware must return Response instance');
}
return $response;
};
}
}
Arguments
source
/miucms
/classify
/middleware
/AllowCrossDomain.class.php
:60
* @param array $header
* @return Response
*/
public function handle(Request $request, Closure $next, ? array $header = []) {
$header = !empty($header) ? array_merge($this->header, $header) : $this->header;
if (!isset($header['Access-Control-Allow-Origin'])) {
$origin = $request->header('origin');
if ($origin && strpos($this->cookieDomain, $origin)) {
$header['Access-Control-Allow-Origin'] = $origin;
} else {
$header['Access-Control-Allow-Origin'] = '*';
}
}
if ($request->isOptions()) {
return App::pull('response')->create()->code(204)->header($header);
}
return $next($request)->header($header);
}
}
Arguments
source
/miucms
/App.class.php
:858
/**
* 调用反射执行类的方法 支持参数绑定
* @access public
* @param mixed $method 方法
* @param array $vars 参数
* @return mixed
*/
public function invokeMethod($method, array $vars = []) {
try {
if (is_array($method)) {
$class = is_object($method[0]) ? $method[0] : $this->invokeClass($method[0]);
$reflect = new ReflectionMethod($class, $method[1]);
} else {
// 静态方法
$reflect = new ReflectionMethod($method);
}
$args = $this->bindParams($reflect, $vars);
return $reflect->invokeArgs($class ?? null, $args);
} catch (ReflectionException $e) {
if (is_array($method)) {
$class = is_object($method[0]) ? get_class($method[0]) : $method[0];
$callback = $class . '::' . $method[1];
} else {
$callback = $method;
}
throw new Exception('method not exists: ' . $callback . '()');
}
}
/**
* 调用反射执行类的方法 支持参数绑定
* @access public
* @param object $instance 对象实例
* @param mixed $reflect 反射类
* @param array $vars 参数
* @return mixed
*/
Arguments
source
/miucms
/App.class.php
:896
* @return mixed
*/
public function invokeReflectMethod($instance, $reflect, array $vars = []) {
$args = $this->bindParams($reflect, $vars);
return $reflect->invokeArgs($instance, $args);
}
/**
* 调用反射执行callable 支持参数绑定
* @access public
* @param mixed $callable
* @param array $vars 参数
* @return mixed
*/
public function invoke($callable, array $vars = []) {
if ($callable instanceof Closure) {
return $this->invokeFunction($callable, $vars);
}
return $this->invokeMethod($callable, $vars);
}
/**
* 调用反射执行类的实例化 支持依赖注入
* @access public
* @param string $class 类名
* @param array $vars 参数
* @return mixed
*/
public function invokeClass(string $class, array $vars = []) {
try {
$reflect = new ReflectionClass($class);
if ($reflect->hasMethod('__make')) {
$method = new ReflectionMethod($class, '__make');
if ($method->isPublic() && $method->isStatic()) {
$args = $this->bindParams($method, $vars);
return $method->invokeArgs(null, $args);
}
}
$constructor = $reflect->getConstructor();
Arguments
source
/miucms
/Middleware.class.php
:191
}
return [[$middleware, 'handle'], $param ?? null];
}
protected function resolve(string $type = 'route') {
return function (Request $request) use ($type) {
$middleware = array_shift($this->queue[$type]);
if (null === $middleware) {
return false;
throw new InvalidArgumentException('The queue was exhausted, with no response returned');
}
list($call, $param) = $middleware;
if (is_array($call) && is_string($call[0])) {
$call = [$this->app->make($call[0]), $call[1]];
}
try {
$response = $this->app->invoke($call, [$request, $this->resolve($type), $param]);
} catch (HttpResponseException $exception) {
$response = $exception->getResponse();
}
if (!$response instanceof Response) {
throw new LogicException('The middleware must return Response instance');
}
return $response;
};
}
}
Arguments
source
/miucms
/classify
/middleware
/SessionInit.class.php
:56
/**
* Session初始化
* @access public
* @param Request $request
* @param Closure $next
* @return void
*/
public function handle($request, Closure $next) {
// Session初始化
$varSessionId = $this->app->config->get('session.var_session_id');
$cookieName = $this->app->config->get('session.name') ?: 'PHPSESSID';
if ($varSessionId && $request->request($varSessionId)) {
$sessionId = $request->request($varSessionId);
} else {
$sessionId = $request->cookie($cookieName) ?: '';
}
$this->session->setId($sessionId);
$request->withSession($this->session);
$response = $next($request)->setSession($this->session);
$this->app->cookie->set($cookieName, $this->session->getId());
// 清空当次请求有效的数据
if (!($response instanceof RedirectResponse)) {
$this->session->flush();
}
return $response;
}
}
Arguments
source
/miucms
/App.class.php
:858
/**
* 调用反射执行类的方法 支持参数绑定
* @access public
* @param mixed $method 方法
* @param array $vars 参数
* @return mixed
*/
public function invokeMethod($method, array $vars = []) {
try {
if (is_array($method)) {
$class = is_object($method[0]) ? $method[0] : $this->invokeClass($method[0]);
$reflect = new ReflectionMethod($class, $method[1]);
} else {
// 静态方法
$reflect = new ReflectionMethod($method);
}
$args = $this->bindParams($reflect, $vars);
return $reflect->invokeArgs($class ?? null, $args);
} catch (ReflectionException $e) {
if (is_array($method)) {
$class = is_object($method[0]) ? get_class($method[0]) : $method[0];
$callback = $class . '::' . $method[1];
} else {
$callback = $method;
}
throw new Exception('method not exists: ' . $callback . '()');
}
}
/**
* 调用反射执行类的方法 支持参数绑定
* @access public
* @param object $instance 对象实例
* @param mixed $reflect 反射类
* @param array $vars 参数
* @return mixed
*/
Arguments
source
/miucms
/App.class.php
:896
* @return mixed
*/
public function invokeReflectMethod($instance, $reflect, array $vars = []) {
$args = $this->bindParams($reflect, $vars);
return $reflect->invokeArgs($instance, $args);
}
/**
* 调用反射执行callable 支持参数绑定
* @access public
* @param mixed $callable
* @param array $vars 参数
* @return mixed
*/
public function invoke($callable, array $vars = []) {
if ($callable instanceof Closure) {
return $this->invokeFunction($callable, $vars);
}
return $this->invokeMethod($callable, $vars);
}
/**
* 调用反射执行类的实例化 支持依赖注入
* @access public
* @param string $class 类名
* @param array $vars 参数
* @return mixed
*/
public function invokeClass(string $class, array $vars = []) {
try {
$reflect = new ReflectionClass($class);
if ($reflect->hasMethod('__make')) {
$method = new ReflectionMethod($class, '__make');
if ($method->isPublic() && $method->isStatic()) {
$args = $this->bindParams($method, $vars);
return $method->invokeArgs(null, $args);
}
}
$constructor = $reflect->getConstructor();
Arguments
source
/miucms
/Middleware.class.php
:191
}
return [[$middleware, 'handle'], $param ?? null];
}
protected function resolve(string $type = 'route') {
return function (Request $request) use ($type) {
$middleware = array_shift($this->queue[$type]);
if (null === $middleware) {
return false;
throw new InvalidArgumentException('The queue was exhausted, with no response returned');
}
list($call, $param) = $middleware;
if (is_array($call) && is_string($call[0])) {
$call = [$this->app->make($call[0]), $call[1]];
}
try {
$response = $this->app->invoke($call, [$request, $this->resolve($type), $param]);
} catch (HttpResponseException $exception) {
$response = $exception->getResponse();
}
if (!$response instanceof Response) {
throw new LogicException('The middleware must return Response instance');
}
return $response;
};
}
}
Arguments
source
/miucms
/Middleware.class.php
:141
}
/**
* 获取注册的中间件
* @access public
* @param string $type 中间件类型
*/
public function all(string $type = 'route'): array
{
return $this->queue[$type] ?? [];
}
/**
* 中间件调度
* @access public
* @param Request $request
* @param string $type 中间件类型
*/
public function dispatch(Request $request, string $type = 'route') {
return call_user_func($this->resolve($type), $request);
}
/**
* 解析中间件
* @access protected
* @param mixed $middleware
* @param string $type 中间件类型
* @return array
*/
protected function buildMiddleware($middleware, string $type = 'route'): array
{
if (is_array($middleware)) {
list($middleware, $param) = $middleware;
}
if ($middleware instanceof \Closure) {
return [$middleware, $param ?? null];
}
if (!is_string($middleware)) {
Arguments
source
/miucms
/Route.class.php
:723
$dispatch = $this->cache->tag('route_cache')->remember($this->getRouteCacheKey($request), $checkCallback);
} else {
$dispatch = $checkCallback();
}
} else {
$dispatch = $this->url($this->path());
}
$dispatch->init($this->app);
$this->app->middleware->add(function () use ($dispatch) {
try {
$response = $dispatch->run();
} catch (HttpResponseException $exception) {
$response = $exception->getResponse();
}
return $response;
});
return $this->app->middleware->dispatch($request);
}
/**
* 获取路由缓存Key
* @access protected
* @param Request $request
* @return string
*/
protected function getRouteCacheKey(Request $request): string {
if (!empty($this->config['route_check_cache_key'])) {
$closure = $this->config['route_check_cache_key'];
$routeKey = $closure($request);
} else {
$routeKey = md5($request->baseUrl(true) . ':' . $request->method());
}
return $routeKey;
}
/**
Arguments
source
/miucms
/Web.class.php
:200
if (is_file(__DATA__ . 'install.lock') == false && is_file(constant('__DATA.CONFIG__') . 'config.inc.php') == false) {
$this->app->config->set('app.auto_multi_app', false);
$this->multi(true)->name('install');
}
if ($this->multi) {
$this->parseMultiApp();
}
// 设置开启事件机制
$this->app->event->withEvent($this->app->config->get('app.with_event', true));
// 监听HttpRun
$this->app->event->trigger('HttpRun');
$withRoute = $this->app->config->get('app.with_route', true) ? function () {
$this->loadRoutes();
} : null;
return $this->app->route->dispatch($request, $withRoute);
}
/**
* 加载路由
* @access protected
* @return void
*/
protected function loadRoutes(): void {
// 加载路由定义
if (is_dir(constant('__CONFIG__') . 'route' . DIR_SEP)) {
$files = glob(constant('__CONFIG__') . 'route' . DIR_SEP . '*.php');
foreach ($files as $file) {
include $file;
}
}
if ($this->app->route->config('route_annotation')) {
$filename = $this->app->getRuntimePath() . 'build_route.php';
if (is_file($filename)) {
include $filename;
Arguments
source
/miucms
/Web.class.php
:149
$path .= DIR_SEP;
}
$this->path = $path;
return $this;
}
/**
* 执行应用程序
* @access public
* @param Request|null $request
* @return Response
*/
public function run(Request $request = null): Response{
//自动创建request对象
$request = $request ?? $this->app->make('request', [], true);
$this->app->instance('request', $request);
try {
$response = $this->runWithRequest($request);
} catch (Throwable $e) {
$this->reportException($e);
$response = $this->renderException($request, $e);
}
return $response->setCookie($this->app->cookie);
}
/**
* 初始化
*/
protected function initialize() {
if (!$this->app->initialized()) {
$this->app->initialize();
}
}
/**
* 执行应用程序
* @param Request $request
Arguments
*/
public static function tpl($tpl = null, $tpldir = false, $vars = [], $config = [], $filter = null) {
if (empty($tpl)) {
return false;
}
if ($tpldir) {
$tpl = ($tpldir === true ? (Request::api() ? 'api' : Request::app()) : $tpldir) . '@' . $tpl;
}
return \miucms\App::pull('response')->create($tpl, 'view', 200)->assign($vars)->config($config)->filter($filter);
}
/**
* 初始化应用程序
*/
public static function creat_app($response = null) {
// 执行HTTP应用并响应
$creat_app = \miucms\App::pull('web');
$response = $creat_app->run();
$response->send();
return $response;
}
// 得到唯一参数
public static function uniqid($key = null) {
$urlhash = md5($key ? $key : uniqid(md5(microtime(true)), true));
$short_list = [];
for ($i = 0; $i < 4; $i++) {
$urlhash_piece = substr($urlhash, $i * 32 / 4, 32 / 4);
$hex = hexdec($urlhash_piece) & 0x3fffffff;
$short_url = '';
for ($j = 0; $j < 3; $j++) {
$short_url .= self::$charset[$hex & 0x0000003d];
$hex = $hex >> 4;
}
array_push($short_list, $short_url);
}
return join($short_list);
}
Arguments
<?php
/**
* ============================================================================
* 版权所有: MIUCMS (中艾科技),并保留所有权利。
* 网站地址: http://www.miucms.com;
* 邮箱 admin@miucms.com
* 应用程序创建类
* ----------------------------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
* 使用;不允许对程序代码以任何形式任何目的的再发布。
*/
require_once __DIR__ . '/source/global';
miucms::creat_app(true);
Arguments