Skip to content

Commit a581e33

Browse files
committed
Add support for transformer response using DIngo router
1 parent 1ca8ac5 commit a581e33

File tree

2 files changed

+97
-99
lines changed

2 files changed

+97
-99
lines changed

src/Mpociot/ApiDoc/Generators/AbstractGenerator.php

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Mpociot\ApiDoc\Generators;
44

55
use Faker\Factory;
6+
use League\Fractal\Manager;
7+
use League\Fractal\Resource\Collection;
8+
use League\Fractal\Resource\Item;
69
use ReflectionClass;
710
use Illuminate\Support\Arr;
811
use Illuminate\Support\Str;
@@ -74,7 +77,7 @@ public function processRoute($route, $bindings = [], $headers = [], $withRespons
7477
try {
7578
$response = $this->getRouteResponse($route, $bindings, $headers);
7679
} catch (\Exception $e) {
77-
dump("Couldn't get response for route: ".implode(',', $this->getMethods($route)).'] '.$route->uri().'', $e);
80+
dump("Couldn't get response for route: ".implode(',', $this->getMethods($route)).'] '.$route->uri().'', $e->getMessage());
7881
}
7982
}
8083

@@ -675,4 +678,97 @@ private function getResponseContent($response)
675678
}
676679
return $content;
677680
}
681+
682+
/**
683+
* Get a response from the transformer tags.
684+
*
685+
* @param array $tags
686+
*
687+
* @return mixed
688+
*/
689+
protected function getTransformerResponse($tags)
690+
{
691+
try {
692+
$transFormerTags = array_filter($tags, function ($tag) {
693+
if (! ($tag instanceof Tag)) {
694+
return false;
695+
}
696+
697+
return \in_array(\strtolower($tag->getName()), ['transformer', 'transformercollection']);
698+
});
699+
if (empty($transFormerTags)) {
700+
// we didn't have any of the tags so goodbye
701+
return false;
702+
}
703+
704+
$modelTag = array_first(array_filter($tags, function ($tag) {
705+
if (! ($tag instanceof Tag)) {
706+
return false;
707+
}
708+
709+
return \in_array(\strtolower($tag->getName()), ['transformermodel']);
710+
}));
711+
$tag = \array_first($transFormerTags);
712+
$transformer = $tag->getContent();
713+
if (! \class_exists($transformer)) {
714+
// if we can't find the transformer we can't generate a response
715+
return;
716+
}
717+
$demoData = [];
718+
719+
$reflection = new ReflectionClass($transformer);
720+
$method = $reflection->getMethod('transform');
721+
$parameter = \array_first($method->getParameters());
722+
$type = null;
723+
if ($modelTag) {
724+
$type = $modelTag->getContent();
725+
}
726+
if (version_compare(PHP_VERSION, '7.0.0') >= 0 && \is_null($type)) {
727+
// we can only get the type with reflection for PHP 7
728+
if ($parameter->hasType() &&
729+
! $parameter->getType()->isBuiltin() &&
730+
\class_exists((string) $parameter->getType())) {
731+
//we have a type
732+
$type = (string) $parameter->getType();
733+
}
734+
}
735+
if ($type) {
736+
// we have a class so we try to create an instance
737+
$demoData = new $type;
738+
try {
739+
// try a factory
740+
$demoData = \factory($type)->make();
741+
} catch (\Exception $e) {
742+
if ($demoData instanceof \Illuminate\Database\Eloquent\Model) {
743+
// we can't use a factory but can try to get one from the database
744+
try {
745+
// check if we can find one
746+
$newDemoData = $type::first();
747+
if ($newDemoData) {
748+
$demoData = $newDemoData;
749+
}
750+
} catch (\Exception $e) {
751+
// do nothing
752+
}
753+
}
754+
}
755+
}
756+
757+
$fractal = new Manager();
758+
$resource = [];
759+
if ($tag->getName() == 'transformer') {
760+
// just one
761+
$resource = new Item($demoData, new $transformer);
762+
}
763+
if ($tag->getName() == 'transformercollection') {
764+
// a collection
765+
$resource = new Collection([$demoData, $demoData], new $transformer);
766+
}
767+
768+
return \response($fractal->createData($resource)->toJson());
769+
} catch (\Exception $e) {
770+
// it isn't possible to parse the transformer
771+
return;
772+
}
773+
}
678774
}

