1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types=1 ); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Wrapper for BladeOne with HTML enabled |
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\BladeOne_Engine |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
26
|
|
|
|
27
|
|
|
namespace PinkCrab\BladeOne; |
28
|
|
|
|
29
|
|
|
use eftec\bladeone\BladeOne; |
|
|
|
|
30
|
|
|
use eftec\bladeonehtml\BladeOneHtml; |
31
|
|
|
use PinkCrab\Perique\Application\App; |
32
|
|
|
use PinkCrab\Perique\Services\View\View; |
33
|
|
|
use PinkCrab\Perique\Services\View\View_Model; |
34
|
|
|
use PinkCrab\Perique\Services\View\Component\Component; |
35
|
|
|
|
36
|
|
|
class PinkCrab_BladeOne extends BladeOne { |
37
|
|
|
use BladeOneHtml; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Bob the constructor. |
41
|
|
|
* The folder at $compiled_path is created in case it doesn't exist. |
42
|
|
|
* |
43
|
|
|
* @param string|string[] $template_path If null then it uses (caller_folder)/views |
44
|
|
|
* @param string $compiled_path If null then it uses (caller_folder)/compiles |
45
|
|
|
* @param int $mode =[BladeOne::MODE_AUTO,BladeOne::MODE_DEBUG,BladeOne::MODE_FAST,BladeOne::MODE_SLOW][$i] |
46
|
|
|
*/ |
47
|
|
|
public function __construct( $template_path = null, $compiled_path = null, $mode = 0 ) { |
48
|
|
|
parent::__construct( $template_path, $compiled_path, $mode ); |
49
|
|
|
|
50
|
|
|
// Add the viewModel directive. |
51
|
|
|
$this->directiveRT( 'viewModel', fn( $expression ) => $this->view_model( $expression, true ) ); |
52
|
|
|
|
53
|
|
|
// Add the component directive. |
54
|
|
|
$this->directiveRT( 'viewComponent', fn( $expression ) => $this->component( $expression, true ) ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The esc function to use |
59
|
|
|
* |
60
|
|
|
* @var callable(mixed):string |
61
|
|
|
*/ |
62
|
|
|
protected static $esc_function = 'esc_html'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The default echo format |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $echoFormat = '\esc_html(%s)'; //phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets the esc function to use |
74
|
|
|
* |
75
|
|
|
* @param string $esc_function |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function set_esc_function( string $esc_function ): void { |
79
|
|
|
// Throw exception if not a valid callable. |
80
|
|
|
if ( ! \is_callable( $esc_function ) ) { |
81
|
|
|
throw new \InvalidArgumentException( 'Invalid esc function provided.' ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
static::$esc_function = $esc_function; |
85
|
|
|
$this->echoFormat = sprintf( '\\%s(%%s)', $esc_function ); //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the template paths |
90
|
|
|
* |
91
|
|
|
* @return string[] |
92
|
|
|
*/ |
93
|
|
|
public function get_template_paths(): array { |
94
|
|
|
return $this->templatePath; //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Escape HTML entities in a string. |
99
|
|
|
* |
100
|
|
|
* @param int|float|string|null|mixed[]|object $value |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public static function e( $value ): string { |
104
|
|
|
if ( \is_null( $value ) ) { |
105
|
|
|
return ''; |
106
|
|
|
} |
107
|
|
|
if ( \is_array( $value ) || \is_object( $value ) ) { |
108
|
|
|
return \call_user_func( static::$esc_function, \print_r( $value, true ) );//phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
109
|
|
|
} |
110
|
|
|
if ( \is_numeric( $value ) ) { |
111
|
|
|
$value = (string) $value; |
112
|
|
|
} |
113
|
|
|
return \call_user_func( static::$esc_function, $value ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Renders component |
118
|
|
|
* |
119
|
|
|
* @param Component $component |
120
|
|
|
* @param bool $print |
121
|
|
|
* @return string|void |
122
|
|
|
*/ |
123
|
|
|
public function component( Component $component, bool $print = true ) { |
124
|
|
|
/** @var View */ |
125
|
|
|
$view = App::view(); |
126
|
|
|
|
127
|
|
|
return $view->component( $component, $print ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Renders a view model |
132
|
|
|
* |
133
|
|
|
* @param View_Model $view_model |
134
|
|
|
* @param bool $print Print or Return the HTML |
135
|
|
|
* @return string|void |
136
|
|
|
*/ |
137
|
|
|
public function view_model( View_Model $view_model, bool $print = true ) { |
138
|
|
|
/** @var View */ |
139
|
|
|
$view = App::view(); |
140
|
|
|
|
141
|
|
|
return $view->view_model( $view_model, $print ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Compile the auth statements into valid PHP. |
146
|
|
|
* |
147
|
|
|
* @param string $expression |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
protected function compileAuth( $expression = '' ): string { |
151
|
|
|
$role = $this->stripParentheses( $expression ); |
152
|
|
|
if ( $role === '' ) { |
153
|
|
|
return $this->phpTag . 'if(!PinkCrab\FunctionConstructors\Strings\isBlank($this->currentUser)): ?>'; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->phpTag . "if(!PinkCrab\FunctionConstructors\Strings\isBlank(\$this->currentUser) && \$this->currentRole==$role): ?>"; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Compile the elseauth statements into valid PHP. |
161
|
|
|
* |
162
|
|
|
* @param string $expression |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
protected function compileElseAuth( $expression = '' ): string { |
166
|
|
|
$role = $this->stripParentheses( $expression ); |
167
|
|
|
if ( $role === '' ) { |
168
|
|
|
return $this->phpTag . 'else: ?>'; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->phpTag . "elseif(!PinkCrab\FunctionConstructors\Strings\isBlank(\$this->currentUser) && \$this->currentRole==$role): ?>"; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Compile the guest statements into valid PHP. |
176
|
|
|
* |
177
|
|
|
* @param string|null $expression |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
protected function compileGuest( $expression = null ): string { |
181
|
|
|
|
182
|
|
|
if ( $expression === null ) { |
183
|
|
|
return $this->phpTag . 'if(PinkCrab\FunctionConstructors\Strings\isBlank($this->currentUser)): ?>'; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$role = $this->stripParentheses( $expression ); |
187
|
|
|
if ( $role === '' ) { |
188
|
|
|
return $this->phpTag . 'if(PinkCrab\FunctionConstructors\Strings\isBlank($this->currentUser)): ?>'; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this->phpTag . "if(PinkCrab\FunctionConstructors\Strings\isBlank(\$this->currentUser) || \$this->currentRole!=$role): ?>"; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Compile the else statements into valid PHP. |
196
|
|
|
* |
197
|
|
|
* @param string|null $expression |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
protected function compileElseGuest( $expression ): string { |
201
|
|
|
$role = $this->stripParentheses( $expression ); |
202
|
|
|
if ( $role === '' ) { |
203
|
|
|
return $this->phpTag . 'else: ?>'; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this->phpTag . "elseif(PinkCrab\FunctionConstructors\Strings\isBlank(\$this->currentUser) || \$this->currentRole!=$role): ?>"; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: