1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bright Nucleus View Component. |
4
|
|
|
* |
5
|
|
|
* @package BrightNucleus\View |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2016 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\View\View; |
13
|
|
|
|
14
|
|
|
use BrightNucleus\View\View; |
15
|
|
|
use BrightNucleus\View\Engine\Engine; |
16
|
|
|
use BrightNucleus\View\ViewBuilder; |
17
|
|
|
use BrightNucleus\Views; |
18
|
|
|
use Closure; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Abstract class AbstractView. |
22
|
|
|
* |
23
|
|
|
* @since 0.1.0 |
24
|
|
|
* |
25
|
|
|
* @package BrightNucleus\View\View |
26
|
|
|
* @author Alain Schlesser <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractView implements View |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* URI of the view. |
33
|
|
|
* |
34
|
|
|
* @since 0.1.0 |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $uri; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Engine to use for the view. |
42
|
|
|
* |
43
|
|
|
* @since 0.1.0 |
44
|
|
|
* |
45
|
|
|
* @var Engine |
46
|
|
|
*/ |
47
|
|
|
protected $engine; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* ViewBuilder instance. |
51
|
|
|
* |
52
|
|
|
* @since 0.2.0 |
53
|
|
|
* |
54
|
|
|
* @var ViewBuilder |
55
|
|
|
*/ |
56
|
|
|
protected $builder; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Instantiate an AbstractView object. |
60
|
|
|
* |
61
|
|
|
* @since 0.1.0 |
62
|
|
|
* |
63
|
|
|
* @param string $uri URI for the view. |
64
|
|
|
* @param Engine $engine Engine to use for the view. |
65
|
|
|
*/ |
66
|
29 |
|
public function __construct($uri, Engine $engine) |
67
|
|
|
{ |
68
|
29 |
|
$this->uri = $uri; |
69
|
29 |
|
$this->engine = $engine; |
70
|
29 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Render the view. |
74
|
|
|
* |
75
|
|
|
* @since 0.1.0 |
76
|
|
|
* |
77
|
|
|
* @param array $context Optional. The context in which to render the view. |
78
|
|
|
* @param bool $echo Optional. Whether to echo the output immediately. Defaults to false. |
79
|
|
|
* |
80
|
|
|
* @return string|void Rendered HTML or nothing, depending on $echo argument. |
81
|
|
|
*/ |
82
|
29 |
|
public function render(array $context = [], $echo = false) |
83
|
|
|
{ |
84
|
29 |
|
$this->initializeViewBuilder(); |
85
|
29 |
|
$this->assimilateContext($context); |
86
|
|
|
|
87
|
29 |
|
$closure = Closure::bind( |
88
|
29 |
|
$this->engine->getRenderCallback($this->uri, $context), |
89
|
|
|
$this, |
90
|
29 |
|
static::class |
91
|
|
|
); |
92
|
|
|
|
93
|
29 |
|
if ( ! $echo) { |
94
|
29 |
|
return $closure(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
echo $closure(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Render a partial view for a given URI. |
102
|
|
|
* |
103
|
|
|
* @since 0.2.0 |
104
|
|
|
* |
105
|
|
|
* @param string $view View identifier to create a view for. |
106
|
|
|
* @param array $context Optional. The context in which to render the view. |
|
|
|
|
107
|
|
|
* @param string|null $type Type of view to create. |
108
|
|
|
* |
109
|
|
|
* @return string Rendered HTML content. |
110
|
|
|
*/ |
111
|
1 |
|
public function renderPart($view, array $context = null, $type = null) |
112
|
|
|
{ |
113
|
1 |
|
if (null === $context) { |
114
|
1 |
|
$context = $this->context; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
$this->initializeViewBuilder(); |
118
|
1 |
|
$viewObject = $this->builder->create($view, $type); |
119
|
|
|
|
120
|
1 |
|
return $viewObject->render($context); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Associate a view builder with this view. |
125
|
|
|
* |
126
|
|
|
* @since 0.2.0 |
127
|
|
|
* |
128
|
|
|
* @param ViewBuilder $builder |
129
|
|
|
* |
130
|
|
|
* @return static |
131
|
|
|
*/ |
132
|
29 |
|
public function setBuilder(ViewBuilder $builder) |
133
|
|
|
{ |
134
|
29 |
|
$this->builder = $builder; |
135
|
|
|
|
136
|
29 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Initialize the view builder associated with the view. |
141
|
|
|
* |
142
|
|
|
* @since 0.2.0 |
143
|
|
|
*/ |
144
|
29 |
|
protected function initializeViewBuilder() |
145
|
|
|
{ |
146
|
29 |
|
if (null === $this->builder) { |
147
|
|
|
$this->builder = Views::getViewBuilder(); |
148
|
|
|
} |
149
|
29 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Assimilate the context to make it available as properties. |
153
|
|
|
* |
154
|
|
|
* @since 0.2.0 |
155
|
|
|
* |
156
|
|
|
* @param array $context Context to assimilate. |
157
|
|
|
*/ |
158
|
29 |
|
protected function assimilateContext(array $context = []) |
159
|
|
|
{ |
160
|
29 |
|
$this->context = $context; |
161
|
29 |
|
foreach ($context as $key => $value) { |
162
|
29 |
|
$this->$key = $value; |
163
|
|
|
} |
164
|
29 |
|
} |
165
|
|
|
} |
166
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
array
and suggests a stricter type likearray<String>
.Most often this is a case of a parameter that can be null in addition to its declared types.