src/Mpociot/ApiDoc/Generators/LaravelGenerator.php

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
namespace Mpociot\ApiDoc\Generators;
44

5-
use ReflectionClass;
6-
use League\Fractal\Manager;
75
use Illuminate\Routing\Route;
8-
use League\Fractal\Resource\Item;
96
use Illuminate\Support\Facades\App;
10-
use Mpociot\Reflection\DocBlock\Tag;
117
use Illuminate\Support\Facades\Request;
12-
use League\Fractal\Resource\Collection;
138

149
class LaravelGenerator extends AbstractGenerator
1510
{
@@ -97,97 +92,4 @@ public function callRoute($method, $uri, $parameters = [], $cookies = [], $files
9792

9893
return $response;
9994
}
100-
101-
/**
102-
* Get a response from the transformer tags.
103-
*
104-
* @param array $tags
105-
*
106-
* @return mixed
107-
*/
108-
protected function getTransformerResponse($tags)
109-
{
110-
try {
111-
$transFormerTags = array_filter($tags, function ($tag) {
112-
if (! ($tag instanceof Tag)) {
113-
return false;
114-
}
115-
116-
return \in_array(\strtolower($tag->getName()), ['transformer', 'transformercollection']);
117-
});
118-
if (empty($transFormerTags)) {
119-
// we didn't have any of the tags so goodbye
120-
return false;
121-
}
122-
123-
$modelTag = array_first(array_filter($tags, function ($tag) {
124-
if (! ($tag instanceof Tag)) {
125-
return false;
126-
}
127-
128-
return \in_array(\strtolower($tag->getName()), ['transformermodel']);
129-
}));
130-
$tag = \array_first($transFormerTags);
131-
$transformer = $tag->getContent();
132-
if (! \class_exists($transformer)) {
133-
// if we can't find the transformer we can't generate a response
134-
return;
135-
}
136-
$demoData = [];
137-
138-
$reflection = new ReflectionClass($transformer);
139-
$method = $reflection->getMethod('transform');
140-
$parameter = \array_first($method->getParameters());
141-
$type = null;
142-
if ($modelTag) {
143-
$type = $modelTag->getContent();
144-
}
145-
if (version_compare(PHP_VERSION, '7.0.0') >= 0 && \is_null($type)) {
146-
// we can only get the type with reflection for PHP 7
147-
if ($parameter->hasType() &&
148-
! $parameter->getType()->isBuiltin() &&
149-
\class_exists((string) $parameter->getType())) {
150-
//we have a type
151-
$type = (string) $parameter->getType();
152-
}
153-
}
154-
if ($type) {
155-
// we have a class so we try to create an instance
156-
$demoData = new $type;
157-
try {
158-
// try a factory
159-
$demoData = \factory($type)->make();
160-
} catch (\Exception $e) {
161-
if ($demoData instanceof \Illuminate\Database\Eloquent\Model) {
162-
// we can't use a factory but can try to get one from the database
163-
try {
164-
// check if we can find one
165-
$newDemoData = $type::first();
166-
if ($newDemoData) {
167-
$demoData = $newDemoData;
168-
}
169-
} catch (\Exception $e) {
170-
// do nothing
171-
}
172-
}
173-
}
174-
}
175-
176-
$fractal = new Manager();
177-
$resource = [];
178-
if ($tag->getName() == 'transformer') {
179-
// just one
180-
$resource = new Item($demoData, new $transformer);
181-
}
182-
if ($tag->getName() == 'transformercollection') {
183-
// a collection
184-
$resource = new Collection([$demoData, $demoData], new $transformer);
185-
}
186-
187-
return \response($fractal->createData($resource)->toJson());
188-
} catch (\Exception $e) {
189-
// it isn't possible to parse the transformer
190-
return;
191-
}
192-
}
19395
}

0 commit comments

Comments
 (0)