ResponseFileStrategy.php 1007 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace Mpociot\ApiDoc\Tools\ResponseStrategies;
  3. use Illuminate\Routing\Route;
  4. use Mpociot\Reflection\DocBlock\Tag;
  5. /**
  6. * Get a response from from a file in the docblock ( @responseFile ).
  7. */
  8. class ResponseFileStrategy
  9. {
  10. public function __invoke(Route $route, array $tags, array $routeProps)
  11. {
  12. return $this->getFileResponse($tags);
  13. }
  14. /**
  15. * Get the response from the file if available.
  16. *
  17. * @param array $tags
  18. *
  19. * @return mixed
  20. */
  21. protected function getFileResponse(array $tags)
  22. {
  23. $responseFileTags = array_filter($tags, function ($tag) {
  24. return $tag instanceof Tag && strtolower($tag->getName()) == 'responsefile';
  25. });
  26. if (empty($responseFileTags)) {
  27. return;
  28. }
  29. $responseFileTag = array_first($responseFileTags);
  30. $json = json_decode(file_get_contents(storage_path($responseFileTag->getContent()), true), true);
  31. return response()->json($json);
  32. }
  33. }