From 53b2537aefd9d955e67440bc9c92eddcbcf876ac Mon Sep 17 00:00:00 2001 From: Florent DUBOST Date: Tue, 26 Mar 2013 11:49:12 +0100 Subject: [PATCH 1/3] Add cache annotation extractor --- Annotation/ApiDoc.php | 25 +++++++++++++++++++++++++ Extractor/ApiDocExtractor.php | 10 +++++++--- Resources/views/method.html.twig | 5 +++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index 5876601..cd49bdc 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -105,6 +105,11 @@ class ApiDoc */ private $authentication = false; + /** + * @var int + */ + private $cache; + /** * @var array */ @@ -328,6 +333,22 @@ class ApiDoc $this->authentication = $authentication; } + /** + * @return int + */ + public function getCache() + { + return $this->cache; + } + + /** + * @param int $cache + */ + public function setCache($cache) + { + $this->cache = (int) $cache; + } + /** * @return array */ @@ -370,6 +391,10 @@ class ApiDoc $data['section'] = $section; } + if ($cache = $this->cache) { + $data['cache'] = $cache; + } + $data['https'] = $this->https; $data['authentication'] = $this->authentication; diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 8f7631a..50e647d 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -22,14 +22,16 @@ use Nelmio\ApiDocBundle\Util\DocCommentExtractor; class ApiDocExtractor { - const ANNOTATION_CLASS = 'Nelmio\\ApiDocBundle\\Annotation\\ApiDoc'; + const ANNOTATION_CLASS = 'Nelmio\\ApiDocBundle\\Annotation\\ApiDoc'; - const FOS_REST_QUERY_PARAM_CLASS = 'FOS\\RestBundle\\Controller\\Annotations\\QueryParam'; + const FOS_REST_QUERY_PARAM_CLASS = 'FOS\\RestBundle\\Controller\\Annotations\\QueryParam'; - const FOS_REST_REQUEST_PARAM_CLASS = 'FOS\\RestBundle\\Controller\\Annotations\\RequestParam'; + const FOS_REST_REQUEST_PARAM_CLASS = 'FOS\\RestBundle\\Controller\\Annotations\\RequestParam'; const JMS_SECURITY_EXTRA_SECURE_CLASS = 'JMS\\SecurityExtraBundle\\Annotation\\Secure'; + const CACHE_ANNOTATION_CLASS = 'Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Cache'; + /** * @var ContainerInterface */ @@ -364,6 +366,8 @@ class ApiDocExtractor )); } elseif (is_a($annot, self::JMS_SECURITY_EXTRA_SECURE_CLASS)) { $annotation->setAuthentication(true); + } elseif (is_a($annot, self::CACHE_ANNOTATION_CLASS)) { + $annotation->setCache($annot->getMaxAge()); } } } diff --git a/Resources/views/method.html.twig b/Resources/views/method.html.twig index 49272f3..f6fdba5 100644 --- a/Resources/views/method.html.twig +++ b/Resources/views/method.html.twig @@ -165,6 +165,11 @@ {% endif %} + {% if data.cache is defined and data.cache is not empty %} +

Cache

+
{{ data.cache }}s
+ {% endif %} + {% if enableSandbox %} From 772184cfa19c2c87d58c28441ee03a22a7d7e9f3 Mon Sep 17 00:00:00 2001 From: Florent DUBOST Date: Wed, 27 Mar 2013 16:20:42 +0100 Subject: [PATCH 2/3] Add some tests for the cache annotation --- Annotation/ApiDoc.php | 4 ++++ Tests/Annotation/ApiDocTest.php | 12 ++++++++++++ Tests/Extractor/ApiDocExtratorTest.php | 13 +++++++++++++ Tests/Fixtures/Controller/TestController.php | 9 +++++++++ composer.json | 4 +++- 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index cd49bdc..28a8f24 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -152,6 +152,10 @@ class ApiDoc $this->setAuthentication((bool) $data['authentication']); } + if (isset($data['cache'])) { + $this->setCache($data['cache']); + } + if (isset($data['section'])) { $this->section = $data['section']; } diff --git a/Tests/Annotation/ApiDocTest.php b/Tests/Annotation/ApiDocTest.php index f1fece7..6a7750a 100644 --- a/Tests/Annotation/ApiDocTest.php +++ b/Tests/Annotation/ApiDocTest.php @@ -210,4 +210,16 @@ class ApiDocTest extends TestCase $this->assertTrue($array['authentication']); } + + public function testConstructWithCache() + { + $data = array( + 'cache' => '60' + ); + + $annot = new ApiDoc($data); + $array = $annot->toArray(); + + $this->assertEquals($data['cache'], $array['cache']); + } } diff --git a/Tests/Extractor/ApiDocExtratorTest.php b/Tests/Extractor/ApiDocExtratorTest.php index b4217fa..eee99dd 100644 --- a/Tests/Extractor/ApiDocExtratorTest.php +++ b/Tests/Extractor/ApiDocExtratorTest.php @@ -166,4 +166,17 @@ class ApiDocExtractorTest extends WebTestCase $annotation->getAuthentication() ); } + + public function testGetWithCache() + { + $container = $this->getContainer(); + $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + $annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::CachedAction', 'test_route_14'); + + $this->assertNotNull($annotation); + $this->assertEquals( + 60, + $annotation->getCache() + ); + } } diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php index 1c33286..4c42042 100644 --- a/Tests/Fixtures/Controller/TestController.php +++ b/Tests/Fixtures/Controller/TestController.php @@ -15,6 +15,7 @@ use FOS\RestBundle\Controller\Annotations\QueryParam; use FOS\RestBundle\Controller\Annotations\RequestParam; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Component\HttpFoundation\Response; +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; class TestController { @@ -145,4 +146,12 @@ class TestController public function authenticatedAction() { } + + /** + * @ApiDoc() + * @Cache(maxage=60, public=1) + */ + public function cachedAction() + { + } } diff --git a/composer.json b/composer.json index 408ba0c..d04b3d0 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,9 @@ "symfony/validator": "~2.1", "symfony/yaml": "~2.1", "friendsofsymfony/rest-bundle": "dev-master", - "jms/serializer-bundle": ">=0.11" + "jms/serializer-bundle": ">=0.11", + "phpunit/phpunit-mock-objects": "dev-master", + "sensio/framework-extra-bundle": "dev-master" }, "minimum-stability": "dev", "autoload": { From e581432d2007a4f923c0429977ba9071190ed729 Mon Sep 17 00:00:00 2001 From: Florent DUBOST Date: Wed, 27 Mar 2013 16:23:13 +0100 Subject: [PATCH 3/3] Oups, cleaning up --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index d04b3d0..83ed958 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,6 @@ "symfony/yaml": "~2.1", "friendsofsymfony/rest-bundle": "dev-master", "jms/serializer-bundle": ">=0.11", - "phpunit/phpunit-mock-objects": "dev-master", "sensio/framework-extra-bundle": "dev-master" }, "minimum-stability": "dev",