   'phpDocumentor\Descriptor\FileDescriptor * hash 01075afb2f40a84837130ff43adf4a65 * pathRest/Controller.php	 * source<?php
/**
 * Restful Api控制器基类
 *
 * @author camera360_server@camera360.com
 * @copyright Chengdu pinguo Technology Co.,Ltd.
 */

namespace PG\MSF\Rest;

use PG\Exception\ParameterValidationExpandException;
use PG\Exception\PrivilegeException;
use PG\MSF\Base\Output;
use PG\MSF\Coroutine\CException;

/**
 * Class Controller
 * @package PG\MSF\Rest
 */
class Controller extends \PG\MSF\Controllers\Controller
{
    /**
     * @var string HTTP请求方法
     */
    public $verb = 'GET';
    /**
     * @var array 操作资源集合所支持的HTTP请求方法
     */
    public $collectionOptions = ['GET', 'POST', 'HEAD', 'OPTIONS'];
    /**
     * @var array 操作资源所支持的HTTP请求方法
     */
    public $resourceOptions = ['GET', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'];

    /**
     * 构造方法
     *
     * @param string $controllerName controller标识
     * @param string $methodName method名称
     */
    public function __construct($controllerName, $methodName)
    {
        parent::__construct($controllerName, $methodName);
        $this->verb = $this->getContext()->getInput()->getRequestMethod();
    }

    /**
     * 返回当前请求是否为GET
     *
     * @return bool
     */
    public function getIsGet()
    {
        return $this->verb === 'GET';
    }

    /**
     * 返回当前请求是否为OPTIONS
     *
     * @return bool
     */
    public function getIsOptions()
    {
        return $this->verb === 'OPTIONS';
    }

    /**
     * 返回当前请求是否为HEAD
     *
     * @return bool
     */
    public function getIsHead()
    {
        return $this->verb === 'HEAD';
    }

    /**
     * 返回当前请求是否为POST
     *
     * @return bool
     */
    public function getIsPost()
    {
        return $this->verb === 'POST';
    }

    /**
     * 返回当前请求是否为DELETE
     *
     * @return bool
     */
    public function getIsDelete()
    {
        return $this->verb === 'DELETE';
    }

    /**
     * 返回当前请求是否为PUT
     *
     * @return bool
     */
    public function getIsPut()
    {
        return $this->verb === 'PUT';
    }

    /**
     * 返回当前请求是否为PATCH
     *
     * @return bool
     */
    public function getIsPatch()
    {
        return $this->verb === 'PATCH';
    }

    /**
     * 响应json格式数据
     *
     * @param mixed|null $data 响应数据
     * @param string $message 响应提示
     * @param int $status 响应HTTP状态码
     * @param callable|null $callback jsonp参数名
     * @throws \Exception
     */
    public function outputJson($data = null, $message = '', $status = 200, $callback = null)
    {
        /* @var $output Output */
        $output = $this->getContext()->getOutput();
        // set status in header
        if (!isset(Output::$codes[$status])) {
            throw new \Exception('Http code invalid', 500);
        }
        $output->setStatusHeader($status);
        // 错误信息返回格式可参考：[https://developer.github.com/v3/]
        if ($status != 200 && $message !== '') {
            $data = [
                'message' => $message
            ];
        }
        $result = json_encode($data);
        if (!empty($output->response)) {
            $output->setContentType('application/json; charset=UTF-8');
            $output->end($result);
        }
    }

    /**
     * 响应options请求
     *
     * @param array $options OPTIONS
     */
    public function outputOptions(array $options)
    {
        /* @var $output Output */
        $output = $this->getContext()->getOutput();
        if ($this->verb !== 'OPTIONS') {
            $output->setStatusHeader(405);
        }
        $output->setHeader('Allow', implode(', ', $options));
        if (!empty($output->response)) {
            $output->setContentType('application/json; charset=UTF-8');
            $output->end();
        }
    }

    /**
     * 异常的回调
     *
     * @param \Throwable $e 异常
     * @throws \Throwable
     */
    public function onExceptionHandle(\Throwable $e)
    {
        try {
            if ($e->getPrevious()) {
                $ce = $e->getPrevious();
                $errMsg = dump($ce, false, true);
            } else {
                $errMsg = dump($e, false, true);
                $ce = $e;
            }

            if ($ce instanceof ParameterValidationExpandException) {
                $this->getContext()->getLog()->warning($errMsg . ' with code 401');
                $this->outputJson(parent::$stdClass, $ce->getMessage(), 401);
            } elseif ($ce instanceof PrivilegeException) {
                $this->getContext()->getLog()->warning($errMsg . ' with code 403');
                $this->outputJson(parent::$stdClass, $ce->getMessage(), 403);
            } elseif ($ce instanceof \MongoException) {
                $this->getContext()->getLog()->error($errMsg . ' with code 500');
                $this->outputJson(parent::$stdClass, Output::$codes[500], 500);
            } elseif ($ce instanceof CException) {
                $this->getContext()->getLog()->error($errMsg . ' with code 500');
                $this->outputJson(parent::$stdClass, $ce->getMessage(), 500);
            } else {
                $this->getContext()->getLog()->error($errMsg . ' with code ' . $ce->getCode());
                // set status in header
                if (isset(Output::$codes[$ce->getCode()])) {
                    $this->outputJson(parent::$stdClass, $ce->getMessage(), $ce->getCode());
                } else {
                    $this->outputJson(parent::$stdClass, $ce->getMessage(), 500);
                }
            }
        } catch (\Throwable $ne) {
            getInstance()->log->error('previous exception ' . dump($ce, false, true));
            getInstance()->log->error('handle exception ' . dump($ne, false, true));
        }
    }
}
 * namespaceAliases#phpDocumentor\Descriptor\Collection * items"ParameterValidationExpandException0\PG\Exception\ParameterValidationExpandExceptionPrivilegeException \PG\Exception\PrivilegeExceptionOutput\PG\MSF\Base\Output
CException\PG\MSF\Coroutine\CException * includes	  * constants	  * functions	 
 * classes	\PG\MSF\Rest\Controller(phpDocumentor\Descriptor\ClassDescriptor	 * parent\PG\MSF\Controllers\Controller * implements	  * abstract * final	  * properties	verb+phpDocumentor\Descriptor\PropertyDescriptor" * types 
 * default'GET'	 * static * visibilitypublic * fqsen\PG\MSF\Rest\Controller::verb * name * namespace 
 * package
 * summary * description * fileDescriptor  * line * tags	var	 *phpDocumentor\Descriptor\Tag\VarDescriptor * variableName 	 .phpDocumentor\Descriptor\Type\StringDescriptor (0,HTTP请求方法	 * errors	 5	  * inheritedElement collectionOptions"  !'array('GET', 'POST', 'HEAD', 'OPTIONS')#$%&*\PG\MSF\Rest\Controller::collectionOptions(7) *+,- ./	0	 12 	 3phpDocumentor\Descriptor\Type\UnknownTypeDescriptor(array(0,.操作资源集合所支持的HTTP请求方法5	 5	 6 resourceOptions"  !9array('GET', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS')#$%&(\PG\MSF\Rest\Controller::resourceOptions(=) *+,- .!/	0	 12 	 :(;(0,(操作资源所支持的HTTP请求方法5	 5	 6 
 * methods	__construct)phpDocumentor\Descriptor\MethodDescriptor"#$% * arguments	$controllerName+phpDocumentor\Descriptor\ArgumentDescriptor	 * method"; 	 3 !  * byReference * isVariadic&(E) *+,controller标识- . /	 5	 6 $methodNameFG"; 	 3 ! HI&(K) *+,method名称- . /	 5	 6 &&\PG\MSF\Rest\Controller::__construct()(B) *+构造方法,- .)/	param	 ,phpDocumentor\Descriptor\Tag\ParamDescriptor2E "?(O,J5	 P2K "G(O,L5	 return	 5	 6 getIsGetC"#$%D	 &#\PG\MSF\Rest\Controller::getIsGet()(R) *+返回当前请求是否为GET,- .4/	Q	 -phpDocumentor\Descriptor\Tag\ReturnDescriptor 	 /phpDocumentor\Descriptor\Type\BooleanDescriptor (Q,5	 O	 5	 6 getIsOptionsC"#$%D	 &'\PG\MSF\Rest\Controller::getIsOptions()(W) *+"返回当前请求是否为OPTIONS,- .>/	Q	 U 	 V (Q,5	 O	 5	 6 	getIsHeadC"#$%D	 &$\PG\MSF\Rest\Controller::getIsHead()(Z) *+返回当前请求是否为HEAD,- .H/	Q	 U 	 V (Q,5	 O	 5	 6 	getIsPostC"#$%D	 &$\PG\MSF\Rest\Controller::getIsPost()(]) *+返回当前请求是否为POST,- .R/	Q	 U 	 V (Q,5	 O	 5	 6 getIsDeleteC"#$%D	 &&\PG\MSF\Rest\Controller::getIsDelete()(`) *+!返回当前请求是否为DELETE,- .\/	Q	 U 	 V (Q,5	 O	 5	 6 getIsPutC"#$%D	 &#\PG\MSF\Rest\Controller::getIsPut()(c) *+返回当前请求是否为PUT,- .f/	Q	 U 	 V (Q,5	 O	 5	 6 
getIsPatchC"#$%D	 &%\PG\MSF\Rest\Controller::getIsPatch()(f) *+ 返回当前请求是否为PATCH,- .p/	Q	 U 	 V (Q,5	 O	 5	 6 
outputJsonC"#$%D	$dataFG" 	 :(mixed:(null!lHI&(j) *+,响应数据- . /	 5	 6 $messageFG" 	 3 !''HI&(n) *+,响应提示- . /	 5	 6 $statusFG" 	 /phpDocumentor\Descriptor\Type\IntegerDescriptor !200HI&(q) *+,响应HTTP状态码- . /	 5	 6 	$callbackFG" 	 :(callable:(l!lHI&(u) *+,jsonp参数名- . /	 5	 6 &%\PG\MSF\Rest\Controller::outputJson()(i) *+响应json格式数据,- .~/	O	 P2j "(O,m5	 P2n "(O,p5	 P2q "(O,t5	 P2u "(O,w5	 throws	 -phpDocumentor\Descriptor\Tag\ThrowsDescriptor 	 :(
\Exception(z,5	 Q	 5	 6 outputOptionsC"#$%D	$optionsFG# 	 :(;! HI&(~) *+,OPTIONS- . /	 5	 6 &(\PG\MSF\Rest\Controller::outputOptions()(}) *+响应options请求,- ./	O	 P2~ #(O,5	 Q	 5	 6 onExceptionHandleC"#$%D	$eFG#* 	 :(
\Throwable! HI&() *+,异常- . /	 5	 6 &,\PG\MSF\Rest\Controller::onExceptionHandle()() *+异常的回调,- ./	O	 P2 #.(O,5	 z	 { 	 :((z,5	 Q	 5	 6  * usedTraits	 &(
Controller)\PG\MSF\Rest*PG\MSF\Rest+Class Controller,-" ./	package	 &phpDocumentor\Descriptor\TagDescriptor(,5	 
subpackage	 5	 6  * interfaces	 	 * traits	 
 * markers	 &(Controller.php) *Default+Restful Api控制器基类,- . /	author	 -phpDocumentor\Descriptor\Tag\AuthorDescriptor(,camera360_server@camera360.com5	 	copyright	 (,"Chengdu pinguo Technology Co.,Ltd.5	 	 (,5	 	 5	 6 