2013-04-23 94 views
0

我試圖在我的CakePHP項目中使用Swagger-PHP,我遇到了一些問題。有人設置了嗎?你有什麼建議嗎?我已經通過作曲家成功安裝了swagger-php,並將其加載到我的控制器中(參見下文)。我試圖通過網絡視圖呈現規範,我不太確定爲什麼註冊表沒有被填充或甚至需要。Swagger-PHP和CakePHP

以下是ApiController.php

結果

object(Swagger\Swagger) { 
    resourceList => array() 
    registry => array() 
    models => array() 
    [protected] fileList => array(
     (int) 0 => '~/Sites/com/sitename-api/app/Model/[ModelName].php', 
     ... All of my models 
    ) 
    [protected] path => '~/Sites/com/sitename-api/app/Model' 
    [protected] excludePath => '~/Sites/com/sitename-api/app/Model/Behavior' 
    [protected] cache => object(Doctrine\Common\Cache\ArrayCache) { 
     [private] data => array(
      'DoctrineNamespaceCacheKey[]' => (int) 1, 
      '[][1]' => 'a:4:{s:8:"registry";a:0:{}s:6:"models";a:0:{}s:4:"path";N;s:11:"excludePath";N;}', 
      '[cd9db43f54f6017ba1a20037c1577eb4d2017868][1]' => 'a:4:{s:8:"registry";a:0:{}s:6:"models";a:0:{}s:4:"path";s:56:"~/Sites/com/sitename-api/app/Model";s:11:"excludePath";s:65:"~/Sites/com/sitename-api/app/Model/Behavior";}' 
     ) 
    } 
    [protected] cacheKey => 'cd9db43f54f6017ba1a20037c1577eb4d2017868' 
} 

所以

use Swagger\Annotations as SWG; 
use Swagger\Swagger; 

public function swagger(){ 
    $path = APP . 'Model'; //Path to the app directory 
$swagger = Swagger::discover($path,APP . 'Model/Behavior'); 
debug($swagger); 
//$swagger->jsonEncode($swagger->registry['/api']); 
$swagResults = $swagger->registry; 
debug($swagResults); 
$this->set(array(
    'results' => $swagResults, 
    '_serialize' => 'results' 
)); 
} 

內,基本上$ swagResults是空的,我猜這應該不是吧?

回答

2

我寫的生成所有招搖文件(要求招搖的PHP 0.6或更新版本)一個控制器:

<?php 
use Swagger\Swagger; 
class SwaggerController extends AppController { 

    function index() { 
     $swagger = Swagger::discover(APP, TMP.':'.APP.'Vendor'); 

     $this->autoRender = false; 
     if (isset($this->request->query['resource'])) { 
      return $swagger->getResource($this->request->query['resource']); 
     } 
     $list = array(
      "apiVersion" => "1.0", 
      "swaggerVersion" => "1.1", 
      "basePath" => Router::url(array('?' => array('resource' =>'')), true), 
      "apis" => array() 
     ); 
     foreach ($swagger->registry as $name => $resource) { 
      $item = array("path" => $name); 
      foreach ($resource->apis as $api) { 
       if ($api->description !== null) { 
        $item['description'] = $api->description; 
        break; 
       } 
      } 
      $list['apis'][] = $item; 
     } 
     return json_encode($list); 
    } 
} 
0

謝謝您的回答鮑勃,我意識到如何真正排除目錄。無論如何,以下工作到目前爲止。現在我只需要更好地處理實際的Swagger規範。

在模型中,添加以下內容:

use Swagger\Annotations as SWG; 

/** 
* User Model 
* @SWG\Model(
*  id="User", 
*  description="Defines a user." 
*) 
*/ 

在控制器中添加以下。注意:controller_name是傳遞給swagger方法的東西。

use Swagger\Annotations as SWG; 

/** 
* @SWG\Resource(
*  resourcePath="/users" 
*) 
*/ 

在API控制器中構建類似以下的方法。

/** 
* swagger method 
* This method renders the Swagger spec 
* @param string $controller The controller a.k.a resource to pull Swagger docs for 
* @return array 
*/ 
public function swagger($controller = ''){ 
    if(!empty($resource)) { 
     $this->request->query['resource'] = '/'.$controller; 
    } 
    $path = APP; //Path to the app directory 
    $path = substr($path, 0, -1); 
    $swagger = Swagger::discover(
      $path, 
      APP . 'Plugin:' . 
      APP . 'Vendor:' . 
      APP . 'Config:' . 
      APP . 'Test:' . 
      APP . 'Console:' . 
      //APP . 'Model:' . 
      APP . 'View/Helper:' . 
      APP . 'Controller/Component:' . 
      APP . 'webroot:' . 
      APP . 'tmp:' . 
      APP . 'index.php:' . 
      'libs:' . 
      'plugins:' . 
      'vendors' 
     ); 
    $swagger->setDefaultApiVersion(Configure::read('CC.version')); 
    $swagger->setDefaultBasePath(Configure::read('CC.site_url') . DS . Configure::read('CC.version')); 
    $swagger->setDefaultSwaggerVersion(SWAGGER_VERSION); 
    $this->autoRender = false; 
    if (isset($this->request->query['resource'])) { 
     return $swagger->getResource($this->request->query['resource']); 
    } 
    $list = array(
     "apiVersion" => API_VERSION, 
     "swaggerVersion" => "1.1", 
     "basePath" => Router::url(array('?' => array('resource' =>'')), true), 
     "apis" => array() 
    ); 
    if (isset($this->request->query['resource'])) { 
     return $swagger->getResource($this->request->query['resource']); 
    } 
    $list['apis'][] = $swagger->registry; 
    $this->set(array(
     'results' => $list, 
     '_serialize' => 'results' 
    )); 
} 

的方法評論可能看起來像:

/** 
* info method 
* This provides an app with basic user information. 
* @param int id The user id 
* @return array 
* @SWG\Api(
*  path="https://stackoverflow.com/users/info/{user_id}.{format}", 
*  description="This provides an app with basic user information.", 
*  @SWG\Operations(
*   @SWG\Operation(
*    httpMethod="GET", 
*    summary="User Basic Info", 
*    notes="", 
*    responseClass="List[User]", 
*    nickname="getUserInfo", 
*    group="users", 
*    @SWG\Parameters(
*     @SWG\Parameter(
*      name="format", 
*      description="The format that the data will be returned in.", 
*      paramType="path", 
*      required="true", 
*      allowMultiple="false", 
*      dataType="Array", 
*      @SWG\AllowableValues(
*       valueType="LIST", 
*       values="['json', 'xml']" 
*      ) 
*     ), 
*     @SWG\Parameter(
*      name="user_id", 
*      description="The user id", 
*      paramType="path", 
*      required="true", 
*      allowMultiple="false", 
*      dataType="int" 
*     ), 
*     @SWG\Parameter(
*      name="client_id", 
*      description="Your client id", 
*      paramType="query", 
*      required="true", 
*      allowMultiple="false", 
*      dataType="string", 
*      threescale_name="client_ids" 
*     ) 
*    ), 
*    @SWG\ErrorResponses(
*     @SWG\ErrorResponse(
*      code="404", 
*      reason="User not found" 
*     ) 
*    ) 
*   ) 
*  ) 
* ) 
*/ 

注:threescale_name和團體定製的操作和參數,我已經增加。只要將這些添加到zircote/swagger-php/library/Swagger/Annotations /(Parameter and Operation).php文件中即可。這些是3scale.net特定的項目。