Created
June 28, 2013 09:38
-
-
Save dmitrymomot/5883604 to your computer and use it in GitHub Desktop.
Added function paths();
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Kohana; | |
use \Kohana; | |
class View { | |
protected static $_global_data = array(); | |
protected static $_paths = array('views'); | |
public static function factory($file = NULL, array $data = NULL) | |
{ | |
return new static($file, $data); | |
} | |
public static function paths($paths = NULL) | |
{ | |
if ($paths === NULL) | |
{ | |
return static::$_paths; | |
} | |
if ( ! is_array($paths)) | |
{ | |
$paths = array($paths); | |
} | |
static::$_paths = array_merge($paths, static::$_paths); | |
} | |
protected static function capture($kohana_view_filename, array $kohana_view_data) | |
{ | |
extract($kohana_view_data, EXTR_SKIP); | |
if (static::$_global_data) | |
{ | |
extract(static::$_global_data, EXTR_SKIP | EXTR_REFS); | |
} | |
ob_start(); | |
try | |
{ | |
include $kohana_view_filename; | |
} | |
catch (\Exception $e) | |
{ | |
ob_end_clean(); | |
throw $e; | |
} | |
return ob_get_clean(); | |
} | |
public static function set_global($key, $value = NULL) | |
{ | |
if (is_array($key)) | |
{ | |
foreach ($key as $key2 => $value) | |
{ | |
static::$_global_data[$key2] = $value; | |
} | |
} | |
else | |
{ | |
static::$_global_data[$key] = $value; | |
} | |
} | |
public static function bind_global($key, & $value) | |
{ | |
static::$_global_data[$key] =& $value; | |
} | |
protected $_file; | |
protected $_data = array(); | |
public function __construct($file = NULL, array $data = NULL) | |
{ | |
if ($file !== NULL) | |
{ | |
$this->set_filename($file); | |
} | |
if ($data !== NULL) | |
{ | |
$this->_data = $data + $this->_data; | |
} | |
} | |
public function & __get($key) | |
{ | |
if (array_key_exists($key, $this->_data)) | |
{ | |
return $this->_data[$key]; | |
} | |
elseif (array_key_exists($key, static::$_global_data)) | |
{ | |
return static::$_global_data[$key]; | |
} | |
else | |
{ | |
throw new Kohana\Exception('View variable is not set: :var', | |
array(':var' => $key)); | |
} | |
} | |
public function __set($key, $value) | |
{ | |
$this->set($key, $value); | |
} | |
public function __isset($key) | |
{ | |
return (isset($this->_data[$key]) OR isset(static::$_global_data[$key])); | |
} | |
public function __unset($key) | |
{ | |
unset($this->_data[$key], static::$_global_data[$key]); | |
} | |
public function __toString() | |
{ | |
try | |
{ | |
return $this->render(); | |
} | |
catch (\Exception $e) | |
{ | |
$error_response = Kohana\Exception::_handler($e); | |
return $error_response->body(); | |
} | |
} | |
public function set_filename($file) | |
{ | |
foreach (static::paths() as $path) | |
{ | |
if (($path = Kohana::find_file('views', $file)) !== FALSE) | |
{ | |
$this->_file = $path; | |
return $this; | |
} | |
} | |
throw new Kohana\Exception('The requested view :file could not be found', array( | |
':file' => $file, | |
)); | |
} | |
public function set($key, $value = NULL) | |
{ | |
if (is_array($key)) | |
{ | |
foreach ($key as $name => $value) | |
{ | |
$this->_data[$name] = $value; | |
} | |
} | |
else | |
{ | |
$this->_data[$key] = $value; | |
} | |
return $this; | |
} | |
public function bind($key, & $value) | |
{ | |
$this->_data[$key] =& $value; | |
return $this; | |
} | |
public function render($file = NULL) | |
{ | |
if ($file !== NULL) | |
{ | |
$this->set_filename($file); | |
} | |
if (empty($this->_file)) | |
{ | |
throw new Kohana\Exception('You must set the file to use within your view before rendering'); | |
} | |
return static::capture($this->_file, $this->_data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment