1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\View; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A view connected to a template file. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class CView implements \Anax\DI\IInjectionAware |
10
|
|
|
{ |
11
|
|
|
use THelpers, |
12
|
|
|
\Anax\DI\TInjectionAware; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Properties |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
private $template; // Template file or array |
21
|
|
|
private $templateData = []; // Data to send to template file |
22
|
|
|
private $sortOrder; // For sorting views |
23
|
|
|
private $type; // Type of view |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set values for the view. |
29
|
|
|
* |
30
|
|
|
* @param string/array $template the template file, or array |
|
|
|
|
31
|
|
|
* @param array $data variables to make available to the |
32
|
|
|
* view, default is empty |
33
|
|
|
* @param int $sort which order to display the views, |
34
|
|
|
* if suitable |
35
|
|
|
* @param string $type which type of view |
36
|
|
|
* |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function set($template, $data = [], $sort = 0, $type = "file") |
40
|
|
|
{ |
41
|
|
|
if (is_array($template)) { |
42
|
|
|
if (isset($template["callback"])) { |
43
|
|
|
$type = "callback"; |
44
|
|
|
$this->template = $template; |
45
|
|
|
} else { |
46
|
|
|
$this->template = $template["template"]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->templateData = isset($template["data"]) |
50
|
|
|
? $template["data"] |
51
|
|
|
: $data; |
52
|
|
|
|
53
|
|
|
$this->sortOrder = isset($template["sort"]) |
54
|
|
|
? $template["sort"] |
55
|
|
|
: $sort; |
56
|
|
|
|
57
|
|
|
$this->type = isset($template["type"]) |
58
|
|
|
? $template["type"] |
59
|
|
|
: $type; |
60
|
|
|
|
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->template = $template; |
65
|
|
|
$this->templateData = $data; |
66
|
|
|
$this->sortOrder = $sort; |
67
|
|
|
$this->type = $type; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Render the view. |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function render() |
80
|
|
|
{ |
81
|
|
|
switch ($this->type) { |
82
|
|
|
case "file": |
83
|
|
|
if (!is_readable($this->template)) { |
84
|
|
|
throw new \Exception("Could not find template file: " . $this->template); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
extract($this->templateData); |
88
|
|
|
include $this->template; |
89
|
|
|
|
90
|
|
|
break; |
91
|
|
|
|
92
|
|
|
case "callback": |
93
|
|
|
if (!isset($this->template["callback"]) || !is_callable($this->template["callback"])) { |
94
|
|
|
throw new \Exception("View missing callback."); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
echo call_user_func($this->template["callback"]); |
98
|
|
|
|
99
|
|
|
break; |
100
|
|
|
|
101
|
|
|
case "string": |
102
|
|
|
echo $this->template; |
103
|
|
|
|
104
|
|
|
break; |
105
|
|
|
|
106
|
|
|
default: |
107
|
|
|
throw new \Exception("Not a valid template type: " . $this->type); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Give the sort order for this view. |
115
|
|
|
* |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
|
|
public function sortOrder() |
119
|
|
|
{ |
120
|
|
|
return $this->sortOrder; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.