Output.class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 addValue($key, $value){
  56. $this->view['values'][$key] = $value;
  57. }
  58. public function render() {
  59. if(!is_file(__DIR__."/classes/".strtolower($this->_template)."/".$this->_template.'.php') ){
  60. $this->_template = 'DefaultTemplate';
  61. }
  62. include_once __DIR__."/classes/".strtolower($this->_template)."/".$this->_template.'.php';
  63. last_class()->setView($this->view)->render();
  64. }
  65. }