Completed
Push — master ( 7cba80...45c651 )
by Damien
02:40
created

PhpEngine   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

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
    public function __construct()
30
    {
31
        $this->paths = array();
32
    }
33
34
    /**
35
     * Add a template directory to look for template files inside.
36
     *
37
     * @param string $path The path to a template directory to load templates from.
38
     * @return boolean
39
     */
40
    public function addPath($path)
41
    {
42
        $this->paths[] = $path;
43
44
        return true;
45
    }
46
47
    /**
48
     * Render the given template name and return the result as a string.
49
     *
50
     * @param string $templateName The name of the template to render
51
     * @param array $parameters Any parameters to render the template with
52
     * @throws \Exception
53
     * @return string
54
     */
55
    public function render($templateName, array $parameters = array())
56
    {
57
        foreach ($this->paths as $path) {
58
            $fullPath = $path . DIRECTORY_SEPARATOR . $templateName;
59
            if (file_exists($fullPath)) {
60
                extract($parameters, EXTR_SKIP);
61
                ob_start();
62
                require $fullPath;
63
                return ob_get_clean();
64
            }
65
        }
66
67
        throw new \RuntimeException(sprintf('The template %s cannot be found.', $templateName));
68
    }
69
}
70