You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB

2 years ago
  1. <?php namespace WpsMcloud\Pages;
  2. use WpsMcloud\Exceptions\ViewNotFoundException;
  3. abstract class Page
  4. {
  5. private static string $viewsDir = 'views';
  6. /**
  7. * @throws ViewNotFoundException
  8. */
  9. public function render(): void
  10. {
  11. $this->renderView();
  12. }
  13. /**
  14. * @throws ViewNotFoundException
  15. */
  16. private function renderView(): void
  17. {
  18. $lowerCaseLastPartThisClassName = strtolower(array_reverse(explode('\\', get_class($this)))[0]);
  19. $filePathWithoutExtension = self::getViewsPath() . DIRECTORY_SEPARATOR .$lowerCaseLastPartThisClassName;
  20. $allowedExtensions = [
  21. 'html',
  22. 'php'
  23. ];
  24. $fileIsFound = false;
  25. while (!$fileIsFound && $currentExtension = current($allowedExtensions)) {
  26. if (file_exists($filePath = $filePathWithoutExtension . '.' . $currentExtension)) {
  27. $fileIsFound = true;
  28. include $filePath;
  29. }
  30. }
  31. if (!$fileIsFound) {
  32. throw new ViewNotFoundException(sprintf(
  33. 'Not found any view file in view dir "%s" for this controller "%s" with these available extension: %s',
  34. self::getViewsPath(),
  35. $lowerCaseLastPartThisClassName,
  36. implode(', ', $allowedExtensions)
  37. ));
  38. }
  39. }
  40. private static function getViewsPath(): string
  41. {
  42. return WPSTUDIO_MEDIA_CLOUD_TRANFORM_BASE_PATH . DIRECTORY_SEPARATOR . self::$viewsDir;
  43. }
  44. }