1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Amarkal\UI; |
4
|
|
|
|
5
|
|
|
abstract class AbstractController |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* An associative array holding the model to be used by this controller |
9
|
|
|
* @var array |
10
|
|
|
*/ |
11
|
|
|
protected $model; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Constructor |
15
|
|
|
* |
16
|
|
|
* @param array $model |
17
|
|
|
*/ |
18
|
|
|
public function __construct( array $model = array() ) |
19
|
|
|
{ |
20
|
|
|
$this->set_model( $model ); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get model argument value by name. |
25
|
|
|
* |
26
|
|
|
* @param string $name The argument's name. |
27
|
|
|
* |
28
|
|
|
* @return mixed the argument's value. |
29
|
|
|
*/ |
30
|
|
|
public function __get( $name ) |
31
|
|
|
{ |
32
|
|
|
if( isset( $this->model[$name] ) ) |
33
|
|
|
{ |
34
|
|
|
return $this->model[$name]; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set model argument by name. |
40
|
|
|
* |
41
|
|
|
* @param string $name The argument's name. |
42
|
|
|
* @param mixed $value The value to set. |
43
|
|
|
* |
44
|
|
|
* @return mixed the settings' argument value. |
45
|
|
|
*/ |
46
|
|
|
public function __set( $name, $value ) |
47
|
|
|
{ |
48
|
|
|
$this->model[$name] = $value; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the current component model data. |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function get_model() |
57
|
|
|
{ |
58
|
|
|
return $this->model; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set the model data for this component. |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function set_model( $model ) |
67
|
|
|
{ |
68
|
|
|
$this->model = $model; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the full path to the template file. |
73
|
|
|
* |
74
|
|
|
* @return string The full path. |
75
|
|
|
*/ |
76
|
|
|
abstract function get_template_path(); |
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Render the template with the local properties. |
80
|
|
|
* |
81
|
|
|
* @return string The rendered template. |
82
|
|
|
* @throws TemplateNotFoundException Thrown if the template file cannot be found. |
83
|
|
|
*/ |
84
|
|
|
public function render( $echo = false ){ |
85
|
|
|
|
86
|
|
|
$rendered_html = ''; |
87
|
|
|
|
88
|
|
|
if( file_exists( $this->get_template_path() ) ) |
89
|
|
|
{ |
90
|
|
|
ob_start(); |
91
|
|
|
include( $this->get_template_path() ); |
92
|
|
|
$rendered_html = ob_get_clean(); |
93
|
|
|
} |
94
|
|
|
else |
95
|
|
|
{ |
96
|
|
|
throw new \RuntimeException( "Error: cannot render HTML, template file not found at " . $this->get_template_path() ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if( !$echo ) |
100
|
|
|
{ |
101
|
|
|
return $rendered_html; |
102
|
|
|
} |
103
|
|
|
echo $rendered_html; |
104
|
|
|
} |
105
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.