Passed
Push — master ( ddccf7...45aa82 )
by Alain
04:47
created

PHPEngine::canHandle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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