view.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 name of last rendered view.
  17. *
  18. * @var string
  19. */
  20. public static $last;
  21. /**
  22. * Create a new view instance.
  23. *
  24. * @param string $view
  25. * @param array $data
  26. * @return void
  27. */
  28. public function __construct($view, $data = array())
  29. {
  30. $this->view = $view;
  31. $this->data = $data;
  32. // -----------------------------------------------------
  33. // Every view has an error collector. This makes it
  34. // convenient to check for any validation errors without
  35. // worrying if the error collector is instantiated.
  36. //
  37. // If an error collector is in the session, it will
  38. // be used as the error collector for the view.
  39. // -----------------------------------------------------
  40. $this->data['errors'] = (Config::get('session.driver') != '' and Session::has('errors')) ? Session::get('errors') : new Validation\Error_Collector;
  41. }
  42. /**
  43. * Create a new view instance.
  44. *
  45. * @param string $view
  46. * @param array $data
  47. * @return View
  48. */
  49. public static function make($view, $data = array())
  50. {
  51. return new self($view, $data);
  52. }
  53. /**
  54. * Get the parsed content of the view.
  55. *
  56. * @return string
  57. */
  58. public function get()
  59. {
  60. static::$last = $this->view;
  61. // -----------------------------------------------------
  62. // Get the evaluated content of all of the sub-views.
  63. // -----------------------------------------------------
  64. foreach ($this->data as &$data)
  65. {
  66. if ($data instanceof View or $data instanceof Response)
  67. {
  68. $data = (string) $data;
  69. }
  70. }
  71. // -----------------------------------------------------
  72. // Extract the view data into the local scope.
  73. // -----------------------------------------------------
  74. extract($this->data, EXTR_SKIP);
  75. // -----------------------------------------------------
  76. // Start the output buffer so nothing escapes to the
  77. // browser. The response will be sent later.
  78. // -----------------------------------------------------
  79. ob_start();
  80. $path = $this->find();
  81. // -----------------------------------------------------
  82. // We include the view into the local scope within a
  83. // try / catch block to catch any exceptions that may
  84. // occur while the view is rendering.
  85. // -----------------------------------------------------
  86. try
  87. {
  88. include $path;
  89. }
  90. catch (\Exception $e)
  91. {
  92. Error::handle($e);
  93. }
  94. return ob_get_clean();
  95. }
  96. /**
  97. * Get the full path to the view.
  98. *
  99. * Views are cascaded, so the application directory views
  100. * will take precedence over the system directory's views
  101. * of the same name.
  102. *
  103. * @return string
  104. */
  105. private function find()
  106. {
  107. if (file_exists($path = APP_PATH.'views/'.$this->view.EXT))
  108. {
  109. return $path;
  110. }
  111. elseif (file_exists($path = SYS_PATH.'views/'.$this->view.EXT))
  112. {
  113. return $path;
  114. }
  115. else
  116. {
  117. throw new \Exception("View [".$this->view."] doesn't exist.");
  118. }
  119. }
  120. /**
  121. * Add a key / value pair to the view data.
  122. *
  123. * @param string $key
  124. * @param mixed $value
  125. * @return View
  126. */
  127. public function bind($key, $value)
  128. {
  129. $this->data[$key] = $value;
  130. return $this;
  131. }
  132. /**
  133. * Magic Method for getting items from the view data.
  134. */
  135. public function __get($key)
  136. {
  137. return $this->data[$key];
  138. }
  139. /**
  140. * Magic Method for setting items in the view data.
  141. */
  142. public function __set($key, $value)
  143. {
  144. $this->bind($key, $value);
  145. }
  146. /**
  147. * Magic Method for determining if an item is in the view data.
  148. */
  149. public function __isset($key)
  150. {
  151. return array_key_exists($key, $this->data);
  152. }
  153. /**
  154. * Magic Method for removing an item from the view data.
  155. */
  156. public function __unset($key)
  157. {
  158. unset($this->data[$key]);
  159. }
  160. /**
  161. * Get the parsed content of the View.
  162. */
  163. public function __toString()
  164. {
  165. return $this->get();
  166. }
  167. }