1<?php
2/*
3 * This file is part of the Text_Template package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11/**
12 * A simple template engine.
13 *
14 * @since Class available since Release 1.0.0
15 */
16class Text_Template
17{
18    /**
19     * @var string
20     */
21    protected $template = '';
22
23    /**
24     * @var string
25     */
26    protected $openDelimiter = '{';
27
28    /**
29     * @var string
30     */
31    protected $closeDelimiter = '}';
32
33    /**
34     * @var array
35     */
36    protected $values = array();
37
38    /**
39     * Constructor.
40     *
41     * @param  string                   $file
42     * @throws InvalidArgumentException
43     */
44    public function __construct($file = '', $openDelimiter = '{', $closeDelimiter = '}')
45    {
46        $this->setFile($file);
47        $this->openDelimiter  = $openDelimiter;
48        $this->closeDelimiter = $closeDelimiter;
49    }
50
51    /**
52     * Sets the template file.
53     *
54     * @param  string                   $file
55     * @throws InvalidArgumentException
56     */
57    public function setFile($file)
58    {
59        $distFile = $file . '.dist';
60
61        if (file_exists($file)) {
62            $this->template = file_get_contents($file);
63        }
64
65        else if (file_exists($distFile)) {
66            $this->template = file_get_contents($distFile);
67        }
68
69        else {
70            throw new InvalidArgumentException(
71              'Template file could not be loaded.'
72            );
73        }
74    }
75
76    /**
77     * Sets one or more template variables.
78     *
79     * @param array $values
80     * @param bool  $merge
81     */
82    public function setVar(array $values, $merge = TRUE)
83    {
84        if (!$merge || empty($this->values)) {
85            $this->values = $values;
86        } else {
87            $this->values = array_merge($this->values, $values);
88        }
89    }
90
91    /**
92     * Renders the template and returns the result.
93     *
94     * @return string
95     */
96    public function render()
97    {
98        $keys = array();
99
100        foreach ($this->values as $key => $value) {
101            $keys[] = $this->openDelimiter . $key . $this->closeDelimiter;
102        }
103
104        return str_replace($keys, $this->values, $this->template);
105    }
106
107    /**
108     * Renders the template and writes the result to a file.
109     *
110     * @param string $target
111     */
112    public function renderTo($target)
113    {
114        $fp = @fopen($target, 'wt');
115
116        if ($fp) {
117            fwrite($fp, $this->render());
118            fclose($fp);
119        } else {
120            $error = error_get_last();
121
122            throw new RuntimeException(
123              sprintf(
124                'Could not write to %s: %s',
125                $target,
126                substr(
127                  $error['message'],
128                  strpos($error['message'], ':') + 2
129                )
130              )
131            );
132        }
133    }
134}
135
136