Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 97 additions & 1 deletion src/Mpociot/ApiDoc/Generators/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Mpociot\ApiDoc\Generators;

use Faker\Factory;
use League\Fractal\Manager;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use ReflectionClass;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -74,7 +77,7 @@ public function processRoute($route, $bindings = [], $headers = [], $withRespons
try {
$response = $this->getRouteResponse($route, $bindings, $headers);
} catch (\Exception $e) {
dump("Couldn't get response for route: ".implode(',', $this->getMethods($route)).'] '.$route->uri().'', $e);
dump("Couldn't get response for route: ".implode(',', $this->getMethods($route)).'] '.$route->uri().'', $e->getMessage());
}
}

Expand Down Expand Up @@ -675,4 +678,97 @@ private function getResponseContent($response)
}
return $content;
}

/**
* Get a response from the transformer tags.
*
* @param array $tags
*
* @return mixed
*/
protected function getTransformerResponse($tags)
{
try {
$transFormerTags = array_filter($tags, function ($tag) {
if (! ($tag instanceof Tag)) {
return false;
}

return \in_array(\strtolower($tag->getName()), ['transformer', 'transformercollection']);
});
if (empty($transFormerTags)) {
// we didn't have any of the tags so goodbye
return false;
}

$modelTag = array_first(array_filter($tags, function ($tag) {
if (! ($tag instanceof Tag)) {
return false;
}

return \in_array(\strtolower($tag->getName()), ['transformermodel']);
}));
$tag = \array_first($transFormerTags);
$transformer = $tag->getContent();
if (! \class_exists($transformer)) {
// if we can't find the transformer we can't generate a response
return;
}
$demoData = [];

$reflection = new ReflectionClass($transformer);
$method = $reflection->getMethod('transform');
$parameter = \array_first($method->getParameters());
$type = null;
if ($modelTag) {
$type = $modelTag->getContent();
}
if (version_compare(PHP_VERSION, '7.0.0') >= 0 && \is_null($type)) {
// we can only get the type with reflection for PHP 7
if ($parameter->hasType() &&
! $parameter->getType()->isBuiltin() &&
\class_exists((string) $parameter->getType())) {
//we have a type
$type = (string) $parameter->getType();
}
}
if ($type) {
// we have a class so we try to create an instance
$demoData = new $type;
try {
// try a factory
$demoData = \factory($type)->make();
} catch (\Exception $e) {
if ($demoData instanceof \Illuminate\Database\Eloquent\Model) {
// we can't use a factory but can try to get one from the database
try {
// check if we can find one
$newDemoData = $type::first();
if ($newDemoData) {
$demoData = $newDemoData;
}
} catch (\Exception $e) {
// do nothing
}
}
}
}

$fractal = new Manager();
$resource = [];
if ($tag->getName() == 'transformer') {
// just one
$resource = new Item($demoData, new $transformer);
}
if ($tag->getName() == 'transformercollection') {
// a collection
$resource = new Collection([$demoData, $demoData], new $transformer);
}

return \response($fractal->createData($resource)->toJson());
} catch (\Exception $e) {
// it isn't possible to parse the transformer
return;
}
}
}
98 changes: 0 additions & 98 deletions src/Mpociot/ApiDoc/Generators/LaravelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

namespace Mpociot\ApiDoc\Generators;

use ReflectionClass;
use League\Fractal\Manager;
use Illuminate\Routing\Route;
use League\Fractal\Resource\Item;
use Illuminate\Support\Facades\App;
use Mpociot\Reflection\DocBlock\Tag;
use Illuminate\Support\Facades\Request;
use League\Fractal\Resource\Collection;

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

return $response;
}

/**
* Get a response from the transformer tags.
*
* @param array $tags
*
* @return mixed
*/
protected function getTransformerResponse($tags)
{
try {
$transFormerTags = array_filter($tags, function ($tag) {
if (! ($tag instanceof Tag)) {
return false;
}

return \in_array(\strtolower($tag->getName()), ['transformer', 'transformercollection']);
});
if (empty($transFormerTags)) {
// we didn't have any of the tags so goodbye
return false;
}

$modelTag = array_first(array_filter($tags, function ($tag) {
if (! ($tag instanceof Tag)) {
return false;
}

return \in_array(\strtolower($tag->getName()), ['transformermodel']);
}));
$tag = \array_first($transFormerTags);
$transformer = $tag->getContent();
if (! \class_exists($transformer)) {
// if we can't find the transformer we can't generate a response
return;
}
$demoData = [];

$reflection = new ReflectionClass($transformer);
$method = $reflection->getMethod('transform');
$parameter = \array_first($method->getParameters());
$type = null;
if ($modelTag) {
$type = $modelTag->getContent();
}
if (version_compare(PHP_VERSION, '7.0.0') >= 0 && \is_null($type)) {
// we can only get the type with reflection for PHP 7
if ($parameter->hasType() &&
! $parameter->getType()->isBuiltin() &&
\class_exists((string) $parameter->getType())) {
//we have a type
$type = (string) $parameter->getType();
}
}
if ($type) {
// we have a class so we try to create an instance
$demoData = new $type;
try {
// try a factory
$demoData = \factory($type)->make();
} catch (\Exception $e) {
if ($demoData instanceof \Illuminate\Database\Eloquent\Model) {
// we can't use a factory but can try to get one from the database
try {
// check if we can find one
$newDemoData = $type::first();
if ($newDemoData) {
$demoData = $newDemoData;
}
} catch (\Exception $e) {
// do nothing
}
}
}
}

$fractal = new Manager();
$resource = [];
if ($tag->getName() == 'transformer') {
// just one
$resource = new Item($demoData, new $transformer);
}
if ($tag->getName() == 'transformercollection') {
// a collection
$resource = new Collection([$demoData, $demoData], new $transformer);
}

return \response($fractal->createData($resource)->toJson());
} catch (\Exception $e) {
// it isn't possible to parse the transformer
return;
}
}
}