Output.class.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. class Output {
  3. private static $_instance;
  4. private $view = false;
  5. protected $values = Array();
  6. private $_template = '';
  7. private $_extension = '';
  8. //SINGLETON==============================================
  9. private function __construct() {
  10. $this->_template = ( defined('TEMPLATE_DEFAULT') ) ? TEMPLATE_DEFAULT : 'DefaultTemplate';
  11. $this->_extension = ( defined('TEMPLATE_FILETYPE') ) ? TEMPLATE_FILETYPE : '.mustache';
  12. }
  13. private static function newObj() {
  14. if (!isset(self::$_instance)) {
  15. self::$_instance = new Output();
  16. }
  17. return self::$_instance;
  18. }
  19. public function getInstance() {
  20. if (!isset(self::$_instance)) {
  21. return self::newObj();
  22. }
  23. return self::$_instance;
  24. }
  25. public function setTemplate($template = 'DefaultTemplate') {
  26. $this->_template = $template;
  27. return $this;
  28. }
  29. public static function renderView($name, $values = null, $location = null) {
  30. if (!$location) {
  31. $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
  32. array_pop($segments);
  33. $location = implode('/', $segments) . '/views/';
  34. }
  35. self::$_instance->setView($name, $values, $location)->render();
  36. }
  37. public function setView($name, $values = Array(), $location = null) {
  38. $folder = '';
  39. if (!$location) {
  40. $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
  41. array_pop($segments);
  42. $location = implode('/', $segments) . '/views/';
  43. }
  44. $folder = $location;
  45. $location .= $name;
  46. if (strpos($this->_extension, $location) == 0) {
  47. $location .= $this->_extension;
  48. }
  49. $this->view = Array();
  50. $this->view['location'] = $location;
  51. $this->view['folder'] = $folder;
  52. $this->view['values'] = $values;
  53. return $this;
  54. }
  55. public function render() {
  56. if(!is_file(__DIR__."/classes/".strtolower($this->_template)."/".$this->_template.'.php') ){
  57. $this->_template = 'DefaultTemplate';
  58. }
  59. include_once __DIR__."/classes/".strtolower($this->_template)."/".$this->_template.'.php';
  60. last_class()->setView($this->view)->render();
  61. }
  62. }