1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* The main view class. |
7
|
|
|
* |
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
19
|
|
|
* |
20
|
|
|
* @author Glynn Quelch <[email protected]> |
21
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
22
|
|
|
* @package PinkCrab\Perique\View |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace PinkCrab\Perique\Services\View; |
26
|
|
|
|
27
|
|
|
use PinkCrab\Perique\Interfaces\Renderable; |
28
|
|
|
use PinkCrab\Perique\Services\View\View_Model; |
29
|
|
|
use PinkCrab\Perique\Services\View\Component\Component; |
30
|
|
|
use PinkCrab\Perique\Services\View\Component\Component_Compiler; |
31
|
|
|
|
32
|
|
|
class View { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* IF the view should be returned as a string. |
36
|
|
|
*/ |
37
|
|
|
public const RETURN_VIEW = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* If the view should be printed. |
41
|
|
|
*/ |
42
|
|
|
public const PRINT_VIEW = true; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The current view engine. |
46
|
|
|
* |
47
|
|
|
* @var Renderable |
48
|
|
|
*/ |
49
|
|
|
protected Renderable $engine; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The component compiler |
53
|
|
|
* |
54
|
|
|
* @var Component_Compiler |
55
|
|
|
*/ |
56
|
|
|
protected Component_Compiler $component_compiler; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Creates an instance of view with the passed engine. |
61
|
|
|
* |
62
|
|
|
* @param Renderable $engine |
63
|
|
|
*/ |
64
|
|
|
public function __construct( Renderable $engine, Component_Compiler $component_compiler ) { |
65
|
|
|
$this->engine = $engine; |
66
|
|
|
$this->component_compiler = $component_compiler; |
67
|
|
|
|
68
|
|
|
// Populate engine with compiler. |
69
|
|
|
$this->engine->set_component_compiler( $component_compiler ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Renders a view with passed data. |
74
|
|
|
* |
75
|
|
|
* @param string $view |
76
|
|
|
* @param iterable<string, mixed> $view_data |
77
|
|
|
* @param bool $print Print or Return the HTML |
78
|
|
|
* @return string|void |
79
|
|
|
*/ |
80
|
|
|
public function render( string $view, iterable $view_data = array(), bool $print = self::PRINT_VIEW ) { |
81
|
|
|
return $this->engine->render( $view, $view_data, $print ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Renders a component. |
86
|
|
|
* |
87
|
|
|
* @param Component $component |
88
|
|
|
* @param bool $print Print or Return the HTML |
89
|
|
|
* @return string|void |
90
|
|
|
*/ |
91
|
|
|
public function component( Component $component, bool $print = self::PRINT_VIEW ) { |
92
|
|
|
return $this->engine->component( $component, $print ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Renders a view model |
97
|
|
|
* |
98
|
|
|
* @param View_Model $view_model |
99
|
|
|
* @param bool $print Print or Return the HTML |
100
|
|
|
* @return string|void |
101
|
|
|
*/ |
102
|
|
|
public function view_model( View_Model $view_model, bool $print = self::PRINT_VIEW ) { |
103
|
|
|
return $this->engine->view_model( $view_model, $print ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Buffer for use with WordPress functions that display directly. |
108
|
|
|
* |
109
|
|
|
* @param callable $to_buffer |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public static function print_buffer( callable $to_buffer ): string { |
113
|
|
|
ob_start(); |
114
|
|
|
$to_buffer(); |
115
|
|
|
$output = ob_get_contents(); |
116
|
|
|
ob_end_clean(); |
117
|
|
|
return $output ?: ''; // phpcs:ignore Universal.Operators.DisallowShortTernary.Found |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns access to the internal rendering engine. |
122
|
|
|
* |
123
|
|
|
* @return Renderable |
124
|
|
|
*/ |
125
|
|
|
public function engine(): Renderable { |
126
|
|
|
return $this->engine; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the base path for the view. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
* @since 1.4.0 |
134
|
|
|
*/ |
135
|
|
|
public function base_path(): string { |
136
|
|
|
return $this->engine->base_view_path(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|