   'phpDocumentor\Descriptor\FileDescriptor * hash 3f5b5d76c308eb9b813f026486a6b076 * pathRoute/NormalRoute.php	 * sourceK<?php
/**
 * NormalRoute
 *
 * @author camera360_server@camera360.com
 * @copyright Chengdu pinguo Technology Co.,Ltd.
 */

namespace PG\MSF\Route;

/**
 * Class NormalRoute
 * @package PG\MSF\Route
 */
class NormalRoute implements IRoute
{
    /**
     * @var bool 是否开启路由缓存
     */
    public $enableCache = true;

    /**
     * @var array 路由缓存
     */
    public $routeCache;

    /**
     * @var \stdClass 请求的路由相关信息
     */
    protected $routePrams;

    /**
     * @var string 控制器完全命名空间类名
     */
    public $controllerClassName;

    /**
     * NormalRoute constructor.
     */
    public function __construct()
    {
        $this->routePrams = new \stdClass();
    }

    /**
     * HTTP请求解析
     *
     * @param \swoole_http_client $request 请求对象
     */
    public function handleHttpRequest($request)
    {
        $this->routePrams->path = rtrim($request->server['path_info'], '/');
        $this->routePrams->verb = $this->parseVerb($request);
        $this->setParams($request->get ?? []);

        if (isset($request->header['x-rpc']) && $request->header['x-rpc'] == 1) {
            $this->routePrams->isRpc          = true;
            $this->routePrams->params         = $request->post ?? $request->get ?? [];
            $this->routePrams->controllerName = 'Rpc';
            $this->routePrams->methodName     = 'Index';
            $this->controllerClassName        = '\PG\MSF\Controllers\Rpc';
            $this->routePrams->path           = '/Rpc/Index';
        } else {
            $this->parsePath($this->routePrams->path);
        }
    }

    /**
     * 计算Controller Class Name
     *
     * @return bool
     */
    public function findControllerClassName()
    {
        $this->controllerClassName = '';
        do {
            $className = "\\App\\Controllers\\" . $this->routePrams->controllerName;
            if (class_exists($className)) {
                $this->controllerClassName = $className;
                break;
            }

            $className = "\\PG\\MSF\\Controllers\\" . $this->routePrams->controllerName;
            if (class_exists($className)) {
                $this->controllerClassName = $className;
                break;
            }

            $className = "\\App\\Console\\" . $this->routePrams->controllerName;
            if (class_exists($className)) {
                $this->controllerClassName = $className;
                break;
            }
        } while (0);

        if ($this->controllerClassName  == '') {
            return false;
        }

        return true;
    }

    /**
     * 解析请求的URL PATH
     *
     * @param string $path 待解析URL Path
     * @return bool
     */
    public function parsePath($path)
    {
        if ($this->getEnableCache() && isset($this->routeCache[$path])) {
            $this->routePrams->controllerName = $this->routeCache[$path][0];
            $this->routePrams->methodName     = $this->routeCache[$path][1];
            $this->controllerClassName        = $this->routeCache[$path][2];
        } else {
            $route = explode('/', ltrim($path, '/'));
            $route = array_map(function ($name) {
                if (strpos($name, '-') !== false) { // 中横线模式处理.
                    $slices = array_map('ucfirst', explode('-', $name));
                    $name = '';
                    foreach ($slices as $slice) {
                        $name .= $slice;
                    }
                } else {
                    $name = ucfirst($name);
                }
                return $name;
            }, $route);
            if (count($route) > 1) {
                $methodName = array_pop($route);
            } else {
                $methodName = getInstance()->config->get('http.default_method', 'Index');
            }
            $this->routePrams->controllerName = ltrim(implode("\\", $route), "\\") ?? null;
            $this->routePrams->methodName     = $methodName;
            $this->controllerClassName        = '';

            if ($this->findControllerClassName()) {
                return true;
            }

            $methodDefault  = getInstance()->config->get('http.default_method', 'Index');
            $controllerName = $this->routePrams->controllerName  . "\\" . $this->getMethodName();
            $this->setControllerName($controllerName);
            $this->setMethodName($methodDefault);

            if ($this->findControllerClassName()) {
                return true;
            }

            return false;
        }
    }

    /**
     * 解析请求的方法
     *
     * @param \swoole_http_request $request 请求对象
     * @return string
     */
    public function parseVerb($request)
    {
        if (isset($request->server['http_x_http_method_override'])) {
            return strtoupper($request->server['http_x_http_method_override']);
        }
        if (isset($request->server['request_method'])) {
            return strtoupper($request->server['request_method']);
        }

        return 'GET';
    }

