Test Failed
Push — main ( 3d651d...c1b661 )
by Rafael
05:29
created

ViewTrait::_()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 Alxarafe\Lib\Trans;
22
use Illuminate\Container\Container;
23
use Illuminate\Events\Dispatcher;
24
use Illuminate\Filesystem\Filesystem;
25
use Illuminate\View\Engines\CompilerEngine;
26
use Illuminate\View\Engines\EngineResolver;
27
use Illuminate\View\FileViewFinder;
28
use Illuminate\View\Factory;
29
use Illuminate\View\Compilers\BladeCompiler;
30
31
trait ViewTrait
32
{
33
    public static $messages = [];
34
    /**
35
     * Theme name. TODO: Has to be updated according to the configuration.
36
     *
37
     * @var string
38
     */
39
    public $theme;
40
    /**
41
     * Code lang for <html lang> tag
42
     *
43
     * @var string
44
     */
45
    public $lang = 'en';
46
    public $body_class;
47
    public $templatesPath;
48
    public $template;
49
    public $title;
50
    public $alerts;
51
52
    public static function _($message, array $parameters = [], $locale = null)
53
    {
54
        return Trans::_($message, $parameters, $locale);
55
    }
56
57
    public static function addMessage($message)
58
    {
59
        self::$messages[]['success'] = $message;
60
    }
61
62
    public static function addAdvice($message)
63
    {
64
        self::$messages[]['warning'] = $message;
65
    }
66
67
    public static function addError($message)
68
    {
69
        self::$messages[]['danger'] = $message;
70
    }
71
72
    public function __destruct()
73
    {
74
        if (!isset($this->template)) {
75
            return;
76
        }
77
78
        if (!isset($this->theme)) {
79
            $this->theme = 'alxarafe';
80
        }
81
82
        if (!isset($this->title)) {
83
            $this->title = 'Alxarafe';
84
        }
85
86
        $this->alerts = static::getMessages();
87
88
        $vars = ['me' => $this];
89
        $viewPaths = [
90
            constant('APP_PATH') . '/Templates',
91
            constant('APP_PATH') . '/Templates/theme/' . $this->theme,
92
            constant('APP_PATH') . '/Templates/common',
93
            constant('ALX_PATH') . '/Templates',
94
            constant('ALX_PATH') . '/Templates/theme/' . $this->theme,
95
            constant('ALX_PATH') . '/Templates/common',
96
        ];
97
98
        if (isset($this->templatesPath)) {
99
            array_unshift($viewPaths, $this->templatesPath);
100
        }
101
102
        $cachePaths = realpath(constant('BASE_PATH') . '/..') . '/tmp/blade';
103
        if (!is_dir($cachePaths) && !mkdir($cachePaths, 0777, true) && !is_dir($cachePaths)) {
104
            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...
105
        }
106
107
        $container = new Container();
108
109
        $container->singleton('files', function () {
110
            return new Filesystem();
111
        });
112
113
        $container->singleton('view.finder', function ($app) use ($viewPaths) {
114
            return new FileViewFinder($app['files'], $viewPaths);
115
        });
116
117
        $container->singleton('blade.compiler', function ($app) use ($cachePaths) {
118
            return new BladeCompiler($app['files'], $cachePaths);
119
        });
120
121
        $container->singleton('view.engine.resolver', function ($app) {
122
            $resolver = new EngineResolver();
123
124
            // Register Blade engine
125
            $resolver->register('blade', function () use ($app) {
126
                return new CompilerEngine($app['blade.compiler']);
127
            });
128
129
            return $resolver;
130
        });
131
132
        $container->singleton('view', function ($app) {
133
            $resolver = $app['view.engine.resolver'];
134
            $finder = $app['view.finder'];
135
            $dispatcher = new Dispatcher($app);
136
137
            return new Factory($resolver, $finder, $dispatcher);
138
        });
139
140
        $viewFactory = $container['view'];
141
142
        echo $viewFactory->make($this->template, $vars)->render();
143
    }
144
145
    public static function getMessages()
146
    {
147
        $alerts = [];
148
        foreach (self::$messages as $message) {
149
            foreach ($message as $type => $text) {
150
                $alerts[] = [
151
                    'type' => $type,
152
                    'text' => $text
153
                ];
154
            }
155
        }
156
        self::$messages = [];
157
        return $alerts;
158
    }
159
160
    public function getTemplatesPath(): string
161
    {
162
        return $this->templatesPath;
163
    }
164
165
    public function setTemplatesPath(string $path)
166
    {
167
        $this->templatesPath = $path;
168
    }
169
}
170