AHttpController.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Tools;
  3. abstract class AHttpController extends AController
  4. {
  5. /**
  6. * @var string $theme
  7. * Theme's directory
  8. * Accessible read-only
  9. **/
  10. private $theme;
  11. /**
  12. * @var \Tools\Output $output
  13. * Output manager
  14. **/
  15. private $output;
  16. public function __construct($context, $params)
  17. {
  18. parent::__construct($context, $params);
  19. $this->output = new \Tools\Output($context);
  20. $this->loadTheme();
  21. }
  22. /**
  23. * Init theme
  24. **/
  25. private function loadTheme()
  26. {
  27. $themeName = \Entity\Config::getConfig(null, "theme", "default");
  28. $themeDir = $this->context->router->themesPath.$themeName.'/';
  29. if (!is_dir($themeDir))
  30. throw new \Exception\Error404();
  31. $this->theme = $themeDir;
  32. }
  33. /**
  34. * Run the controller. Entry point
  35. **/
  36. public function start()
  37. {
  38. //TODO trigger hook GET, POST
  39. //TODO call some hooks
  40. $this->run();
  41. //TODO calls some hooks
  42. }
  43. /**
  44. * Load and render a file
  45. * @param string $viewFile template view to load (from theme)
  46. * @return boolean true on success
  47. **/
  48. protected function render($viewFile)
  49. {
  50. $themedir = opendir($this->theme);
  51. $exists = false;
  52. while ($dir = readdir($themedir))
  53. {
  54. if ($dir !== $viewFile || is_dir($this->theme.'/'.$dir))
  55. continue;
  56. $exists = $dir;
  57. }
  58. closedir($themedir);
  59. if ($exists === false)
  60. return false;
  61. $this->output->renderFile($this->theme . $exists);
  62. return true;
  63. }
  64. public abstract function run();
  65. public function __get($key)
  66. {
  67. if ($key === "theme")
  68. return $this->theme;
  69. throw new \Exception("Cannot access attribute {$key}");
  70. }
  71. }