2016-07-01 17 views
3

私はSymfonyフレームワークを使用しており、私のプロジェクトのRESTful APIに自動ドキュメントエンジンを追加しています。Symfony - 注釈は決してインポートされませんでした

いくつかの検索の後、私はapidocエンジン(http://apidocjs.com/)を見つけました。それはかなりシンプルに機能します:あなたはRESTful APIとドキュメンテーションが生成されるすべてのコントローラのためにいくつかの注釈を追加する必要があります。

注釈の例は次のとおりです。

/** 
* @Route("/api/dictionary_list/{userId}/{sessionKey}", name="api/dictionary_list") 
* @api {get} /api/dictionary_list/{userId}/{sessionKey} 01. Values list (ids) for all system dictionaries 
* @apiName Dictionary list 
* @apiGroup Dictionary 
* 
* @apiParam {Integer} userId User's ID received in authorization request 
* @apiParam {String} sessionKey Session key received in authorization request 
* 
* @apiSuccess {Integer} parcelStatuses The name of current dictionary 
* @apiSuccess {String} itemId Item id which used in requests 
* @apiSuccess {String} itemName Item name 
*/ 

public function dictionaryListAction($userId=null, $sessionKey=null) 
{ 
... 
} 

あなたが見ることができるように、apidocのための注釈はsymfonyにルーティングするためのアノテーションと同じです。ところで

本番環境ではうまく動作しますが、開発環境で、私は

[Semantical Error] The annotation "@apiName" in method AppBundle\Controller\Api\apiDictionaryController::dictionaryListAction() was never imported. 

のような例外がこの問題を修正し、apidocのための注釈が無視されなければならないことをsymfonyのに言うことはどのような方法があり得ますか?

+0

であなたのバンドル内のコンパイラパスを登録する必要があり、[ここ](https://github.com/symfony/symfony/issues/17000)も – Matteo

答えて

1

注釈アノテーションを使用すると、Docrine注釈リーダーに、コントローラ内のこの注釈をスキップするよう指示できます。これを行うには、アノテートを@IgnoreAnnotation("Annotation")とclassのclass docコメントに追加するだけです。あなたの場合

/** 
* @IgnoreAnnotation("apiName") 
* @IgnoreAnnotation("apiGroup") 
* @IgnoreAnnotation("apiParam") 
* @IgnoreAnnotation("apiSuccess") 
*/ 
class ActionController extends Controller 


/** 
* @Route("/api/dictionary_list/{userId}/{sessionKey}", name="api/dictionary_list") 
* @api {get} /api/dictionary_list/{userId}/{sessionKey} 01. Values list (ids) for all system dictionaries 
* @apiName Dictionary list 
* @apiGroup Dictionary 
* 
* @apiParam {Integer} userId User's ID received in authorization request 
* @apiParam {String} sessionKey Session key received in authorization request 
* 
* @apiSuccess {Integer} parcelStatuses The name of current dictionary 
* @apiSuccess {String} itemId Item id which used in requests 
* @apiSuccess {String} itemName Item name 
*/ 

public function dictionaryListAction($userId=null, $sessionKey=null) 
{ 
... 
} 

また、デフォルトがthis oneとしてスキップとして、この注釈が含まれるようにdoctrine/annotationsプロジェクトにPRを開くように考えることができます。

このヘルプが必要です。あなたがルートをインポートする必要が

1

symfonyはdoctrine/annotationsパッケージを使用してアノテーションを解析します。ブラックリストに登録されていない不明なアノテーションに遭遇すると、例外がスローされます。

あなたは、追加の注釈をブラックリストDoctrine docs - Ignoring missing exceptionsを見ることができます:それはグローバル設定なので

use Doctrine\Common\Annotations\AnnotationReader; 

AnnotationReader::addGlobalIgnoredName('api'); 
AnnotationReader::addGlobalIgnoredName('apiParam'); 
AnnotationReader::addGlobalIgnoredName('apiGroup'); 
AnnotationReader::addGlobalIgnoredName('apiSuccess'); 

私はアプリ/ autoload.phpでこれを置くところ。

+0

ありがとうございましたが、私はあなたのアドバイスに従うI PHPの致命的なエラー: 'Doctrine \ Common \ Annotations \ AnnotationReader'クラスが/ home/alex/Projects/parcel-search/web-app/app/autoloadに見つかりません。php on line 7.'私が見逃したことは? –

+1

* require vendor/autoload.php *の後で、しかし 'return'の前に置く必要があります。 – ShiraNai7

-1

使用Sensio \バンドル\ FrameworkExtraBundle \構成\ルートを。

1

DIコンテナのコンパイル中に注釈が読み取られるため、compiler passの間にapidocsアノテーションを無視することをお勧めします。

例:

<?php 
namespace YourBundle\DependencyInjection; 

use Doctrine\Common\Annotations\AnnotationReader; 
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 
use Symfony\Component\DependencyInjection\ContainerBuilder; 

class IgnoreApiDocsAnnotationsPass implements CompilerPassInterface { 
    public function process(ContainerBuilder $container) { 
     AnnotationReader::addGlobalIgnoredName('api'); 
     AnnotationReader::addGlobalIgnoredName('apiDefine'); 
     AnnotationReader::addGlobalIgnoredName('apiDeprecated'); 
     AnnotationReader::addGlobalIgnoredName('apiDescription'); 
     AnnotationReader::addGlobalIgnoredName('apiError'); 
     AnnotationReader::addGlobalIgnoredName('apiErrorExample'); 
     AnnotationReader::addGlobalIgnoredName('apiExample'); 
     AnnotationReader::addGlobalIgnoredName('apiGroup'); 
     AnnotationReader::addGlobalIgnoredName('apiHeader'); 
     AnnotationReader::addGlobalIgnoredName('apiHeaderExample'); 
     AnnotationReader::addGlobalIgnoredName('apiIgnore'); 
     AnnotationReader::addGlobalIgnoredName('apiName'); 
     AnnotationReader::addGlobalIgnoredName('apiParam'); 
     AnnotationReader::addGlobalIgnoredName('apiParamExample'); 
     AnnotationReader::addGlobalIgnoredName('apiPermission'); 
     AnnotationReader::addGlobalIgnoredName('apiPrivate'); 
     AnnotationReader::addGlobalIgnoredName('apiSampleRequest'); 
     AnnotationReader::addGlobalIgnoredName('apiSuccess'); 
     AnnotationReader::addGlobalIgnoredName('apiSuccessExample'); 
     AnnotationReader::addGlobalIgnoredName('apiUse'); 
     AnnotationReader::addGlobalIgnoredName('apiVersion'); 
    } 
} 

私はapidoc docsから、アノテーションの完全なリストをobtainted。あなたは議論

<?php 
namespace YourBundle; 

use YourBundle\DependencyInjection\IgnoreApiDocsAnnotationsPass; 
use Symfony\Component\DependencyInjection\ContainerBuilder; 
use Symfony\Component\HttpKernel\Bundle\Bundle; 

class YourBundle extends Bundle { 
    public function build(ContainerBuilder $container) { 
     parent::build($container); 
     $container->addCompilerPass(new IgnoreApiDocsAnnotationsPass()); 
    } 

} 
関連する問題