Exemple #1
0
 public function render()
 {
     $service = DI()->request->get('service', 'Default.Index');
     $rules = array();
     $returns = array();
     $description = '';
     $descComment = '//请使用@desc 注释';
     $typeMaps = array('string' => '字符串', 'int' => '整型', 'float' => '浮点型', 'boolean' => '布尔型', 'date' => '日期', 'array' => '数组', 'fixed' => '固定值', 'enum' => '枚举类型', 'object' => '对象');
     try {
         $api = PhalApi_ApiFactory::generateService(false);
         $rules = $api->getApiRules();
     } catch (PhalApi_Exception $ex) {
         $service .= ' - ' . $ex->getMessage();
         include dirname(__FILE__) . '/api_desc_tpl.php';
         return;
     }
     list($className, $methodName) = explode('.', $service);
     $className = 'Api_' . $className;
     $rMethod = new ReflectionMethod($className, $methodName);
     $docComment = $rMethod->getDocComment();
     $docCommentArr = explode("\n", $docComment);
     foreach ($docCommentArr as $comment) {
         $comment = trim($comment);
         //标题描述
         if (empty($description) && strpos($comment, '@') === false && strpos($comment, '/') === false) {
             $description = substr($comment, strpos($comment, '*') + 1);
             continue;
         }
         //@desc注释
         $pos = stripos($comment, '@desc');
         if ($pos !== false) {
             $descComment = substr($comment, $pos + 5);
             continue;
         }
         //@return注释
         $pos = stripos($comment, '@return');
         if ($pos === false) {
             continue;
         }
         $returnCommentArr = explode(' ', substr($comment, $pos + 8));
         if (count($returnCommentArr) < 2) {
             continue;
         }
         if (!isset($returnCommentArr[2])) {
             $returnCommentArr[2] = '';
             //可选的字段说明
         }
         $returns[] = $returnCommentArr;
     }
     include dirname(__FILE__) . '/api_desc_tpl.php';
 }
Exemple #2
0
 /**
 * 响应操作
 *
 * 通过工厂方法创建合适的控制器,然后调用指定的方法,最后返回格式化的数据。
 *
 * @return mixed 根据配置的或者手动设置的返回格式,将结果返回
 *  其结果包含以下元素:
 ```
 *  array(
 *      'ret'   => 200,	            //服务器响应状态
 *      'data'  => array(),	        //正常并成功响应后,返回给客户端的数据	
 *      'msg'   => '',		        //错误提示信息
 *  );
 ```
 */
 public function response()
 {
     $rs = DI()->response;
     try {
         $api = PhalApi_ApiFactory::generateService();
         $service = DI()->request->get('service', 'Default.Index');
         list($apiClassName, $action) = explode('.', $service);
         $rs->setData(call_user_func(array($api, $action)));
     } catch (PhalApi_Exception $ex) {
         $rs->setRet($ex->getCode());
         $rs->setMsg($ex->getMessage());
     } catch (Exception $ex) {
         throw $ex;
     }
     return $rs;
 }
Exemple #3
0
 /**
  * @param string $url 请求的链接
  * @param array $param 额外POST的数据
  * @return array 接口的返回结果
  */
 public static function go($url, $params = array())
 {
     parse_str($url, $urlParams);
     $params = array_merge($urlParams, $params);
     if (!isset($params['service'])) {
         throw new Exception('miss service in url');
     }
     DI()->request = new PhalApi_Request($params);
     $apiObj = PhalApi_ApiFactory::generateService(true);
     list($api, $action) = explode('.', $urlParams['service']);
     $rs = $apiObj->{$action}();
     /**
             $this->assertNotEmpty($rs);
             $this->assertArrayHasKey('code', $rs);
             $this->assertArrayHasKey('msg', $rs);
     */
     return $rs;
 }
 /**
  * @expectedException PhalApi_Exception_BadRequest
  */
 public function testIllegalServiceName()
 {
     $data['service'] = 'Default';
     DI()->request = new PhalApi_Request($data);
     $rs = PhalApi_ApiFactory::generateService();
 }
 /**
  * @expectedException PhalApi_Exception_InternalServerError
  */
 public function testNotPhalApiSubclass()
 {
     $data['service'] = 'Crazy.What';
     DI()->request = new PhalApi_Request($data);
     $rs = PhalApi_ApiFactory::generateService();
 }