Merge pull request #157 from fdubost/feature-cache

Add cache annotation extractor
This commit is contained in:
William Durand 2013-03-31 11:09:43 -07:00
commit d6491e77bc
7 changed files with 77 additions and 4 deletions

View File

@ -105,6 +105,11 @@ class ApiDoc
*/ */
private $authentication = false; private $authentication = false;
/**
* @var int
*/
private $cache;
/** /**
* @var array * @var array
*/ */
@ -147,6 +152,10 @@ class ApiDoc
$this->setAuthentication((bool) $data['authentication']); $this->setAuthentication((bool) $data['authentication']);
} }
if (isset($data['cache'])) {
$this->setCache($data['cache']);
}
if (isset($data['section'])) { if (isset($data['section'])) {
$this->section = $data['section']; $this->section = $data['section'];
} }
@ -328,6 +337,22 @@ class ApiDoc
$this->authentication = $authentication; $this->authentication = $authentication;
} }
/**
* @return int
*/
public function getCache()
{
return $this->cache;
}
/**
* @param int $cache
*/
public function setCache($cache)
{
$this->cache = (int) $cache;
}
/** /**
* @return array * @return array
*/ */
@ -370,6 +395,10 @@ class ApiDoc
$data['section'] = $section; $data['section'] = $section;
} }
if ($cache = $this->cache) {
$data['cache'] = $cache;
}
$data['https'] = $this->https; $data['https'] = $this->https;
$data['authentication'] = $this->authentication; $data['authentication'] = $this->authentication;

View File

@ -22,14 +22,16 @@ use Nelmio\ApiDocBundle\Util\DocCommentExtractor;
class ApiDocExtractor 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 JMS_SECURITY_EXTRA_SECURE_CLASS = 'JMS\\SecurityExtraBundle\\Annotation\\Secure';
const CACHE_ANNOTATION_CLASS = 'Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Cache';
/** /**
* @var ContainerInterface * @var ContainerInterface
*/ */
@ -382,6 +384,8 @@ class ApiDocExtractor
)); ));
} elseif (is_a($annot, self::JMS_SECURITY_EXTRA_SECURE_CLASS)) { } elseif (is_a($annot, self::JMS_SECURITY_EXTRA_SECURE_CLASS)) {
$annotation->setAuthentication(true); $annotation->setAuthentication(true);
} elseif (is_a($annot, self::CACHE_ANNOTATION_CLASS)) {
$annotation->setCache($annot->getMaxAge());
} }
} }
} }

View File

@ -165,6 +165,11 @@
</table> </table>
{% endif %} {% endif %}
{% if data.cache is defined and data.cache is not empty %}
<h4>Cache</h4>
<div>{{ data.cache }}s</div>
{% endif %}
</div> </div>
{% if enableSandbox %} {% if enableSandbox %}

View File

@ -210,4 +210,16 @@ class ApiDocTest extends TestCase
$this->assertTrue($array['authentication']); $this->assertTrue($array['authentication']);
} }
public function testConstructWithCache()
{
$data = array(
'cache' => '60'
);
$annot = new ApiDoc($data);
$array = $annot->toArray();
$this->assertEquals($data['cache'], $array['cache']);
}
} }

View File

@ -166,4 +166,17 @@ class ApiDocExtractorTest extends WebTestCase
$annotation->getAuthentication() $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()
);
}
} }

View File

@ -15,6 +15,7 @@ use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\RequestParam; use FOS\RestBundle\Controller\Annotations\RequestParam;
use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
class TestController class TestController
{ {
@ -145,4 +146,12 @@ class TestController
public function authenticatedAction() public function authenticatedAction()
{ {
} }
/**
* @ApiDoc()
* @Cache(maxage=60, public=1)
*/
public function cachedAction()
{
}
} }

View File

@ -29,7 +29,8 @@
"symfony/validator": "~2.1", "symfony/validator": "~2.1",
"symfony/yaml": "~2.1", "symfony/yaml": "~2.1",
"friendsofsymfony/rest-bundle": "dev-master", "friendsofsymfony/rest-bundle": "dev-master",
"jms/serializer-bundle": ">=0.11" "jms/serializer-bundle": ">=0.11",
"sensio/framework-extra-bundle": "dev-master"
}, },
"minimum-stability": "dev", "minimum-stability": "dev",
"autoload": { "autoload": {