From 56fdd4e64cde220a54306578c8da75461acdf091 Mon Sep 17 00:00:00 2001 From: Florent DUBOST Date: Fri, 11 Oct 2013 15:44:31 +0200 Subject: [PATCH 1/3] Adding possibilty to name resource --- Annotation/ApiDoc.php | 14 +++++++++++--- Extractor/ApiDocExtractor.php | 10 +++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index 744d340..760e174 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -78,7 +78,7 @@ class ApiDoc /** * @var Boolean */ - private $isResource = false; + private $resource = false; /** * @var string @@ -132,7 +132,7 @@ class ApiDoc public function __construct(array $data) { - $this->isResource = isset($data['resource']) && $data['resource']; + $this->resource = !empty($data['resource']) ? $data['resource'] : false; if (isset($data['description'])) { $this->description = $data['description']; @@ -292,7 +292,15 @@ class ApiDoc */ public function isResource() { - return $this->isResource; + return (bool) $this->resource; + } + + /** + * @return mixed + */ + public function getResource() + { + return $this->resource && is_string($this->resource) ? $this->resource : false; } /** diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 02fe52d..3535300 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -108,8 +108,12 @@ class ApiDocExtractor if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) { if ($annotation = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS)) { if ($annotation->isResource()) { - // remove format from routes used for resource grouping - $resources[] = str_replace('.{_format}', '', $route->getPattern()); + if (!($resource = $annotation->getResource())) { + // remove format from routes used for resource grouping + $resources[] = str_replace('.{_format}', '', $route->getPattern()); + } else { + $resources[] = $resource; + } } $array[] = array('annotation' => $this->extractData($annotation, $route, $method)); @@ -123,7 +127,7 @@ class ApiDocExtractor $pattern = $element['annotation']->getRoute()->getPattern(); foreach ($resources as $resource) { - if (0 === strpos($pattern, $resource)) { + if (0 === strpos($pattern, $resource) || $resource === $element['annotation']->getResource()) { $array[$index]['resource'] = $resource; $hasResource = true; From 8f6ac59c977764c00832118135ffc1cc93680991 Mon Sep 17 00:00:00 2001 From: Florent DUBOST Date: Fri, 11 Oct 2013 16:18:02 +0200 Subject: [PATCH 2/3] Adding test for named resource --- Tests/Extractor/ApiDocExtractorTest.php | 9 +++++++-- Tests/Fixtures/Controller/TestController.php | 9 +++++++++ Tests/Fixtures/app/config/routing.yml | 4 ++++ Tests/Formatter/MarkdownFormatterTest.php | 6 ++++++ Tests/Formatter/SimpleFormatterTest.php | 11 +++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Tests/Extractor/ApiDocExtractorTest.php b/Tests/Extractor/ApiDocExtractorTest.php index 1948c36..2dc4f19 100644 --- a/Tests/Extractor/ApiDocExtractorTest.php +++ b/Tests/Extractor/ApiDocExtractorTest.php @@ -15,7 +15,7 @@ use Nelmio\ApiDocBundle\Tests\WebTestCase; class ApiDocExtractorTest extends WebTestCase { - const ROUTES_QUANTITY = 20; + const ROUTES_QUANTITY = 21; public function testAll() { @@ -66,8 +66,13 @@ class ApiDocExtractorTest extends WebTestCase $this->assertFalse(isset($array2['filters'])); $this->assertEquals('Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', $a2->getInput()); - $a3 = $data['13']['annotation']; + $a4 = $data[5]['annotation']; + $this->assertTrue($a4->isResource()); + $this->assertEquals('TestResource', $a4->getResource()); + + $a3 = $data['14']['annotation']; $this->assertTrue($a3->getHttps()); + } public function testGet() diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php index ea86559..c9e1eae 100644 --- a/Tests/Fixtures/Controller/TestController.php +++ b/Tests/Fixtures/Controller/TestController.php @@ -19,6 +19,15 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; class TestController { + /** + * @ApiDoc( + * resource="TestResource" + * ) + */ + public function namedResourceAction() + { + } + /** * @ApiDoc( * resource=true, diff --git a/Tests/Fixtures/app/config/routing.yml b/Tests/Fixtures/app/config/routing.yml index fcbf561..fb71259 100644 --- a/Tests/Fixtures/app/config/routing.yml +++ b/Tests/Fixtures/app/config/routing.yml @@ -121,3 +121,7 @@ test_route_17: test_return_nested_output: pattern: /return-nested-output defaults: { _controller: NelmioApiDocTestBundle:Test:jmsReturnNestedOutput, _format: json } + +test_route_named_resource: + pattern: /named-resource + defaults: { _controller: NelmioApiDocTestBundle:Test:namedResource } diff --git a/Tests/Formatter/MarkdownFormatterTest.php b/Tests/Formatter/MarkdownFormatterTest.php index 65d4571..dde1296 100644 --- a/Tests/Formatter/MarkdownFormatterTest.php +++ b/Tests/Formatter/MarkdownFormatterTest.php @@ -138,6 +138,12 @@ _post test 2_ +## TestResource ## + +### `ANY` /named-resource ### + + + ### `POST` /another-post ### _create another test_ diff --git a/Tests/Formatter/SimpleFormatterTest.php b/Tests/Formatter/SimpleFormatterTest.php index 6eba5a1..cf05031 100644 --- a/Tests/Formatter/SimpleFormatterTest.php +++ b/Tests/Formatter/SimpleFormatterTest.php @@ -844,6 +844,17 @@ With multiple lines.', 'deprecated' => false, ), ), + 'TestResource' => + array( + 0 => + array( + 'method' => 'ANY', + 'uri' => '/named-resource', + 'https' => false, + 'authentication' => false, + 'deprecated' => false, + ), + ), ); $this->assertEquals($expected, $result); From f16aa64cf0416f6f4483e45337bea2e3fefd138a Mon Sep 17 00:00:00 2001 From: Florent DUBOST Date: Fri, 11 Oct 2013 16:40:26 +0200 Subject: [PATCH 3/3] more readable --- Extractor/ApiDocExtractor.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 3535300..83bc59e 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -108,11 +108,11 @@ class ApiDocExtractor if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) { if ($annotation = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS)) { if ($annotation->isResource()) { - if (!($resource = $annotation->getResource())) { + if ($resource = $annotation->getResource()) { + $resources[] = $resource; + } else { // remove format from routes used for resource grouping $resources[] = str_replace('.{_format}', '', $route->getPattern()); - } else { - $resources[] = $resource; } }