Passed
Push — main ( 7af4bb...131f20 )
by Rafael
58:13
created

ViewTrait::setTemplatesPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/* Copyright (C) 2024      Rafael San José      <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Alxarafe\Base\Controller\Trait;
20
21
use Jenssegers\Blade\Blade;
22
23
trait ViewTrait
24
{
25
    /**
26
     * Theme name. TODO: Has to be updated according to the configuration.
27
     *
28
     * @var string
29
     */
30
    public $theme;
31
32
    /**
33
     * Code lang for <html lang> tag
34
     *
35
     * @var string
36
     */
37
    public $lang = 'en';
38
39
    public $body_class;
40
41
    public $templatesPath;
42
    public $template;
43
    public $title;
44
45
    public function __destruct()
46
    {
47
        if (!isset($this->template)) {
48
            return;
49
        }
50
51
        if (!isset($this->theme)) {
52
            $this->theme = 'alixar';
53
        }
54
55
        if (!isset($this->title)) {
56
            $this->title = 'Alixar';
57
        }
58
59
        $vars = ['me' => $this];
60
        $viewPaths = [
61
            $this->templatesPath,
62
            BASE_PATH . '/Templates',
63
            BASE_PATH . '/Templates/theme/' . $this->theme,
64
            BASE_PATH . '/Templates/common',
65
        ];
66
        $cachePaths = realpath(BASE_PATH . '/..') . '/tmp/blade';
67
        if (!is_dir($cachePaths) && !mkdir($cachePaths, 0777, true) && !is_dir($cachePaths)) {
68
            die('Could not create cache directory for templates: ' . $cachePaths);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
69
        }
70
        $blade = new Blade($viewPaths, $cachePaths);
71
        echo $blade->render($this->template, $vars);
72
    }
73
74
    public function getTemplatesPath(): string
75
    {
76
        return $this->templatesPath;
77
    }
78
79
    public function setTemplatesPath(string $path)
80
    {
81
        $this->templatesPath = $path;
82
    }
83
}
84