FileLoader::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 0
loc 16
rs 9.9666
c 0
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 FileLoader.php
36
 *
37
 *  The file system Template loader class
38
 *
39
 *  @package    Platine\Template\Loader
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\Loader;
51
52
use Platine\Template\Configuration;
53
use Platine\Template\Exception\NotFoundException;
54
use Platine\Template\Exception\ParseException;
55
use Platine\Template\Parser\Lexer;
56
use Platine\Template\Util\Helper;
57
58
/**
59
 * @class FileLoader
60
 * @package Platine\Template\Loader
61
 */
62
class FileLoader implements LoaderInterface
63
{
64
    /**
65
     * The configuration instance
66
     * @var Configuration
67
     */
68
    protected Configuration $config;
69
70
    /**
71
     * The root path to store file
72
     * @var string
73
     */
74
    protected string $path;
75
76
    /**
77
     * Create new instance
78
     * @param Configuration|null $config
79
     */
80
    public function __construct(?Configuration $config = null)
81
    {
82
        $this->config = $config ?? new Configuration([]);
83
84
        $dir = $this->config->get('template_dir');
85
        $path = Helper::normalizePath($dir);
86
        $realPath = realpath($path);
87
88
        if ($realPath === false || !is_writable($realPath)) {
89
            throw new NotFoundException(sprintf(
90
                'The template directory [%s] does not exist or is not writable',
91
                $path
92
            ));
93
        }
94
95
        $this->path = $realPath . DIRECTORY_SEPARATOR;
96
    }
97
98
    /**
99
    * {@inheritdoc}
100
    */
101
    public function read(string $name): string
102
    {
103
        $file = $this->getFilePath($name);
104
105
        return (string) file_get_contents($file);
106
    }
107
108
109
    /**
110
     *
111
     * @param string $file
112
     * @return string
113
     * @throws ParseException
114
     * @throws NotFoundException
115
     */
116
    protected function getFilePath(string $file): string
117
    {
118
        $pattern = '/^[^.\/][a-zA-Z0-9_\/-]+$/';
119
120
        $lexer = new Lexer($pattern);
121
        if ($lexer->match($file) === false) {
122
            throw new ParseException(sprintf(
123
                'Invalid template filename [%s]',
124
                $file
125
            ));
126
        }
127
128
        $extension = $this->config->get('file_extension');
129
        if (!empty($extension)) {
130
            $file .= '.' . $extension;
131
        }
132
133
        $realPath = $this->path . $file;
134
135
        if (is_file($realPath) === false) {
136
            throw new NotFoundException(sprintf(
137
                'Template file [%s] does not exist',
138
                $realPath
139
            ));
140
        }
141
142
        return $realPath;
143
    }
144
}
145