    /**
     * 获取控制器名称
     *
     * @return string
     */
    public function getControllerName()
    {
        return $this->routePrams->controllerName;
    }

    /**
     * 获取请求对应的控制器完全命名空间类名
     *
     * @return string
     */
    public function getControllerClassName()
    {
        return $this->controllerClassName;
    }

    /**
     * 获取方法名称
     *
     * @return string
     */
    public function getMethodName()
    {
        return $this->routePrams->methodName;
    }

    /**
     * 获取请求的PATH
     *
     * @return string
     */
    public function getPath()
    {
        return $this->routePrams->path;
    }

    /**
     * 判断请求是否为RPC请求
     *
     * @return bool
     */
    public function getIsRpc()
    {
        return $this->routePrams->isRpc ?? false;
    }

    /**
     * 获取请求的方法
     *
     * @return string|null
     */
    public function getVerb()
    {
        return $this->routePrams->verb ?? null;
    }

    /**
     * 获取请求的参数
     *
     * @return array
     */
    public function getParams()
    {
        return $this->routePrams->params ?? [];
    }

    /**
     * 设置请求的控制器标识
     *
     * @param string $name 控制器标识
     * @return $this
     */
    public function setControllerName($name)
    {
        $this->routePrams->controllerName = $name;
        return $this;
    }

    /**
     * 设置请求控制器的方法标识
     *
     * @param string $name 控制器的方法标识
     * @return $this
     */
    public function setMethodName($name)
    {
        $this->routePrams->methodName = $name;
        return $this;
    }

    /**
     * 设置请求的参数
     *
     * @param array $params 请求的参数
     * @return $this
     */
    public function setParams($params)
    {
        $this->routePrams->params = $params;
        return $this;
    }

    /**
     * 获取是否支持路由Cache
     *
     * @return bool
     */
    public function getEnableCache()
    {
        return $this->enableCache;
    }

    /**
     * 缓存路由
     *
     * @param string $path URL Path
     * @param array $callable 路由解析结果
     * @return $this
     */
    public function setRouteCache($path, $callable)
    {
        $this->routeCache[$path] = $callable;
        return $this;
    }

