HTML.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Class for formatting REST-Response bodies as HTML
  4. *
  5. * @package KO7\REST
  6. *
  7. * @copyright (c) since 2016 Koseven Team
  8. * @license https://koseven.ga/LICENSE
  9. */
  10. class KO7_REST_Format_HTML extends REST_Format {
  11. /**
  12. * Format function
  13. *
  14. * @throws REST_Exception
  15. *
  16. * @return string
  17. */
  18. public function format() : string
  19. {
  20. // Filter the path parts, remove empty ones
  21. $path = array_filter([
  22. $this->_request->directory(),
  23. $this->_request->controller(),
  24. $this->_request->action(),
  25. ]);
  26. $path = strtolower(implode(DIRECTORY_SEPARATOR, $path));
  27. // Try to find view, change view name if not found
  28. // e.g Controller: Welcome, check views/Welcome.php else views/welcome/{index,update,create,delete}.php
  29. if (KO7::find_file('views', dirname($path)))
  30. {
  31. $path = dirname($path);
  32. }
  33. // Try to initialize View
  34. try
  35. {
  36. return View::factory($path, $this->_body)->render();
  37. }
  38. catch (View_Exception $e)
  39. {
  40. throw new REST_Exception($e->getMessage());
  41. }
  42. }
  43. }