Issues (14)

src/Template.php (3 issues)

1
<?php
2
3
/**
4
 * Platine Template
5
 *
6
 * Platine Template is a template engine that has taken a lot of inspiration from Django.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Template
11
 * Copyright (c) 2014 Guz Alexander, http://guzalexander.com
12
 * Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com
13
 * Copyright (c) 2006 Mateo Murphy
14
 *
15
 * Permission is hereby granted, free of charge, to any person obtaining a copy
16
 * of this software and associated documentation files (the "Software"), to deal
17
 * in the Software without restriction, including without limitation the rights
18
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
 * copies of the Software, and to permit persons to whom the Software is
20
 * furnished to do so, subject to the following conditions:
21
 *
22
 * The above copyright notice and this permission notice shall be included in all
23
 * copies or substantial portions of the Software.
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
 * SOFTWARE.
32
 */
33
34
/**
35
 *  @file Template.php
36
 *
37
 *  The Template class
38
 *
39
 *  @package    Platine\Template
40
 *  @author Platine Developers Team
41
 *  @copyright  Copyright (c) 2020
42
 *  @license    http://opensource.org/licenses/MIT  MIT License
43
 *  @link   https://www.platine-php.com
44
 *  @version 1.0.0
45
 *  @filesource
46
 */
47
48
declare(strict_types=1);
49
50
namespace Platine\Template;
51
52
use Platine\Template\Cache\AbstractCache;
53
use Platine\Template\Cache\NullCache;
54
use Platine\Template\Loader\LoaderInterface;
55
use Platine\Template\Loader\StringLoader;
56
use Platine\Template\Parser\Context;
57
use Platine\Template\Parser\Parser;
58
use Platine\Template\Parser\AbstractTag;
59
60
/**
61
 * @class Template
62
 * @package Platine\Template
63
 */
64
class Template
65
{
66
    /**
67
     * The configuration instance
68
     * @var Configuration
69
     */
70
    protected Configuration $config;
71
72
    /**
73
     * The loader instance
74
     * @var LoaderInterface
75
     */
76
    protected LoaderInterface $loader;
77
78
    /**
79
     * The Parser instance
80
     * @var Parser
81
     */
82
    protected Parser $parser;
83
84
    /**
85
     * Tick callback
86
     * @var callable|null
87
     */
88
    protected $tickCallback = null;
89
90
    /**
91
     * The cache instance
92
     * @var AbstractCache
93
     */
94
    protected AbstractCache $cache;
95
96
    /**
97
     * List of filters
98
     * @var array<int, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<int, class-string>.
Loading history...
99
     */
100
    protected array $filters = [];
101
102
    /**
103
     * List of tags
104
     * @var array<string, string|AbstractTag>
105
     */
106
    protected array $tags = [];
107
108
    /**
109
     * Create new instance
110
     * @param Configuration|null $config
111
     * @param LoaderInterface|null $loader
112
     * @param AbstractCache|null $cache
113
     */
114
    public function __construct(
115
        ?Configuration $config = null,
116
        ?LoaderInterface $loader = null,
117
        ?AbstractCache $cache = null
118
    ) {
119
        $this->config = $config ?? new Configuration([]);
120
        $this->loader = $loader ?? new StringLoader([]);
121
        $this->cache = $cache ?? new NullCache($config);
122
        $this->parser = new Parser($this);
123
124
        //Add custom tags
125
        $this->tags = $this->config->get('tags');
126
127
        //Add custom filters
128
        $this->filters = $this->config->get('filters');
129
    }
130
131
    /**
132
     * Return the configuration instance
133
     * @return Configuration
134
     */
135
    public function getConfig(): Configuration
136
    {
137
        return $this->config;
138
    }
139
140
    /**
141
     * Return the loader instance
142
     * @return LoaderInterface
143
     */
144
    public function getLoader(): LoaderInterface
145
    {
146
        return $this->loader;
147
    }
148
149
    /**
150
     * Return the cache instance
151
     * @return AbstractCache
152
     */
153
    public function getCache(): AbstractCache
154
    {
155
        return $this->cache;
156
    }
157
158
    /**
159
     * Return the list of tags
160
     * @return array<string, string|AbstractTag>
161
     */
162
    public function getTags(): array
163
    {
164
        return $this->tags;
165
    }
166
167
    /**
168
     * Return the list of filters
169
     * @return array<int, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<int, class-string>.
Loading history...
170
     */
171
    public function getFilters(): array
172
    {
173
        return $this->filters;
174
    }
175
176
    /**
177
     * Register Tag
178
     * @param string $name
179
     * @param string|AbstractTag $class
180
     * @return $this
181
     */
182
    public function addTag(string $name, string|AbstractTag $class): self
183
    {
184
        $this->tags[$name] = $class;
185
186
        return $this;
187
    }
188
189
    /**
190
     * Register the filter
191
     * @param class-string $filter
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
192
     * @return $this
193
     */
194
    public function addFilter(string $filter): self
195
    {
196
        $this->filters[] = $filter;
197
198
        return $this;
199
    }
200
201
    /**
202
     * Set tick callback
203
     * @param callable|null $tickCallback
204
     * @return $this
205
     */
206
    public function setTickCallback(?callable $tickCallback): self
207
    {
208
        $this->tickCallback = $tickCallback;
209
210
        return $this;
211
    }
212
213
    /**
214
     * Render the template
215
     * @param string $name the name of template (filename, etc)
216
     * @param array<string, mixed> $assigns
217
     * @param array<string, mixed> $registers
218
     * @return string the final output
219
     */
220
    public function render(string $name, array $assigns = [], array $registers = []): string
221
    {
222
        $context = new Context($assigns, $registers);
223
224
        if ($this->tickCallback !== null) {
225
            $context->setTickCallback($this->tickCallback);
226
        }
227
228
        foreach ($this->filters as $filter) {
229
            $context->addFilter($filter);
230
        }
231
232
        return $this->parser->render($name, $context);
233
    }
234
235
    /**
236
     * Render the template using the string
237
     * @param string $content the template content
238
     * @param array<string, mixed> $assigns
239
     * @param array<string, mixed> $registers
240
     * @return string the final output
241
     */
242
    public function renderString(string $content, array $assigns = [], array $registers = []): string
243
    {
244
        $context = new Context($assigns, $registers);
245
246
        if ($this->tickCallback !== null) {
247
            $context->setTickCallback($this->tickCallback);
248
        }
249
250
        foreach ($this->filters as $filter) {
251
            $context->addFilter($filter);
252
        }
253
254
        return $this->parser->renderString($content, $context);
255
    }
256
}
257