Removed dep on API
[ninja.git] / system / libraries / View.php
blob654169fdc5386d3ba01633746867c7889afefdb0
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Loads and displays Kohana view files. Can also handle output of some binary
4 * files, such as image, Javascript, and CSS files.
6 * $Id: View.php 3917 2009-01-21 03:06:22Z zombor $
8 * @package Core
9 * @author Kohana Team
10 * @copyright (c) 2007-2008 Kohana Team
11 * @license http://kohanaphp.com/license.html
13 class View {
15 // The view file name and type
16 protected $kohana_filename = FALSE;
17 protected $kohana_filetype = FALSE;
19 // View variable storage
20 protected $kohana_local_data = array();
21 protected static $kohana_global_data = array();
23 /**
24 * Creates a new View using the given parameters.
26 * @param string view name
27 * @param array pre-load data
28 * @param string type of file: html, css, js, etc.
29 * @return object
31 public static function factory($name = NULL, $data = NULL, $type = NULL)
33 return new View($name, $data, $type);
36 /**
37 * Attempts to load a view and pre-load view data.
39 * @throws Kohana_Exception if the requested view cannot be found
40 * @param string view name
41 * @param array pre-load data
42 * @param string type of file: html, css, js, etc.
43 * @return void
45 public function __construct($name = NULL, $data = NULL, $type = NULL)
47 if (is_string($name) AND $name !== '')
49 // Set the filename
50 $this->set_filename($name, $type);
53 if (is_array($data) AND ! empty($data))
55 // Preload data using array_merge, to allow user extensions
56 $this->kohana_local_data = array_merge($this->kohana_local_data, $data);
60 /**
61 * Magic method access to test for view property
63 * @param string View property to test for
64 * @return boolean
66 public function __isset($key = NULL)
68 return $this->is_set($key);
71 /**
72 * Sets the view filename.
74 * @chainable
75 * @param string view filename
76 * @param string view file type
77 * @return object
79 public function set_filename($name, $type = NULL)
81 if ($type == NULL)
83 // Load the filename and set the content type
84 $this->kohana_filename = Kohana::find_file('views', $name, TRUE);
85 $this->kohana_filetype = EXT;
87 else
89 // Check if the filetype is allowed by the configuration
90 if ( ! in_array($type, Kohana::config('view.allowed_filetypes')))
91 throw new Kohana_Exception('core.invalid_filetype', $type);
93 // Load the filename and set the content type
94 $this->kohana_filename = Kohana::find_file('views', $name, TRUE, $type);
95 $this->kohana_filetype = Kohana::config('mimes.'.$type);
97 if ($this->kohana_filetype == NULL)
99 // Use the specified type
100 $this->kohana_filetype = $type;
104 return $this;
108 * Sets a view variable.
110 * @param string|array name of variable or an array of variables
111 * @param mixed value when using a named variable
112 * @return object
114 public function set($name, $value = NULL)
116 if (is_array($name))
118 foreach ($name as $key => $value)
120 $this->__set($key, $value);
123 else
125 $this->__set($name, $value);
128 return $this;
132 * Checks for a property existence in the view locally or globally. Unlike the built in __isset(),
133 * this method can take an array of properties to test simultaneously.
135 * @param string $key property name to test for
136 * @param array $key array of property names to test for
137 * @return boolean property test result
138 * @return array associative array of keys and boolean test result
140 public function is_set( $key = FALSE )
142 // Setup result;
143 $result = FALSE;
145 // If key is an array
146 if (is_array($key))
148 // Set the result to an array
149 $result = array();
151 // Foreach key
152 foreach ($key as $property)
154 // Set the result to an associative array
155 $result[$property] = (array_key_exists($property, $this->kohana_local_data) OR array_key_exists($property, self::$kohana_global_data)) ? TRUE : FALSE;
158 else
160 // Otherwise just check one property
161 $result = (array_key_exists($key, $this->kohana_local_data) OR array_key_exists($key, self::$kohana_global_data)) ? TRUE : FALSE;
164 // Return the result
165 return $result;
169 * Sets a bound variable by reference.
171 * @param string name of variable
172 * @param mixed variable to assign by reference
173 * @return object
175 public function bind($name, & $var)
177 $this->kohana_local_data[$name] =& $var;
179 return $this;
183 * Sets a view global variable.
185 * @param string|array name of variable or an array of variables
186 * @param mixed value when using a named variable
187 * @return object
189 public function set_global($name, $value = NULL)
191 if (is_array($name))
193 foreach ($name as $key => $value)
195 self::$kohana_global_data[$key] = $value;
198 else
200 self::$kohana_global_data[$name] = $value;
203 return $this;
207 * Magically sets a view variable.
209 * @param string variable key
210 * @param string variable value
211 * @return void
213 public function __set($key, $value)
215 $this->kohana_local_data[$key] = $value;
219 * Magically gets a view variable.
221 * @param string variable key
222 * @return mixed variable value if the key is found
223 * @return void if the key is not found
225 public function &__get($key)
227 if (isset($this->kohana_local_data[$key]))
228 return $this->kohana_local_data[$key];
230 if (isset(self::$kohana_global_data[$key]))
231 return self::$kohana_global_data[$key];
233 if (isset($this->$key))
234 return $this->$key;
236 $this->kohana_local_data[$key] = null;
237 return $this->kohana_local_data[$key];
241 * Magically converts view object to string.
243 * @return string
245 public function __toString()
247 return $this->render();
251 * Renders a view.
253 * @param boolean set to TRUE to echo the output instead of returning it
254 * @param callback special renderer to pass the output through
255 * @return string if print is FALSE
256 * @return void if print is TRUE
258 public function render($print = FALSE, $renderer = FALSE)
260 if (empty($this->kohana_filename))
261 throw new Kohana_Exception('core.view_set_filename');
263 if (is_string($this->kohana_filetype))
265 // Merge global and local data, local overrides global with the same name
266 $data = array_merge(self::$kohana_global_data, $this->kohana_local_data);
268 // Load the view in the controller for access to $this
269 $output = Kohana::$instance->_kohana_load_view($this->kohana_filename, $data);
271 if ($renderer !== FALSE AND is_callable($renderer, TRUE))
273 // Pass the output through the user defined renderer
274 $output = call_user_func($renderer, $output);
277 if ($print === TRUE)
279 // Display the output
280 echo $output;
281 return;
284 else
286 // Set the content type and size
287 header('Content-Type: '.$this->kohana_filetype[0]);
289 if ($print === TRUE)
291 if ($file = fopen($this->kohana_filename, 'rb'))
293 // Display the output
294 fpassthru($file);
295 fclose($file);
297 return;
300 // Fetch the file contents
301 $output = file_get_contents($this->kohana_filename);
304 return $output;
306 } // End View