Passed
Push — master ( 87ba60...d80a49 )
by Alain
02:43
created

PHPEngine::getRenderCallback()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 9
cts 9
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 23
nc 2
nop 2
crap 4
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\FailedToLoadView;
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 30
    public function canHandle($criterion)
41
    {
42 30
        return URIHelper::hasExtension($criterion, static::PHP_EXTENSION)
43 30
               && is_readable($criterion);
44
    }
45
46
    /**
47
     * Get the rendering callback for 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 callable Rendering callback.
55
     * @throws FailedToLoadView If the View URI is not accessible or readable.
56
     * @throws FailedToLoadView If the View URI could not be loaded.
57
     */
58 28
    public function getRenderCallback($uri, array $context = [])
59
    {
60 28
        if ( ! is_readable($uri)) {
61
            throw new FailedToLoadView(
62
                sprintf(
63
                    _('The View URI "%1$s" is not accessible or readable.'),
64
                    $uri
65
                )
66
            );
67
        }
68
69 28
        $closure = function () use ($uri, $context) {
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 28
            $bufferLevel = ob_get_level();
74 28
            ob_start();
75
76
            try {
77 28
                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 FailedToLoadView(
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 28
            return ob_get_clean();
97 28
        };
98
99 28
        return $closure;
100
    }
101
}
102