@@ -7,7 +7,7 @@ |
||
| 7 | 7 | * @link https://github.com/ingenerator/koharness |
| 8 | 8 | */ |
| 9 | 9 | return array( |
| 10 | - 'modules' => array( |
|
| 10 | + 'modules' => array( |
|
| 11 | 11 | 'kohana-view' => __DIR__, |
| 12 | - ) |
|
| 12 | + ) |
|
| 13 | 13 | ); |
@@ -39,85 +39,85 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | class Kohana_View_Stream_Wrapper |
| 41 | 41 | { |
| 42 | - /** |
|
| 43 | - * Current stream position. |
|
| 44 | - * |
|
| 45 | - * @var int |
|
| 46 | - */ |
|
| 47 | - protected $_pos = 0; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Data for streaming. |
|
| 51 | - * |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - protected $_data; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Stream stats. |
|
| 58 | - * |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - protected $_stat; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Raw output character. Prepend this on any echo variables to |
|
| 65 | - * turn off auto encoding of the output |
|
| 66 | - */ |
|
| 67 | - protected $_raw_output_char = '!'; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The encoding method to use on view output. Only use the method name |
|
| 71 | - */ |
|
| 72 | - protected $_encode_method = 'HTML::chars'; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Opens the script file and converts markup. |
|
| 76 | - */ |
|
| 77 | - public function stream_open($path, $mode, $options, &$opened_path) |
|
| 78 | - { |
|
| 79 | - // get the view script source |
|
| 80 | - $path = str_replace('kohana.view://', '', $path); |
|
| 81 | - $this->_data = file_get_contents($path); |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * If reading the file failed, update our local stat store |
|
| 85 | - * to reflect the real stat of the file, then return on failure |
|
| 86 | - */ |
|
| 87 | - if ($this->_data === false) |
|
| 88 | - { |
|
| 89 | - $this->_stat = stat($path); |
|
| 90 | - return false; |
|
| 91 | - } |
|
| 42 | + /** |
|
| 43 | + * Current stream position. |
|
| 44 | + * |
|
| 45 | + * @var int |
|
| 46 | + */ |
|
| 47 | + protected $_pos = 0; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Data for streaming. |
|
| 51 | + * |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + protected $_data; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Stream stats. |
|
| 58 | + * |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + protected $_stat; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Raw output character. Prepend this on any echo variables to |
|
| 65 | + * turn off auto encoding of the output |
|
| 66 | + */ |
|
| 67 | + protected $_raw_output_char = '!'; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The encoding method to use on view output. Only use the method name |
|
| 71 | + */ |
|
| 72 | + protected $_encode_method = 'HTML::chars'; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Opens the script file and converts markup. |
|
| 76 | + */ |
|
| 77 | + public function stream_open($path, $mode, $options, &$opened_path) |
|
| 78 | + { |
|
| 79 | + // get the view script source |
|
| 80 | + $path = str_replace('kohana.view://', '', $path); |
|
| 81 | + $this->_data = file_get_contents($path); |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * If reading the file failed, update our local stat store |
|
| 85 | + * to reflect the real stat of the file, then return on failure |
|
| 86 | + */ |
|
| 87 | + if ($this->_data === false) |
|
| 88 | + { |
|
| 89 | + $this->_stat = stat($path); |
|
| 90 | + return false; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Escape all variables and convert <?= ?> to long-form <?php echo ?> |
|
| 95 | - * |
|
| 96 | - */ |
|
| 97 | - $regex = '/<\?(\=|php)?(.+?)\?>/s'; |
|
| 98 | - $this->_data = preg_replace_callback($regex, array($this, '_escape_val'), $this->_data); |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * file_get_contents() won't update PHP's stat cache, so we grab a stat |
|
| 102 | - * of the file to prevent additional reads should the script be |
|
| 103 | - * requested again, which will make include() happy. |
|
| 104 | - */ |
|
| 105 | - $this->_stat = stat($path); |
|
| 106 | - |
|
| 107 | - // PHP7.4 requires that we return the accurate value for the filesize, otherwise it does not read the full string |
|
| 93 | + /** |
|
| 94 | + * Escape all variables and convert <?= ?> to long-form <?php echo ?> |
|
| 95 | + * |
|
| 96 | + */ |
|
| 97 | + $regex = '/<\?(\=|php)?(.+?)\?>/s'; |
|
| 98 | + $this->_data = preg_replace_callback($regex, array($this, '_escape_val'), $this->_data); |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * file_get_contents() won't update PHP's stat cache, so we grab a stat |
|
| 102 | + * of the file to prevent additional reads should the script be |
|
| 103 | + * requested again, which will make include() happy. |
|
| 104 | + */ |
|
| 105 | + $this->_stat = stat($path); |
|
| 106 | + |
|
| 107 | + // PHP7.4 requires that we return the accurate value for the filesize, otherwise it does not read the full string |
|
| 108 | 108 | // and then causes a cryptic "unexpected end of file" error |
| 109 | 109 | $this->_stat['size'] = strlen($this->_data); |
| 110 | - return true; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Escapes a variable from template matching |
|
| 115 | - * |
|
| 116 | - * @param array matches |
|
| 117 | - * @return string |
|
| 118 | - */ |
|
| 119 | - protected function _escape_val($matches) |
|
| 120 | - { |
|
| 110 | + return true; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Escapes a variable from template matching |
|
| 115 | + * |
|
| 116 | + * @param array matches |
|
| 117 | + * @return string |
|
| 118 | + */ |
|
| 119 | + protected function _escape_val($matches) |
|
| 120 | + { |
|
| 121 | 121 | // Start by trimming the echoed string |
| 122 | 122 | $code = trim($matches[2]); |
| 123 | 123 | |
@@ -143,101 +143,101 @@ discard block |
||
| 143 | 143 | // Remove the "turn off escape" character |
| 144 | 144 | return '<?php echo '.substr($var, strlen($this->_raw_output_char), strlen($var)-1).'; ?>'; |
| 145 | 145 | } |
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Included so that __FILE__ returns the appropriate info |
|
| 150 | - * |
|
| 151 | - * @return array |
|
| 152 | - */ |
|
| 153 | - public function url_stat() |
|
| 154 | - { |
|
| 155 | - return $this->_stat; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Reads from the stream. |
|
| 160 | - */ |
|
| 161 | - public function stream_read($count) |
|
| 162 | - { |
|
| 163 | - $ret = substr($this->_data, $this->_pos, $count); |
|
| 164 | - $this->_pos += strlen($ret); |
|
| 165 | - return $ret; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Tells the current position in the stream. |
|
| 170 | - */ |
|
| 171 | - public function stream_tell() |
|
| 172 | - { |
|
| 173 | - return $this->_pos; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Tells if we are at the end of the stream. |
|
| 178 | - */ |
|
| 179 | - public function stream_eof() |
|
| 180 | - { |
|
| 181 | - return $this->_pos >= strlen($this->_data); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Stream statistics. |
|
| 186 | - */ |
|
| 187 | - public function stream_stat() |
|
| 188 | - { |
|
| 189 | - return $this->_stat; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Seek to a specific point in the stream. |
|
| 194 | - */ |
|
| 195 | - public function stream_seek($offset, $whence) |
|
| 196 | - { |
|
| 197 | - switch ($whence) |
|
| 198 | - { |
|
| 199 | - case SEEK_SET: |
|
| 200 | - if ($offset < strlen($this->_data) && $offset >= 0) |
|
| 201 | - { |
|
| 202 | - $this->_pos = $offset; |
|
| 203 | - return true; |
|
| 204 | - } |
|
| 205 | - else |
|
| 206 | - { |
|
| 207 | - return false; |
|
| 208 | - } |
|
| 209 | - break; |
|
| 210 | - |
|
| 211 | - case SEEK_CUR: |
|
| 212 | - if ($offset >= 0) |
|
| 213 | - { |
|
| 214 | - $this->_pos += $offset; |
|
| 215 | - return true; |
|
| 216 | - } |
|
| 217 | - else |
|
| 218 | - { |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - break; |
|
| 222 | - |
|
| 223 | - case SEEK_END: |
|
| 224 | - if (strlen($this->_data) + $offset >= 0) |
|
| 225 | - { |
|
| 226 | - $this->_pos = strlen($this->_data) + $offset; |
|
| 227 | - return true; |
|
| 228 | - } |
|
| 229 | - else |
|
| 230 | - { |
|
| 231 | - return false; |
|
| 232 | - } |
|
| 233 | - break; |
|
| 234 | - |
|
| 235 | - default: |
|
| 236 | - return false; |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - public function stream_set_option($option, $arg1, $arg2) |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Included so that __FILE__ returns the appropriate info |
|
| 150 | + * |
|
| 151 | + * @return array |
|
| 152 | + */ |
|
| 153 | + public function url_stat() |
|
| 154 | + { |
|
| 155 | + return $this->_stat; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Reads from the stream. |
|
| 160 | + */ |
|
| 161 | + public function stream_read($count) |
|
| 162 | + { |
|
| 163 | + $ret = substr($this->_data, $this->_pos, $count); |
|
| 164 | + $this->_pos += strlen($ret); |
|
| 165 | + return $ret; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Tells the current position in the stream. |
|
| 170 | + */ |
|
| 171 | + public function stream_tell() |
|
| 172 | + { |
|
| 173 | + return $this->_pos; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Tells if we are at the end of the stream. |
|
| 178 | + */ |
|
| 179 | + public function stream_eof() |
|
| 180 | + { |
|
| 181 | + return $this->_pos >= strlen($this->_data); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Stream statistics. |
|
| 186 | + */ |
|
| 187 | + public function stream_stat() |
|
| 188 | + { |
|
| 189 | + return $this->_stat; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Seek to a specific point in the stream. |
|
| 194 | + */ |
|
| 195 | + public function stream_seek($offset, $whence) |
|
| 196 | + { |
|
| 197 | + switch ($whence) |
|
| 198 | + { |
|
| 199 | + case SEEK_SET: |
|
| 200 | + if ($offset < strlen($this->_data) && $offset >= 0) |
|
| 201 | + { |
|
| 202 | + $this->_pos = $offset; |
|
| 203 | + return true; |
|
| 204 | + } |
|
| 205 | + else |
|
| 206 | + { |
|
| 207 | + return false; |
|
| 208 | + } |
|
| 209 | + break; |
|
| 210 | + |
|
| 211 | + case SEEK_CUR: |
|
| 212 | + if ($offset >= 0) |
|
| 213 | + { |
|
| 214 | + $this->_pos += $offset; |
|
| 215 | + return true; |
|
| 216 | + } |
|
| 217 | + else |
|
| 218 | + { |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + break; |
|
| 222 | + |
|
| 223 | + case SEEK_END: |
|
| 224 | + if (strlen($this->_data) + $offset >= 0) |
|
| 225 | + { |
|
| 226 | + $this->_pos = strlen($this->_data) + $offset; |
|
| 227 | + return true; |
|
| 228 | + } |
|
| 229 | + else |
|
| 230 | + { |
|
| 231 | + return false; |
|
| 232 | + } |
|
| 233 | + break; |
|
| 234 | + |
|
| 235 | + default: |
|
| 236 | + return false; |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + public function stream_set_option($option, $arg1, $arg2) |
|
| 241 | 241 | { |
| 242 | 242 | // PHP7.4 requires this be implemented, but we can return FALSE that we don't support any of the options |
| 243 | 243 | // (blocking mode, buffering, etc) |
@@ -12,272 +12,272 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Kohana_View_Model { |
| 14 | 14 | |
| 15 | - // View filename |
|
| 16 | - protected $_file; |
|
| 15 | + // View filename |
|
| 16 | + protected $_file; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Returns a new raw View object. If you do not define the "file" parameter, |
|
| 20 | - * you must call [View::set_filename]. |
|
| 21 | - * |
|
| 22 | - * $view = View::factory($file); |
|
| 23 | - * |
|
| 24 | - * @param string view filename |
|
| 25 | - * @param array array of values |
|
| 26 | - * @return View |
|
| 27 | - */ |
|
| 28 | - public static function factory($file = NULL, array $data = NULL) |
|
| 29 | - { |
|
| 30 | - // Return a raw view object if no template is specified. |
|
| 31 | - if ($file === FALSE) |
|
| 32 | - return new View(FALSE, $data); |
|
| 18 | + /** |
|
| 19 | + * Returns a new raw View object. If you do not define the "file" parameter, |
|
| 20 | + * you must call [View::set_filename]. |
|
| 21 | + * |
|
| 22 | + * $view = View::factory($file); |
|
| 23 | + * |
|
| 24 | + * @param string view filename |
|
| 25 | + * @param array array of values |
|
| 26 | + * @return View |
|
| 27 | + */ |
|
| 28 | + public static function factory($file = NULL, array $data = NULL) |
|
| 29 | + { |
|
| 30 | + // Return a raw view object if no template is specified. |
|
| 31 | + if ($file === FALSE) |
|
| 32 | + return new View(FALSE, $data); |
|
| 33 | 33 | |
| 34 | - $class = 'View_'.strtr($file, '/', '_'); |
|
| 34 | + $class = 'View_'.strtr($file, '/', '_'); |
|
| 35 | 35 | |
| 36 | - if ( ! class_exists($class)) |
|
| 37 | - { |
|
| 38 | - $class = 'View'; |
|
| 39 | - } |
|
| 36 | + if ( ! class_exists($class)) |
|
| 37 | + { |
|
| 38 | + $class = 'View'; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - return new $class($file, $data); |
|
| 42 | - } |
|
| 41 | + return new $class($file, $data); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Captures the output that is generated when a view is included. |
|
| 46 | - * The view data will be extracted to make local variables. |
|
| 47 | - * |
|
| 48 | - * $output = $this->capture($file); |
|
| 49 | - * |
|
| 50 | - * @param string filename |
|
| 51 | - * @return string |
|
| 52 | - */ |
|
| 53 | - protected function capture($kohana_view_filename) |
|
| 54 | - { |
|
| 55 | - if ( ! in_array('kohana.view', stream_get_wrappers())) |
|
| 56 | - { |
|
| 57 | - stream_wrapper_register('kohana.view', 'View_Stream_Wrapper'); |
|
| 58 | - } |
|
| 44 | + /** |
|
| 45 | + * Captures the output that is generated when a view is included. |
|
| 46 | + * The view data will be extracted to make local variables. |
|
| 47 | + * |
|
| 48 | + * $output = $this->capture($file); |
|
| 49 | + * |
|
| 50 | + * @param string filename |
|
| 51 | + * @return string |
|
| 52 | + */ |
|
| 53 | + protected function capture($kohana_view_filename) |
|
| 54 | + { |
|
| 55 | + if ( ! in_array('kohana.view', stream_get_wrappers())) |
|
| 56 | + { |
|
| 57 | + stream_wrapper_register('kohana.view', 'View_Stream_Wrapper'); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - // Capture the view output |
|
| 61 | - ob_start(); |
|
| 60 | + // Capture the view output |
|
| 61 | + ob_start(); |
|
| 62 | 62 | |
| 63 | - try |
|
| 64 | - { |
|
| 65 | - include 'kohana.view://'.$kohana_view_filename; |
|
| 66 | - } |
|
| 67 | - catch (Exception $e) |
|
| 68 | - { |
|
| 69 | - // Delete the output buffer |
|
| 70 | - ob_end_clean(); |
|
| 63 | + try |
|
| 64 | + { |
|
| 65 | + include 'kohana.view://'.$kohana_view_filename; |
|
| 66 | + } |
|
| 67 | + catch (Exception $e) |
|
| 68 | + { |
|
| 69 | + // Delete the output buffer |
|
| 70 | + ob_end_clean(); |
|
| 71 | 71 | |
| 72 | - // Re-throw the exception |
|
| 73 | - throw $e; |
|
| 74 | - } |
|
| 72 | + // Re-throw the exception |
|
| 73 | + throw $e; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - // Get the captured output and close the buffer |
|
| 77 | - return ob_get_clean(); |
|
| 78 | - } |
|
| 76 | + // Get the captured output and close the buffer |
|
| 77 | + return ob_get_clean(); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Sets the initial view filename and local data. Views should almost |
|
| 82 | - * always only be created using [View::factory]. |
|
| 83 | - * |
|
| 84 | - * $view = new View($file); |
|
| 85 | - * |
|
| 86 | - * @param string view filename |
|
| 87 | - * @param array array of values |
|
| 88 | - * @return void |
|
| 89 | - * @uses View::set_filename |
|
| 90 | - */ |
|
| 91 | - public function __construct($file = NULL, array $data = NULL) |
|
| 92 | - { |
|
| 93 | - if ($file === NULL) |
|
| 94 | - { |
|
| 95 | - $foo = explode('_', get_class($this)); |
|
| 96 | - array_shift($foo); |
|
| 97 | - $file = strtolower(implode('/', $foo)); |
|
| 98 | - } |
|
| 80 | + /** |
|
| 81 | + * Sets the initial view filename and local data. Views should almost |
|
| 82 | + * always only be created using [View::factory]. |
|
| 83 | + * |
|
| 84 | + * $view = new View($file); |
|
| 85 | + * |
|
| 86 | + * @param string view filename |
|
| 87 | + * @param array array of values |
|
| 88 | + * @return void |
|
| 89 | + * @uses View::set_filename |
|
| 90 | + */ |
|
| 91 | + public function __construct($file = NULL, array $data = NULL) |
|
| 92 | + { |
|
| 93 | + if ($file === NULL) |
|
| 94 | + { |
|
| 95 | + $foo = explode('_', get_class($this)); |
|
| 96 | + array_shift($foo); |
|
| 97 | + $file = strtolower(implode('/', $foo)); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - if ($file !== FALSE) |
|
| 101 | - { |
|
| 102 | - $this->set_filename($file); |
|
| 103 | - } |
|
| 100 | + if ($file !== FALSE) |
|
| 101 | + { |
|
| 102 | + $this->set_filename($file); |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - if ($data !== NULL) |
|
| 106 | - { |
|
| 107 | - // Add the values to the current data |
|
| 108 | - $this->set($data); |
|
| 109 | - } |
|
| 110 | - } |
|
| 105 | + if ($data !== NULL) |
|
| 106 | + { |
|
| 107 | + // Add the values to the current data |
|
| 108 | + $this->set($data); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Magic method, searches for the given variable and returns its value. |
|
| 114 | - * Local variables will be returned before global variables. |
|
| 115 | - * |
|
| 116 | - * $value = $view->foo; |
|
| 117 | - * |
|
| 118 | - * [!!] If the variable has not yet been set, an exception will be thrown. |
|
| 119 | - * |
|
| 120 | - * @param string variable name |
|
| 121 | - * @return mixed |
|
| 122 | - * @throws Kohana_Exception |
|
| 123 | - */ |
|
| 124 | - public function __get($key) |
|
| 125 | - { |
|
| 126 | - if (method_exists($this, $key)) |
|
| 127 | - { |
|
| 128 | - return $this->$key(); |
|
| 129 | - } |
|
| 130 | - elseif (property_exists($this, $key)) |
|
| 131 | - { |
|
| 132 | - return $this->$key; |
|
| 133 | - } |
|
| 134 | - else |
|
| 135 | - { |
|
| 136 | - throw new Kohana_Exception('View variable is not set: :var', |
|
| 137 | - array(':var' => $key)); |
|
| 138 | - } |
|
| 139 | - } |
|
| 112 | + /** |
|
| 113 | + * Magic method, searches for the given variable and returns its value. |
|
| 114 | + * Local variables will be returned before global variables. |
|
| 115 | + * |
|
| 116 | + * $value = $view->foo; |
|
| 117 | + * |
|
| 118 | + * [!!] If the variable has not yet been set, an exception will be thrown. |
|
| 119 | + * |
|
| 120 | + * @param string variable name |
|
| 121 | + * @return mixed |
|
| 122 | + * @throws Kohana_Exception |
|
| 123 | + */ |
|
| 124 | + public function __get($key) |
|
| 125 | + { |
|
| 126 | + if (method_exists($this, $key)) |
|
| 127 | + { |
|
| 128 | + return $this->$key(); |
|
| 129 | + } |
|
| 130 | + elseif (property_exists($this, $key)) |
|
| 131 | + { |
|
| 132 | + return $this->$key; |
|
| 133 | + } |
|
| 134 | + else |
|
| 135 | + { |
|
| 136 | + throw new Kohana_Exception('View variable is not set: :var', |
|
| 137 | + array(':var' => $key)); |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * Magic method, calls [View::set] with the same parameters. |
|
| 143 | - * |
|
| 144 | - * $view->foo = 'something'; |
|
| 145 | - * |
|
| 146 | - * @param string variable name |
|
| 147 | - * @param mixed value |
|
| 148 | - * @return void |
|
| 149 | - */ |
|
| 150 | - public function __set($key, $value) |
|
| 151 | - { |
|
| 152 | - $this->set($key, $value); |
|
| 153 | - } |
|
| 141 | + /** |
|
| 142 | + * Magic method, calls [View::set] with the same parameters. |
|
| 143 | + * |
|
| 144 | + * $view->foo = 'something'; |
|
| 145 | + * |
|
| 146 | + * @param string variable name |
|
| 147 | + * @param mixed value |
|
| 148 | + * @return void |
|
| 149 | + */ |
|
| 150 | + public function __set($key, $value) |
|
| 151 | + { |
|
| 152 | + $this->set($key, $value); |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * Magic method, returns the output of [View::render]. |
|
| 158 | - * |
|
| 159 | - * @return string |
|
| 160 | - * @uses View::render |
|
| 161 | - */ |
|
| 162 | - public function __toString() |
|
| 163 | - { |
|
| 164 | - try |
|
| 165 | - { |
|
| 166 | - return $this->render(); |
|
| 167 | - } |
|
| 168 | - catch (Exception $e) |
|
| 169 | - { |
|
| 170 | - // Display the exception message |
|
| 171 | - Kohana_Exception::handler($e); |
|
| 156 | + /** |
|
| 157 | + * Magic method, returns the output of [View::render]. |
|
| 158 | + * |
|
| 159 | + * @return string |
|
| 160 | + * @uses View::render |
|
| 161 | + */ |
|
| 162 | + public function __toString() |
|
| 163 | + { |
|
| 164 | + try |
|
| 165 | + { |
|
| 166 | + return $this->render(); |
|
| 167 | + } |
|
| 168 | + catch (Exception $e) |
|
| 169 | + { |
|
| 170 | + // Display the exception message |
|
| 171 | + Kohana_Exception::handler($e); |
|
| 172 | 172 | |
| 173 | - return ''; |
|
| 174 | - } |
|
| 175 | - } |
|
| 173 | + return ''; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Sets the view filename. |
|
| 179 | - * |
|
| 180 | - * $view->set_filename($file); |
|
| 181 | - * |
|
| 182 | - * @param string view filename |
|
| 183 | - * @return View |
|
| 184 | - * @throws Kohana_View_Exception |
|
| 185 | - */ |
|
| 186 | - public function set_filename($file) |
|
| 187 | - { |
|
| 188 | - if (($path = Kohana::find_file('views', $file)) === FALSE) |
|
| 189 | - { |
|
| 190 | - throw new Kohana_View_Exception('The requested view :file could not be found', array( |
|
| 191 | - ':file' => $file, |
|
| 192 | - )); |
|
| 193 | - } |
|
| 177 | + /** |
|
| 178 | + * Sets the view filename. |
|
| 179 | + * |
|
| 180 | + * $view->set_filename($file); |
|
| 181 | + * |
|
| 182 | + * @param string view filename |
|
| 183 | + * @return View |
|
| 184 | + * @throws Kohana_View_Exception |
|
| 185 | + */ |
|
| 186 | + public function set_filename($file) |
|
| 187 | + { |
|
| 188 | + if (($path = Kohana::find_file('views', $file)) === FALSE) |
|
| 189 | + { |
|
| 190 | + throw new Kohana_View_Exception('The requested view :file could not be found', array( |
|
| 191 | + ':file' => $file, |
|
| 192 | + )); |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - // Store the file path locally |
|
| 196 | - $this->_file = $path; |
|
| 195 | + // Store the file path locally |
|
| 196 | + $this->_file = $path; |
|
| 197 | 197 | |
| 198 | - return $this; |
|
| 199 | - } |
|
| 198 | + return $this; |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | - /** |
|
| 202 | - * Assigns a variable by name. Assigned values will be available as a |
|
| 203 | - * variable within the view file: |
|
| 204 | - * |
|
| 205 | - * // This value can be accessed as $foo within the view |
|
| 206 | - * $view->set('foo', 'my value'); |
|
| 207 | - * |
|
| 208 | - * You can also use an array to set several values at once: |
|
| 209 | - * |
|
| 210 | - * // Create the values $food and $beverage in the view |
|
| 211 | - * $view->set(array('food' => 'bread', 'beverage' => 'water')); |
|
| 212 | - * |
|
| 213 | - * @param string variable name or an array of variables |
|
| 214 | - * @param mixed value |
|
| 215 | - * @return $this |
|
| 216 | - */ |
|
| 217 | - public function set($key, $value = NULL) |
|
| 218 | - { |
|
| 219 | - if (is_array($key)) |
|
| 220 | - { |
|
| 221 | - foreach ($key as $name => $value) |
|
| 222 | - { |
|
| 223 | - $this->{$name} = $value; |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - else |
|
| 227 | - { |
|
| 228 | - $this->{$key} = $value; |
|
| 229 | - } |
|
| 201 | + /** |
|
| 202 | + * Assigns a variable by name. Assigned values will be available as a |
|
| 203 | + * variable within the view file: |
|
| 204 | + * |
|
| 205 | + * // This value can be accessed as $foo within the view |
|
| 206 | + * $view->set('foo', 'my value'); |
|
| 207 | + * |
|
| 208 | + * You can also use an array to set several values at once: |
|
| 209 | + * |
|
| 210 | + * // Create the values $food and $beverage in the view |
|
| 211 | + * $view->set(array('food' => 'bread', 'beverage' => 'water')); |
|
| 212 | + * |
|
| 213 | + * @param string variable name or an array of variables |
|
| 214 | + * @param mixed value |
|
| 215 | + * @return $this |
|
| 216 | + */ |
|
| 217 | + public function set($key, $value = NULL) |
|
| 218 | + { |
|
| 219 | + if (is_array($key)) |
|
| 220 | + { |
|
| 221 | + foreach ($key as $name => $value) |
|
| 222 | + { |
|
| 223 | + $this->{$name} = $value; |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + else |
|
| 227 | + { |
|
| 228 | + $this->{$key} = $value; |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - return $this; |
|
| 232 | - } |
|
| 231 | + return $this; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - /** |
|
| 235 | - * Assigns a value by reference. The benefit of binding is that values can |
|
| 236 | - * be altered without re-setting them. It is also possible to bind variables |
|
| 237 | - * before they have values. Assigned values will be available as a |
|
| 238 | - * variable within the view file: |
|
| 239 | - * |
|
| 240 | - * // This reference can be accessed as $ref within the view |
|
| 241 | - * $view->bind('ref', $bar); |
|
| 242 | - * |
|
| 243 | - * @param string variable name |
|
| 244 | - * @param mixed referenced variable |
|
| 245 | - * @return $this |
|
| 246 | - */ |
|
| 247 | - public function bind($key, & $value) |
|
| 248 | - { |
|
| 249 | - $this->{$key} =& $value; |
|
| 234 | + /** |
|
| 235 | + * Assigns a value by reference. The benefit of binding is that values can |
|
| 236 | + * be altered without re-setting them. It is also possible to bind variables |
|
| 237 | + * before they have values. Assigned values will be available as a |
|
| 238 | + * variable within the view file: |
|
| 239 | + * |
|
| 240 | + * // This reference can be accessed as $ref within the view |
|
| 241 | + * $view->bind('ref', $bar); |
|
| 242 | + * |
|
| 243 | + * @param string variable name |
|
| 244 | + * @param mixed referenced variable |
|
| 245 | + * @return $this |
|
| 246 | + */ |
|
| 247 | + public function bind($key, & $value) |
|
| 248 | + { |
|
| 249 | + $this->{$key} =& $value; |
|
| 250 | 250 | |
| 251 | - return $this; |
|
| 252 | - } |
|
| 251 | + return $this; |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * Renders the view object to a string. Global and local data are merged |
|
| 256 | - * and extracted to create local variables within the view file. |
|
| 257 | - * |
|
| 258 | - * $output = View::render(); |
|
| 259 | - * |
|
| 260 | - * [!!] Global variables with the same key name as local variables will be |
|
| 261 | - * overwritten by the local variable. |
|
| 262 | - * |
|
| 263 | - * @param string view filename |
|
| 264 | - * @return string |
|
| 265 | - * @throws Kohana_View_Exception |
|
| 266 | - * @uses View::capture |
|
| 267 | - */ |
|
| 268 | - public function render($file = NULL) |
|
| 269 | - { |
|
| 270 | - if ($file !== NULL) |
|
| 271 | - { |
|
| 272 | - $this->set_filename($file); |
|
| 273 | - } |
|
| 254 | + /** |
|
| 255 | + * Renders the view object to a string. Global and local data are merged |
|
| 256 | + * and extracted to create local variables within the view file. |
|
| 257 | + * |
|
| 258 | + * $output = View::render(); |
|
| 259 | + * |
|
| 260 | + * [!!] Global variables with the same key name as local variables will be |
|
| 261 | + * overwritten by the local variable. |
|
| 262 | + * |
|
| 263 | + * @param string view filename |
|
| 264 | + * @return string |
|
| 265 | + * @throws Kohana_View_Exception |
|
| 266 | + * @uses View::capture |
|
| 267 | + */ |
|
| 268 | + public function render($file = NULL) |
|
| 269 | + { |
|
| 270 | + if ($file !== NULL) |
|
| 271 | + { |
|
| 272 | + $this->set_filename($file); |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - if (empty($this->_file)) |
|
| 276 | - { |
|
| 277 | - throw new Kohana_View_Exception('You must set the file to use within your view before rendering'); |
|
| 278 | - } |
|
| 275 | + if (empty($this->_file)) |
|
| 276 | + { |
|
| 277 | + throw new Kohana_View_Exception('You must set the file to use within your view before rendering'); |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - // Combine local and global data and capture the output |
|
| 281 | - return $this->capture($this->_file); |
|
| 282 | - } |
|
| 280 | + // Combine local and global data and capture the output |
|
| 281 | + return $this->capture($this->_file); |
|
| 282 | + } |
|
| 283 | 283 | } // End View |
| 284 | 284 | \ No newline at end of file |
@@ -18,42 +18,42 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class ViewFactory { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Create and return a view of the specified type |
|
| 23 | - * |
|
| 24 | - * @param string $class name of the view class to create |
|
| 25 | - * @param array $data array of data to pass to the view |
|
| 26 | - * |
|
| 27 | - * @return \View |
|
| 28 | - * @throws \View_Exception if the class is not defined |
|
| 29 | - */ |
|
| 30 | - public function create($class, $data = array()) |
|
| 31 | - { |
|
| 32 | - // Check the view class exists |
|
| 33 | - if ( ! class_exists($class)) |
|
| 34 | - { |
|
| 35 | - throw new \View_Exception("View class $class does not exist"); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - // Temporary fix to map template names for namespaced classes |
|
| 39 | - //@todo: Refactor the existing factory methods and classes to support namespaces |
|
| 40 | - if ('\\View\\' === substr($class, 0, 6)) |
|
| 41 | - { |
|
| 42 | - // Strip the \View\ prefix |
|
| 43 | - $template = substr($class, 6); |
|
| 44 | - |
|
| 45 | - // Exchange namespace separators for directory separators |
|
| 46 | - $template = str_replace('\\', DIRECTORY_SEPARATOR, $template); |
|
| 47 | - |
|
| 48 | - // Lowercase the View file name |
|
| 49 | - $template = strtolower($template); |
|
| 50 | - } |
|
| 51 | - else |
|
| 52 | - { |
|
| 53 | - $template = NULL; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - return new $class($template, $data); |
|
| 57 | - } |
|
| 21 | + /** |
|
| 22 | + * Create and return a view of the specified type |
|
| 23 | + * |
|
| 24 | + * @param string $class name of the view class to create |
|
| 25 | + * @param array $data array of data to pass to the view |
|
| 26 | + * |
|
| 27 | + * @return \View |
|
| 28 | + * @throws \View_Exception if the class is not defined |
|
| 29 | + */ |
|
| 30 | + public function create($class, $data = array()) |
|
| 31 | + { |
|
| 32 | + // Check the view class exists |
|
| 33 | + if ( ! class_exists($class)) |
|
| 34 | + { |
|
| 35 | + throw new \View_Exception("View class $class does not exist"); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + // Temporary fix to map template names for namespaced classes |
|
| 39 | + //@todo: Refactor the existing factory methods and classes to support namespaces |
|
| 40 | + if ('\\View\\' === substr($class, 0, 6)) |
|
| 41 | + { |
|
| 42 | + // Strip the \View\ prefix |
|
| 43 | + $template = substr($class, 6); |
|
| 44 | + |
|
| 45 | + // Exchange namespace separators for directory separators |
|
| 46 | + $template = str_replace('\\', DIRECTORY_SEPARATOR, $template); |
|
| 47 | + |
|
| 48 | + // Lowercase the View file name |
|
| 49 | + $template = strtolower($template); |
|
| 50 | + } |
|
| 51 | + else |
|
| 52 | + { |
|
| 53 | + $template = NULL; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + return new $class($template, $data); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | 59 | } |
| 60 | 60 | \ No newline at end of file |