-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathview.php
124 lines (113 loc) · 2.45 KB
/
view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* @package wp-content-aware-engine
* @author Joachim Jensen <[email protected]>
* @license GPLv3
* @copyright 2023 by Joachim Jensen
*/
defined('ABSPATH') || exit;
/**
* View Class
*/
class WPCAView
{
/**
* Path to view template
*
* @var string
*/
private $_path;
/**
* Parameters for view template
*
* @var array
*/
private $_params;
/**
* Template content
*
* @var string
*/
private $_content;
/**
* Constructor
*
* @since 1.0
* @param string $path
* @param array $params
*/
private function __construct($path, $params = [])
{
$this->_path = $path;
$this->_params = $params;
}
/**
* Create instance of view
*
* @since 1.0
* @param string $name
* @param array $params
* @return WPCAView
*/
public static function make($name, $params = [])
{
return new self($name, $params);
}
/**
* Add possibility to set params dynamically
*
* @since 1.0
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
if (substr($method, 0, 3) == 'set' && count($args) == 1) {
$this->_params[strtolower(substr($method, 2))] = $args[0];
return $this;
}
return call_user_func_array($method, $args);
}
/**
* Get path to file
*
* @since 1.0
* @return string
*/
private function resolve_path()
{
if (stripos(strrev($this->_path), 'php.') === 0) {
return $this->_path;
}
return WPCA_PATH.'view/'.str_replace('.', '/', $this->_path).'.php';
}
/**
* Get content
*
* @since 1.0
* @return string
*/
private function get_content()
{
if (!$this->_content) {
ob_start();
if ($this->_params) {
extract($this->_params);
}
require($this->resolve_path());
$this->_content = ob_get_clean();
}
return $this->_content;
}
/**
* Render content
*
* @since 1.0
* @return void
*/
public function render()
{
echo $this->get_content();
}
}