PhpEngine   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addPath() 0 6 1
A render() 0 14 3
1
<?php
2
/**
3
 * Veto.
4
 * PHP Microframework.
5
 *
6
 * @author Damien Walsh <[email protected]>
7
 * @copyright Damien Walsh 2013-2014
8
 * @version 0.1
9
 * @package veto
10
 */
11
namespace Veto\Templating\Engine;
12
13
use Veto\Templating\EngineInterface;
14
15
/**
16
 * PhpEngine
17
 * A basic PHP-based template engine for rendering old-style PHP viewscripts. Used internally by Veto for rendering
18
 * exception and debug pages.
19
 *
20
 * @since 0.1
21
 */
22
class PhpEngine implements EngineInterface
23
{
24
    /**
25
     * @var string[]
26
     */
27
    private $paths;
28
29
    /**
30
     * PhpEngine constructor.
31
     */
32
    public function __construct()
33
    {
34
        $this->paths = array();
35
    }
36
37
    /**
38
     * Add a template directory to look for template files inside.
39
     *
40
     * @param string $path The path to a template directory to load templates from.
41
     * @return boolean
42
     */
43
    public function addPath($path)
44
    {
45
        $this->paths[] = $path;
46
47
        return true;
48
    }
49
50
    /**
51
     * Render the given template name and return the result as a string.
52
     *
53
     * @param string $templateName The name of the template to render
54
     * @param array $parameters Any parameters to render the template with
55
     * @throws \Exception
56
     * @return string
57
     */
58
    public function render($templateName, array $parameters = array())
59
    {
60
        foreach ($this->paths as $path) {
61
            $fullPath = $path . DIRECTORY_SEPARATOR . $templateName;
62
            if (file_exists($fullPath)) {
63
                extract($parameters, EXTR_SKIP);
64
                ob_start();
65
                require $fullPath;
66
                return ob_get_clean();
67
            }
68
        }
69
70
        throw new \RuntimeException(sprintf('The template %s cannot be found.', $templateName));
71
    }
72
}
73