Completed
Pull Request — 2.0.x (#6)
by Andrew
02:36
created

Kohana_View_Model::__get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The property _file does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
	{
112
113
		// Combine local and global data and capture the output
114
		return $this->capture($this->_file);
115
	}
116
} // End View
117