1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Acts as an object wrapper for output with embedded PHP, called "views". |
4
|
|
|
* Variables can be assigned with the view object and referenced locally within |
5
|
|
|
* the view. |
6
|
|
|
* |
7
|
|
|
* @package Kohana |
8
|
|
|
* @category Base |
9
|
|
|
* @author Kohana Team |
10
|
|
|
* @copyright (c) 2008-2010 Kohana Team |
11
|
|
|
* @license http://kohanaphp.com/license |
12
|
|
|
*/ |
13
|
|
|
class Kohana_View_Model { |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Captures the output that is generated when a view is included. |
17
|
|
|
* The view data will be extracted to make local variables. |
18
|
|
|
* |
19
|
|
|
* $output = $this->capture($file); |
20
|
|
|
* |
21
|
|
|
* @param string filename |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
protected function capture($kohana_view_filename) |
25
|
|
|
{ |
26
|
|
|
if ( ! in_array('kohana.view', stream_get_wrappers())) |
27
|
|
|
{ |
28
|
|
|
stream_wrapper_register('kohana.view', 'View_Stream_Wrapper'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// Capture the view output |
32
|
|
|
ob_start(); |
33
|
|
|
|
34
|
|
|
try |
35
|
|
|
{ |
36
|
|
|
include 'kohana.view://'.$kohana_view_filename; |
37
|
|
|
} |
38
|
|
|
catch (Exception $e) |
39
|
|
|
{ |
40
|
|
|
// Delete the output buffer |
41
|
|
|
ob_end_clean(); |
42
|
|
|
|
43
|
|
|
// Re-throw the exception |
44
|
|
|
throw $e; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Get the captured output and close the buffer |
48
|
|
|
return ob_get_clean(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Magic method, returns the output of [View::render]. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
* @uses View::render |
56
|
|
|
*/ |
57
|
|
|
public function __toString() |
58
|
|
|
{ |
59
|
|
|
try |
60
|
|
|
{ |
61
|
|
|
return $this->render(); |
62
|
|
|
} |
63
|
|
|
catch (Exception $e) |
64
|
|
|
{ |
65
|
|
|
// Display the exception message |
66
|
|
|
Kohana_Exception::handler($e); |
67
|
|
|
|
68
|
|
|
return ''; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets the view filename. |
74
|
|
|
* |
75
|
|
|
* $view->set_filename($file); |
76
|
|
|
* |
77
|
|
|
* @param string view filename |
78
|
|
|
* @return View |
79
|
|
|
* @throws Kohana_View_Exception |
80
|
|
|
*/ |
81
|
|
|
public function set_filename($file) |
82
|
|
|
{ |
83
|
|
|
if (($path = Kohana::find_file('views', $file)) === FALSE) |
84
|
|
|
{ |
85
|
|
|
throw new Kohana_View_Exception('The requested view :file could not be found', [ |
86
|
|
|
':file' => $file, |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Store the file path locally |
91
|
|
|
$this->_file = $path; |
|
|
|
|
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Renders the view object to a string. Global and local data are merged |
98
|
|
|
* and extracted to create local variables within the view file. |
99
|
|
|
* |
100
|
|
|
* $output = View::render(); |
101
|
|
|
* |
102
|
|
|
* [!!] Global variables with the same key name as local variables will be |
103
|
|
|
* overwritten by the local variable. |
104
|
|
|
* |
105
|
|
|
* @param string view filename |
106
|
|
|
* @return string |
107
|
|
|
* @throws Kohana_View_Exception |
108
|
|
|
* @uses View::capture |
109
|
|
|
*/ |
110
|
|
|
public function render($file = NULL) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
|
113
|
|
|
// Combine local and global data and capture the output |
114
|
|
|
return $this->capture($this->_file); |
115
|
|
|
} |
116
|
|
|
} // End View |
117
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.