    /**
     * 获取已缓存的路由信息
     *
     * @param string $path URL Path
     * @return mixed|null
     */
    public function getRouteCache($path)
    {
        return $this->routeCache[$path] ?? null;
    }
}
 * namespaceAliases#phpDocumentor\Descriptor\Collection * items  * includes	  * constants	  * functions	 
 * classes	\PG\MSF\Route\NormalRoute(phpDocumentor\Descriptor\ClassDescriptor	 * parent * implements	\PG\MSF\Route\IRoute * abstract * final	  * properties	enableCache+phpDocumentor\Descriptor\PropertyDescriptor" * types 
 * defaulttrue	 * static * visibilitypublic * fqsen&\PG\MSF\Route\NormalRoute::enableCache * name * namespace 
 * package
 * summary * description * fileDescriptor  * line * tags	var	 *phpDocumentor\Descriptor\Tag\VarDescriptor * variableName	 /phpDocumentor\Descriptor\Type\BooleanDescriptor  ($是否开启路由缓存	 * errors	 -	  * inheritedElement 
routeCache"  %\PG\MSF\Route\NormalRoute::routeCache /! "#$% &'	(	 )*	 3phpDocumentor\Descriptor\Type\UnknownTypeDescriptor array ($路由缓存-	 -	 . 
routePrams"  	protected%\PG\MSF\Route\NormalRoute::routePrams 4! "#$% &'	(	 )*	 1 	\stdClass ($请求的路由相关信息-	 -	 . controllerClassName"  .\PG\MSF\Route\NormalRoute::controllerClassName 9! "#$% &#'	(	 )*	 .phpDocumentor\Descriptor\Type\StringDescriptor  ($!控制器完全命名空间类名-	 -	 . 
 * methods	__construct)phpDocumentor\Descriptor\MethodDescriptor" * arguments	 (\PG\MSF\Route\NormalRoute::__construct() >! "#NormalRoute constructor.$% &('	return	 param	 -	 . handleHttpRequest?"@	$request+phpDocumentor\Descriptor\ArgumentDescriptor	 * method"S	 1 \swoole_http_client  * byReference * isVariadic F! "#$请求对象% & '	 -	 . .\PG\MSF\Route\NormalRoute::handleHttpRequest() E! "#HTTP请求解析$% &2'	D	 ,phpDocumentor\Descriptor\Tag\ParamDescriptor*F"W D$L-	 C	 -	 . findControllerClassName?"@	 4\PG\MSF\Route\NormalRoute::findControllerClassName() P! "#计算Controller Class Name$% &I'	C	 -phpDocumentor\Descriptor\Tag\ReturnDescriptor	 +  C$-	 D	 -	 . 	parsePath?"@	$pathGH"z	 ;  JK U! "#$待解析URL Path% & '	 -	 . &\PG\MSF\Route\NormalRoute::parsePath() T! "#解析请求的URL PATH$% &m'	D	 O*U"~ D$V-	 C	 S	 +  C$-	 -	 . 	parseVerb?"@	FGH"	 1 \swoole_http_request JK F! "#$L% & '	 -	 . &\PG\MSF\Route\NormalRoute::parseVerb() Y! "#解析请求的方法$% &'	D	 O*F" D$L-	 C	 S	 ;  C$-	 -	 . getControllerName?"@	 .\PG\MSF\Route\NormalRoute::getControllerName() ]! "#获取控制器名称$% &'	C	 S	 ;  C$-	 D	 -	 . getControllerClassName?"@	 3\PG\MSF\Route\NormalRoute::getControllerClassName() `! "#6获取请求对应的控制器完全命名空间类名$% &'	C	 S	 ;  C$-	 D	 -	 . getMethodName?"@	 *\PG\MSF\Route\NormalRoute::getMethodName() c! "#获取方法名称$% &'	C	 S	 ;  C$-	 D	 -	 . getPath?"@	 $\PG\MSF\Route\NormalRoute::getPath() f! "#获取请求的PATH$% &'	C	 S	 ;  C$-	 D	 -	 . getIsRpc?"@	 %\PG\MSF\Route\NormalRoute::getIsRpc() i! "#判断请求是否为RPC请求$% &'	C	 S	 +  C$-	 D	 -	 . getVerb?"@	 $\PG\MSF\Route\NormalRoute::getVerb() l! "#获取请求的方法$% &'	C	 S	 ; 1 null C$-	 D	 -	 . 	getParams?"@	 &\PG\MSF\Route\NormalRoute::getParams() p! "#获取请求的参数$% &'	C	 S	 1 2 C$-	 D	 -	 . setControllerName?"@	$nameGH#*	 ;  JK t! "#$控制器标识% & '	 -	 . .\PG\MSF\Route\NormalRoute::setControllerName() s! "#设置请求的控制器标识$% &'	D	 O*t#. D$u-	 C	 S	 1 $this C$-	 -	 . setMethodName?"@	tGH#F	 ;  JK t! "#$控制器的方法标识% & '	 -	 . *\PG\MSF\Route\NormalRoute::setMethodName() y! "#$设置请求控制器的方法标识$% &'	D	 O*t#J D$z-	 C	 S	 1 x C$-	 -	 . 	setParams?"@	$paramsGH#b	 1 2 JK ~! "#$请求的参数% & '	 -	 . &\PG\MSF\Route\NormalRoute::setParams() }! "#设置请求的参数$% &'	D	 O*~#f D$-	 C	 S	 1 x C$-	 -	 . getEnableCache?"@	 +\PG\MSF\Route\NormalRoute::getEnableCache() ! "#获取是否支持路由Cache$% &'	C	 S	 +  C$-	 D	 -	 . setRouteCache?"@	UGH#	 ;  JK U! "#$URL Path% & '	 -	 . 	$callableGH#	 1 2 JK ! "#$路由解析结果% & '	 -	 . *\PG\MSF\Route\NormalRoute::setRouteCache() ! "#缓存路由$% &('	D	 O*U# D$-	 O*# D$-	 C	 S	 1 x C$-	 -	 . getRouteCache?"@	UGH#	 ;  JK U! "#$% & '	 -	 . *\PG\MSF\Route\NormalRoute::getRouteCache() ! "#获取已缓存的路由信息$% &4'	D	 O*U# D$-	 C	 S	 1 mixed1 o C$-	 -	 .  * usedTraits	  NormalRoute!\PG\MSF\Route"PG\MSF\Route#Class NormalRoute$%" &'	package	 &phpDocumentor\Descriptor\TagDescriptor $-	 
subpackage	 -	 .  * interfaces	 	 * traits	 
 * markers	  NormalRoute.php! "Default#$% & '	author	 -phpDocumentor\Descriptor\Tag\AuthorDescriptor $camera360_server@camera360.com-	 	copyright	  $"Chengdu pinguo Technology Co.,Ltd.-	 	  $-	 	 -	 . 