123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php namespace System;
- class View {
-
- public $view;
-
- public $data = array();
-
- public static $last;
-
- public function __construct($view, $data = array())
- {
- $this->view = $view;
- $this->data = $data;
- }
-
- public static function make($view, $data = array())
- {
- return new self($view, $data);
- }
-
- public function get()
- {
- static::$last = $this->view;
-
- foreach ($this->data as &$data)
- {
- if ($data instanceof View or $data instanceof Response)
- {
- $data = (string) $data;
- }
- }
-
- extract($this->data, EXTR_SKIP);
- ob_start();
- $path = $this->find();
-
-
-
-
-
- try
- {
- include $path;
- }
- catch (\Exception $e)
- {
- Error::handle($e);
- }
- return ob_get_clean();
- }
-
- private function find()
- {
- if (file_exists($path = APP_PATH.'views/'.$this->view.EXT))
- {
- return $path;
- }
- elseif (file_exists($path = SYS_PATH.'views/'.$this->view.EXT))
- {
- return $path;
- }
- else
- {
- throw new \Exception("View [".$this->view."] doesn't exist.");
- }
- }
-
- public function bind($key, $value)
- {
- $this->data[$key] = $value;
- return $this;
- }
-
- public function __get($key)
- {
- return $this->data[$key];
- }
-
- public function __set($key, $value)
- {
- $this->bind($key, $value);
- }
-
- public function __isset($key)
- {
- return array_key_exists($key, $this->data);
- }
-
- public function __unset($key)
- {
- unset($this->data[$key]);
- }
-
- public function __toString()
- {
- return $this->get();
- }
- }
|