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\Engine; |
13
|
|
|
|
14
|
|
|
use BrightNucleus\View\Exception\FailedToLoadViewException; |
15
|
|
|
use Exception; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class PHPEngine. |
19
|
|
|
* |
20
|
|
|
* @since 0.1.0 |
21
|
|
|
* |
22
|
|
|
* @package BrightNucleus\View\Engine |
23
|
|
|
* @author Alain Schlesser <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class PHPEngine extends AbstractEngine |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
const PHP_EXTENSION = '.php'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Check whether the Findable can handle an individual criterion. |
32
|
|
|
* |
33
|
|
|
* @since 0.1.0 |
34
|
|
|
* |
35
|
|
|
* @param mixed $criterion Criterion to check. |
36
|
|
|
* |
37
|
|
|
* @return bool Whether the Findable can handle the criterion. |
38
|
|
|
*/ |
39
|
17 |
|
public function canHandle($criterion) |
40
|
|
|
{ |
41
|
17 |
|
return $this->hasExtension($criterion, static::PHP_EXTENSION) |
42
|
17 |
|
&& is_readable($criterion); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Render a given URI. |
47
|
|
|
* |
48
|
|
|
* @since 0.1.0 |
49
|
|
|
* |
50
|
|
|
* @param string $uri URI to render. |
51
|
|
|
* @param array $context Context in which to render. |
52
|
|
|
* |
53
|
|
|
* @return string Rendered HTML. |
54
|
|
|
* @throws FailedToLoadViewException If the View URI is not accessible or readable. |
55
|
|
|
* @throws FailedToLoadViewException If the View URI could not be loaded. |
56
|
|
|
*/ |
57
|
17 |
|
public function render($uri, array $context = []) |
58
|
|
|
{ |
59
|
17 |
|
if (! is_readable($uri)) { |
60
|
|
|
throw new FailedToLoadViewException( |
61
|
|
|
sprintf( |
62
|
|
|
_('The View URI "%1$s" is not accessible or readable.'), |
63
|
|
|
$uri |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
17 |
|
extract($context, EXTR_SKIP); |
69
|
|
|
|
70
|
|
|
// Save current buffering level so we can backtrack in case of an error. |
71
|
|
|
// This is needed because the view itself might also add an unknown number of output buffering levels. |
72
|
17 |
|
$bufferLevel = ob_get_level(); |
73
|
17 |
|
ob_start(); |
74
|
|
|
|
75
|
|
|
try { |
76
|
17 |
|
include($uri); |
77
|
|
|
} catch (Exception $exception) { |
78
|
|
|
|
79
|
|
|
// Remove whatever levels were added up until now. |
80
|
|
|
while (ob_get_level() > $bufferLevel) { |
81
|
|
|
ob_end_clean(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
throw new FailedToLoadViewException( |
85
|
|
|
sprintf( |
86
|
|
|
_('Could not load the View URI "%1$s". Reason: "%2$s".'), |
87
|
|
|
$uri, |
88
|
|
|
$exception->getMessage() |
89
|
|
|
), |
90
|
|
|
$exception->getCode(), |
91
|
|
|
$exception |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
17 |
|
return ob_get_clean(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|