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