Passed
Push — develop ( 067c9c...bf1d25 )
by nguereza
04:45
created

Configuration::getTemplateDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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 Configuration.php
36
 *
37
 *  The Template Configuration 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   http://www.iacademy.cf
44
 *  @version 1.0.0
45
 *  @filesource
46
 */
47
48
declare(strict_types=1);
49
50
namespace Platine\Template;
51
52
use InvalidArgumentException;
53
54
/**
55
 * Class Configuration
56
 * @package Platine\Template
57
 */
58
class Configuration
59
{
60
61
    /**
62
     * The configuration raw data
63
     * @var array<string, mixed>
64
     */
65
    protected array $config = [];
66
67
    /**
68
     * The cache file directory to use
69
     * @var string
70
     */
71
    protected string $cacheDir = '.';
72
73
    /**
74
     * The cache prefix to use
75
     * @var string
76
     */
77
    protected string $cachePrefix = '__platine_template';
78
79
    /**
80
     * The cache expiration in second
81
     * @var int
82
     */
83
    protected int $cacheExpire = 3600;
84
85
    /**
86
     * The template files directory to use
87
     * @var string
88
     */
89
    protected string $templateDir = '.';
90
91
    /**
92
     * The template file extension to use
93
     * @var string
94
     */
95
    protected string $fileExtension = 'tpl';
96
97
    /**
98
     * Allow template names with extension in include and extends.
99
     * @var bool
100
     */
101
    protected bool $includeWithExtension = false;
102
103
    /**
104
     * Automatically escape any variables
105
     * unless told otherwise by a "raw" filter
106
     * @var bool
107
     */
108
    protected bool $autoEscape = false;
109
110
    /**
111
     * The custom tags list
112
     * @var array<string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
113
     */
114
    protected array $tags = [];
115
116
    /**
117
     * The custom filters list
118
     * @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...
119
     */
120
    protected array $filters = [];
121
122
    /**
123
     * Create new instance
124
     * @param array<string, mixed> $config
125
     */
126
    public function __construct(array $config = [])
127
    {
128
        $this->load($config);
129
    }
130
131
    /**
132
     * Return the value of the given configuration
133
     * @param string $name
134
     * @return mixed
135
     */
136
    public function get(string $name)
137
    {
138
        if (!array_key_exists($name, $this->config)) {
139
            throw new InvalidArgumentException(sprintf(
140
                'Invalid configuration [%s]',
141
                $name
142
            ));
143
        }
144
145
        return $this->config[$name];
146
    }
147
148
    /**
149
     * Return the cache directory
150
     * @return string
151
     */
152
    public function getCacheDir(): string
153
    {
154
        return $this->cacheDir;
155
    }
156
157
    /**
158
     * Return the template file directory
159
     * @return string
160
     */
161
    public function getTemplateDir(): string
162
    {
163
        return $this->templateDir;
164
    }
165
166
    /**
167
     * Wether use extension in include or extends filename
168
     * @return bool
169
     */
170
    public function isIncludeWithExtension(): bool
171
    {
172
        return $this->includeWithExtension;
173
    }
174
175
    /**
176
     * Return the template file extension
177
     * @return string
178
     */
179
    public function getFileExtension(): string
180
    {
181
        return $this->fileExtension;
182
    }
183
184
    /**
185
     * Return the cache prefix
186
     * @return string
187
     */
188
    public function getCachePrefix(): string
189
    {
190
        return $this->cachePrefix;
191
    }
192
193
    /**
194
     * Return the cache expiration in second
195
     * @return int
196
     */
197
    public function getCacheExpire(): int
198
    {
199
        return $this->cacheExpire;
200
    }
201
202
    /**
203
     * Whether is auto escaped
204
     * @return bool
205
     */
206
    public function isAutoEscape(): bool
207
    {
208
        return $this->autoEscape;
209
    }
210
211
    /**
212
     * Return the tag list
213
     * @return array<string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
214
     */
215
    public function getTags(): array
216
    {
217
        return $this->tags;
218
    }
219
220
    /**
221
     * Return the filter list
222
     * @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...
223
     */
224
    public function getFilters(): array
225
    {
226
        return $this->filters;
227
    }
228
229
230
    /**
231
     * Load the configuration
232
     * @param array<string, mixed> $config
233
     * @return void
234
     */
235
    public function load(array $config): void
236
    {
237
        $this->config = $config;
238
239
        foreach ($config as $name => $value) {
240
            $key = str_replace('_', '', lcfirst(ucwords($name, '_')));
241
            if (property_exists($this, $key)) {
242
                if (in_array($key, ['tags', 'filters']) && is_array($value)) {
243
                    $method = 'set' . ucfirst($key);
244
                    $this->{$method}($value);
245
                } else {
246
                    $this->{$key} = $value;
247
                }
248
            }
249
        }
250
    }
251
252
    /**
253
     * Set the tags configuration
254
     * @param array<string, class-string> $tags
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
255
     * @return void
256
     */
257
    protected function setTags(array $tags): void
258
    {
259
        $this->tags = $tags;
260
    }
261
262
    /**
263
     * Set the filters configuration
264
     * @param array<int, class-string> $filters
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...
265
     * @return void
266
     */
267
    protected function setFilters(array $filters): void
268
    {
269
        $this->filters = $filters;
270
    }
271
}
272