PHPEngine   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 25 2
A ensure_is_object() 0 14 3
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\Render;
13
14
/**
15
 * Renders PHP templates.
16
 */
17
class PHPEngine implements Engine
18
{
19
	/**
20
	 * @inheritdoc
21
	 */
22
	public function render($template_pathname, $thisArg, array $variables, array $options = [])
23
	{
24
		$f = \Closure::bind(function($__TEMPLATE_PATHNAME__, $__VARIABLES__) {
25
26
			extract($__VARIABLES__);
27
28
			require $__TEMPLATE_PATHNAME__;
29
30
		}, $this->ensure_is_object($thisArg));
31
32
		ob_start();
33
34
		try
35
		{
36
			$f($template_pathname, [ self::VAR_TEMPLATE_PATHNAME => $template_pathname ] + $variables);
37
38
			return ob_get_clean();
39
		}
40
		catch (\Exception $e)
41
		{
42
			ob_end_clean();
43
44
			throw $e;
45
		}
46
	}
47
48
	/**
49
	 * Ensures that a value is an object.
50
	 *
51
	 * - `value` is an object, value is returned.
52
	 * - `value` is an array, an `ArrayObject` instance is returned.
53
	 * - Otherwise `value` is cast into a string and a {@link String} instance is returned.
54
	 *
55
	 * @param $value
56
	 *
57
	 * @return \ArrayObject|StringObject
58
	 */
59
	protected function ensure_is_object($value)
60
	{
61
		if (is_object($value))
62
		{
63
			return $value;
64
		}
65
66
		if (is_array($value))
67
		{
68
			return new \ArrayObject($value);
69
		}
70
71
		return new StringObject($value);
72
	}
73
}
74