view.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php namespace System;
  2. class View {
  3. /**
  4. * The name of the view.
  5. *
  6. * @var string
  7. */
  8. public $view;
  9. /**
  10. * The view data.
  11. *
  12. * @var array
  13. */
  14. public $data = array();
  15. /**
  16. * The content of the view.
  17. *
  18. * @var string
  19. */
  20. public $content = '';
  21. /**
  22. * The name of last rendered view.
  23. *
  24. * @var string
  25. */
  26. public static $last;
  27. /**
  28. * Create a new view instance.
  29. *
  30. * @param string $view
  31. * @param array $data
  32. * @return void
  33. */
  34. public function __construct($view, $data = array())
  35. {
  36. $this->view = $view;
  37. $this->data = $data;
  38. $this->content = $this->load($view);
  39. }
  40. /**
  41. * Create a new view instance.
  42. *
  43. * @param string $view
  44. * @param array $data
  45. * @return View
  46. */
  47. public static function make($view, $data = array())
  48. {
  49. return new self($view, $data);
  50. }
  51. /**
  52. * Load the content of a view.
  53. *
  54. * @param string $view
  55. * @return string
  56. */
  57. private function load($view)
  58. {
  59. if (file_exists($path = APP_PATH.'views/'.$view.EXT))
  60. {
  61. return file_get_contents($path);
  62. }
  63. elseif (file_exists($path = SYS_PATH.'views/'.$view.EXT))
  64. {
  65. return file_get_contents($path);
  66. }
  67. else
  68. {
  69. throw new \Exception("View [$view] doesn't exist.");
  70. }
  71. }
  72. /**
  73. * Add a key / value pair to the view data.
  74. *
  75. * @param string $key
  76. * @param mixed $value
  77. * @return View
  78. */
  79. public function bind($key, $value)
  80. {
  81. $this->data[$key] = $value;
  82. return $this;
  83. }
  84. /**
  85. * Get the parsed content of the view.
  86. *
  87. * @return string
  88. */
  89. public function get()
  90. {
  91. static::$last = $this->view;
  92. // -----------------------------------------------------
  93. // Get the content of all of the sub-views.
  94. // -----------------------------------------------------
  95. foreach ($this->data as &$data)
  96. {
  97. if ($data instanceof View or $data instanceof Response)
  98. {
  99. $data = (string) $data;
  100. }
  101. }
  102. extract($this->data, EXTR_SKIP);
  103. ob_start();
  104. echo eval('?>'.$this->content);
  105. return ob_get_clean();
  106. }
  107. /**
  108. * Magic Method for getting items from the view data.
  109. */
  110. public function __get($key)
  111. {
  112. return $this->data[$key];
  113. }
  114. /**
  115. * Magic Method for setting items in the view data.
  116. */
  117. public function __set($key, $value)
  118. {
  119. $this->bind($key, $value);
  120. }
  121. /**
  122. * Magic Method for determining if an item is in the view data.
  123. */
  124. public function __isset($key)
  125. {
  126. return array_key_exists($key, $this->data);
  127. }
  128. /**
  129. * Magic Method for removing an item from the view data.
  130. */
  131. public function __unset($key)
  132. {
  133. unset($this->data[$key]);
  134. }
  135. /**
  136. * Get the parsed content of the View.
  137. */
  138. public function __toString()
  139. {
  140. return $this->get();
  141. }
  142. }