Skip to content

Commit 083281d

Browse files
authored
Merge pull request #581 from mpociot/plugin-fixes-improvements
Plugin fixes and other improvements
2 parents 96120bb + 18f5c81 commit 083281d

File tree

6 files changed

+65
-10
lines changed

6 files changed

+65
-10
lines changed

docs/plugins.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ Route processing is performed in four stages:
88
- queryParameters
99
- responses
1010

11-
For each stage, the Generator attempts one or more configured strategies to fetch data. The Generator will call of the strategies configured, progressively combining their results together before to produce the final output of that stage.
11+
For each stage, the Generator attempts the specified strategies to fetch data. The Generator will call of the strategies configured, progressively combining their results together before to produce the final output of that stage.
12+
13+
There are a number of strategies inccluded with the package, so you don't have to set up anything to get it working.
14+
15+
> Note: The included ResponseCalls strategy is designed to stop if a response has already been gotten from any other strategy.
1216
1317
## Strategies
1418
To create a strategy, create a class that extends `\Mpociot\ApiDoc\Strategies\Strategy`.

src/Strategies/Responses/ResponseCalls.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public function __invoke(Route $route, \ReflectionClass $controller, \Reflection
4343
$request = $this->prepareRequest($route, $rulesToApply, $bodyParameters, $queryParameters);
4444

4545
try {
46-
$response = [200 => $this->makeApiCall($request)->getContent()];
46+
$response = $this->makeApiCall($request);
47+
$response = [$response->getStatusCode() => $response->getContent()];
4748
} catch (\Exception $e) {
4849
echo 'Exception thrown during response call for ['.implode(',', $route->methods)."] {$route->uri}.\n";
4950
if (Flags::$shouldBeVerbose) {

src/Strategies/Responses/UseTransformerTags.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function getTransformerResponse(array $tags)
5454
return null;
5555
}
5656

57-
$transformer = $this->getTransformerClass($transformerTag);
57+
list($statusCode, $transformer) = $this->getStatusCodeAmdTransformerClass($transformerTag);
5858
$model = $this->getClassToBeTransformed($tags, (new ReflectionClass($transformer))->getMethod('transform'));
5959
$modelInstance = $this->instantiateTransformerModel($model);
6060

@@ -68,7 +68,7 @@ protected function getTransformerResponse(array $tags)
6868
? new Collection([$modelInstance, $modelInstance], new $transformer)
6969
: new Item($modelInstance, new $transformer);
7070

71-
return [200 => response($fractal->createData($resource)->toJson())->getContent()];
71+
return [$statusCode => response($fractal->createData($resource)->toJson())->getContent()];
7272
} catch (\Exception $e) {
7373
return null;
7474
}
@@ -77,11 +77,16 @@ protected function getTransformerResponse(array $tags)
7777
/**
7878
* @param Tag $tag
7979
*
80-
* @return string|null
80+
* @return array
8181
*/
82-
private function getTransformerClass($tag)
82+
private function getStatusCodeAmdTransformerClass($tag): array
8383
{
84-
return $tag->getContent();
84+
$content = $tag->getContent();
85+
preg_match('/^(\d{3})?\s?([\s\S]*)$/', $content, $result);
86+
$status = $result[1] ?: 200;
87+
$transformerClass = $result[2];
88+
89+
return [$status, $transformerClass];
8590
}
8691

8792
/**

src/Tools/Generator.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,26 @@ protected function fetchResponses(ReflectionClass $controller, ReflectionMethod
120120

121121
protected function iterateThroughStrategies(string $stage, array $context, array $arguments)
122122
{
123-
$strategies = $this->config->get("strategies.$stage", []);
123+
$defaultStrategies = [
124+
'metadata' => [
125+
\Mpociot\ApiDoc\Strategies\Metadata\GetFromDocBlocks::class,
126+
],
127+
'bodyParameters' => [
128+
\Mpociot\ApiDoc\Strategies\BodyParameters\GetFromBodyParamTag::class,
129+
],
130+
'queryParameters' => [
131+
\Mpociot\ApiDoc\Strategies\QueryParameters\GetFromQueryParamTag::class,
132+
],
133+
'responses' => [
134+
\Mpociot\ApiDoc\Strategies\Responses\UseResponseTag::class,
135+
\Mpociot\ApiDoc\Strategies\Responses\UseResponseFileTag::class,
136+
\Mpociot\ApiDoc\Strategies\Responses\UseTransformerTags::class,
137+
\Mpociot\ApiDoc\Strategies\Responses\ResponseCalls::class,
138+
],
139+
];
140+
141+
// Use the default strategies for the stage, unless they were explicitly set
142+
$strategies = $this->config->get("strategies.$stage", $defaultStrategies[$stage]);
124143
$context[$stage] = $context[$stage] ?? [];
125144
foreach ($strategies as $strategyClass) {
126145
$strategy = new $strategyClass($stage, $this->config);

tests/Fixtures/TestController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ public function transformerTag()
251251
return '';
252252
}
253253

254+
/**
255+
* @transformer 201 \Mpociot\ApiDoc\Tests\Fixtures\TestTransformer
256+
*/
257+
public function transformerTagWithStatusCode()
258+
{
259+
return '';
260+
}
261+
254262
/**
255263
* @transformer \Mpociot\ApiDoc\Tests\Fixtures\TestTransformer
256264
* @transformermodel \Mpociot\ApiDoc\Tests\Fixtures\TestModel

tests/Unit/GeneratorTestCase.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,26 @@ public function can_parse_transformer_tag_with_model()
435435
$this->assertTrue(is_array($response));
436436
$this->assertEquals(200, $response['status']);
437437
$this->assertSame(
438-
$response['content'],
439-
'{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
438+
'{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}',
439+
$response['content']
440+
);
441+
}
442+
443+
/** @test */
444+
public function can_parse_transformer_tag_with_status_code()
445+
{
446+
$route = $this->createRoute('GET', '/transformerTagWithStatusCode', 'transformerTagWithStatusCode');
447+
$parsed = $this->generator->processRoute($route);
448+
$response = Arr::first($parsed['response']);
449+
450+
$this->assertTrue(is_array($parsed));
451+
$this->assertArrayHasKey('showresponse', $parsed);
452+
$this->assertTrue($parsed['showresponse']);
453+
$this->assertTrue(is_array($response));
454+
$this->assertEquals(201, $response['status']);
455+
$this->assertSame(
456+
'{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}',
457+
$response['content']
440458
);
441459
}
442460

0 commit comments

Comments
 (0)