Completed
Push — master ( 02d85d...e7ad5b )
by Glynn
32s queued 14s
created

BladeOne_Provider::set_esc_function()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types=1 );
4
5
/**
6
 * Implementation of BladeOne for the PinkCrab Perique frameworks Renderable interface
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_Provider
23
 */
24
25
namespace PinkCrab\BladeOne;
26
27
use Exception;
28
use ReflectionClass;
29
use BadMethodCallException;
30
use eftec\bladeone\BladeOne;
31
use eftec\bladeonehtml\BladeOneHtml;
32
use PinkCrab\BladeOne\PinkCrab_BladeOne;
33
use PinkCrab\Perique\Interfaces\Renderable;
34
use PinkCrab\Perique\Services\View\View_Model;
35
use PinkCrab\Perique\Services\View\Component\Component;
36
use PinkCrab\Perique\Services\View\Component\Component_Compiler;
37
38
class BladeOne_Provider implements Renderable {
39
40
	/**
41
	 * BladeOne Instance
42
	 *
43
	 * @var PinkCrab_BladeOne
44
	 */
45
	protected static $blade;
46
47
	/**
48
	 * Access to the component compiler.
49
	 *
50
	 * @var Component_Compiler
51
	 */
52
	protected $component_compiler;
53
54
	/**
55
	 * Creates an instance with blade one.
56
	 *
57
	 * @param PinkCrab_BladeOne $blade
58
	 */
59
	final public function __construct( PinkCrab_BladeOne $blade ) {
60
		static::$blade = $blade;
61
	}
62
63
	/**
64
	 * Static constructor with BladeOne initialisation details
65
	 *
66
	 * @param string|array<mixed> $template_path If null then it uses (caller_folder)/views
67
	 * @param string $compiled_path If null then it uses (caller_folder)/compiles
68
	 * @param int $mode =[BladeOne::MODE_AUTO,BladeOne::MODE_DEBUG,BladeOne::MODE_FAST,BladeOne::MODE_SLOW][$i]
69
	 * @return self
70
	 */
71
	public static function init(
72
		$template_path = null,
73
		?string $compiled_path = null,
74
		int $mode = 0
75
	): self {
76
		return new static( new PinkCrab_BladeOne( $template_path, $compiled_path, $mode ) );
77
	}
78
79
	/**
80
	 * Returns the current BladeOne instance.
81
	 *
82
	 * @return BladeOne
83
	 */
84
	public function get_blade(): BladeOne {
85
		return static::$blade;
86
	}
87
88
	/**
89
	 * Sets the esc function.
90
	 *
91
	 * @param string $esc
92
	 * @return self
93
	 */
94
	public function set_esc_function( string $esc ): self {
95
		static::$blade->set_esc_function( $esc );
96
		return $this;
97
	}
98
99
	/**
100
	 * Sets the component compiler.
101
	 *
102
	 * @param Component_Compiler $compiler
103
	 * @return void
104
	 */
105
	public function set_component_compiler( Component_Compiler $compiler ): void {
106
		$this->component_compiler = $compiler;
107
	}
108
109
	/**
110
	 * Display a view and its context.
111
	 *
112
	 * @param string $view
113
	 * @param iterable<string, mixed> $data
114
	 * @param bool $print
115
	 * @return void|string
116
	 */
117
	public function render( string $view, iterable $data, bool $print = true ) {
118
		if ( $print ) {
119
			print static::$blade->run( $view, (array) $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
120
		} else {
121
			return static::$blade->run( $view, (array) $data );
122
		}
123
	}
124
125
	/**
126
	 * Renders a view Model
127
	 *
128
	 * @param View_Model $view_model
129
	 * @return string|void
130
	 */
131
	public function view_model( View_Model $view_model, bool $print = true ) {
132
		return $this->render( str_replace( array( '/', '\\' ), '.', $view_model->template() ), $view_model->data(), $print );
133
	}
134
135
		/**
136
	 * Renders a component.
137
	 *
138
	 * @param Component $component
139
	 * @return string|void
140
	 */
141
	public function component( Component $component, bool $print = true ) {
142
143
		// Throw exception of no compiler passed.
144
		if ( ! is_a( $this->component_compiler, Component_Compiler::class ) ) {
145
			throw new Exception( 'No component compiler passed to BladeOne' );
146
		}
147
148
		// Compile the component.
149
		$compiled = $this->component_compiler->compile( $component );
150
		return $this->render( str_replace( array( '/', '\\' ), '.', $compiled->template() ), $compiled->data(), $print );
151
	}
152
153
	/**
154
	 * magic instanced method caller.
155
	 *
156
	 * @param string $method
157
	 * @param array<mixed> $args
158
	 * @return mixed
159
	 * @throws BadMethodCallException
160
	 */
161
	public function __call( string $method, array $args = array() ) {
162
		if ( ! $this->is_method( $method ) ) {
163
			throw new BadMethodCallException( "{$method} is not a valid method on the BladeOne instance." );
164
		}
165
166
		return static::$blade->{$method}( ...$args );
167
	}
168
169
	/**
170
	 * Magic static method caller.
171
	 *
172
	 * @param string $method
173
	 * @param array<mixed> $args
174
	 * @return mixed
175
	 * @throws BadMethodCallException
176
	 */
177
	public static function __callStatic( string $method, array $args = array() ) {
178
		if ( ! static::is_static_method( $method ) ) {
179
			throw new BadMethodCallException( "{$method} is not a valid method on the BladeOne instance." );
180
		}
181
182
		return static::$blade::{$method}( ...$args );
183
	}
184
185
	/**
186
	 * Checks if the passed method exists, is public and isnt static.
187
	 *
188
	 * @param string $method
189
	 * @return bool
190
	 */
191
	protected function is_method( string $method ): bool {
192
		$class_reflection = new ReflectionClass( static::$blade );
193
194
		// Check method exists.
195
		if ( ! $class_reflection->hasMethod( $method ) ) {
196
			return false;
197
		}
198
199
		$method_reflection = $class_reflection->getMethod( $method );
200
201
		return $method_reflection->isPublic() && ! $method_reflection->isStatic();
202
	}
203
204
	/**
205
	 * Checks if the passed method exists, is public and IS static.
206
	 *
207
	 * @param string $method
208
	 * @return bool
209
	 */
210
	protected static function is_static_method( string $method ): bool {
211
		$class_reflection = new ReflectionClass( static::$blade );
212
213
		// Check method exists.
214
		if ( ! $class_reflection->hasMethod( $method ) ) {
215
			return false;
216
		}
217
218
		$method_reflection = $class_reflection->getMethod( $method );
219
		return $method_reflection->isPublic() && $method_reflection->isStatic();
220
	}
221
222
	/**
223
	 * Sets if piping is enabled in templates.
224
	 *
225
	 * @param bool $bool
226
	 * @return self
227
	 */
228
	public function allow_pipe( bool $bool = true ): self {
229
		static::$blade->pipeEnable = $bool; //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
230
		return $this;
231
	}
232
233
	/**
234
	 * Register a handler for custom directives.
235
	 *
236
	 * @param string   $name
237
	 * @param callable $handler
238
	 * @return self
239
	 */
240
	public function directive( string $name, callable $handler ): self {
241
		static::$blade->directive( $name, $handler );
242
		return $this;
243
	}
244
245
	/**
246
	 * Register a handler for custom directives for run at runtime
247
	 *
248
	 * @param string   $name
249
	 * @param callable $handler
250
	 * @return self
251
	 */
252
	public function directive_rt( $name, callable $handler ): self {
253
		static::$blade->directiveRT( $name, $handler );
254
		return $this;
255
	}
256
257
	/**
258
	 * Define a template alias
259
	 *
260
	 * @param string      $view  example "folder.template"
261
	 * @param string|null $alias example "mynewop". If null then it uses the name of the template.
262
	 * @return self
263
	 */
264
	public function add_include( $view, $alias = null ): self {
265
		static::$blade->addInclude( $view, $alias );
266
		return $this;
267
	}
268
269
	/**
270
	 * Define a class with a namespace
271
	 *
272
	 * @param string $alias_name
273
	 * @param string $class_with_namespace
274
	 * @return self
275
	 */
276
	public function add_alias_classes( $alias_name, $class_with_namespace ): self {
277
		static::$blade->addAliasClasses( $alias_name, $class_with_namespace );
278
		return $this;
279
	}
280
281
	/**
282
	 * Set the compile mode
283
	 *
284
	 * @param int $mode BladeOne::MODE_AUTO, BladeOne::MODE_DEBUG, BladeOne::MODE_FAST, BladeOne::MODE_SLOW
285
	 * @return self
286
	 */
287
	public function set_mode( int $mode ): self {
288
		static::$blade->setMode( $mode );
289
		return $this;
290
	}
291
292
	/**
293
	 * Adds a global variable. If <b>$var_name</b> is an array then it merges all the values.
294
	 * <b>Example:</b>
295
	 * <pre>
296
	 * $this->share('variable',10.5);
297
	 * $this->share('variable2','hello');
298
	 * // or we could add the two variables as:
299
	 * $this->share(['variable'=>10.5,'variable2'=>'hello']);
300
	 * </pre>
301
	 *
302
	 * @param string|array<string, mixed> $var_name It is the name of the variable or it is an associative array
303
	 * @param mixed        $value
304
	 * @return $this
305
	 */
306
	public function share( $var_name, $value = null ): self {
307
		static::$blade->share( $var_name, $value );
308
		return $this;
309
	}
310
311
	/**
312
	 * Sets the function used for resolving classes with inject.
313
	 *
314
	 * @param callable $function
315
	 * @return $this
316
	 */
317
	public function set_inject_resolver( callable $function ): self {
318
		static::$blade->setInjectResolver( $function );
319
		return $this;
320
	}
321
322
	/**
323
	 * Set the file extension for the template files.
324
	 * It must includes the leading dot e.g. .blade.php
325
	 *
326
	 * @param string $file_extension Example: .prefix.ext
327
	 * @return $this
328
	 */
329
	public function set_file_extension( string $file_extension ): self {
330
		static::$blade->setFileExtension( $file_extension );
331
		return $this;
332
	}
333
334
	/**
335
	 * Set the file extension for the compiled files.
336
	 * Including the leading dot for the extension is required, e.g. .bladec
337
	 *
338
	 * @param string $file_extension
339
	 * @return $this
340
	 */
341
	public function set_compiled_extension( string $file_extension ): self {
342
		static::$blade->setCompiledExtension( $file_extension );
343
		return $this;
344
	}
345
346
}
347
348