Passed
Push — main ( 2e52e3...625462 )
by Dimitri
03:32
created

AbstractAdapter::resetData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\View\Adapters;
13
14
use BlitzPHP\Autoloader\Locator;
15
use BlitzPHP\Exceptions\ViewException;
16
use BlitzPHP\Loader\Services;
17
use BlitzPHP\View\RendererInterface;
18
19
abstract class AbstractAdapter implements RendererInterface
20
{
21
    /**
22
     * Données mises à la disposition des vues.
23
     *
24
     * @var array
25
     */
26
    protected $data = [];
27
28
    /**
29
     * Les variables de rendu
30
     *
31
     * @var array
32
     */
33
    protected $renderVars = [];
34
35
    /**
36
     * Le répertoire de base dans lequel rechercher nos vues.
37
     *
38
     * @var string
39
     */
40
    protected $viewPath = '';
41
42
    /**
43
     * Instance de Locator lorsque nous devons tenter de trouver une vue qui n'est pas à l'emplacement standard.
44
     */
45
    protected ?Locator $locator = null;
46
47
    /**
48
     * Le nom de la mise en page utilisée, le cas échéant.
49
     * Défini par la méthode "extend" utilisée dans les vues.
50
     *
51
     * @var string|null
52
     */
53
    protected $layout;
54
55
    /**
56
     * Les statistiques sur nos performances ici
57
     *
58
     * @var array
59
     */
60
    protected $performanceData = [];
61
62
    /**
63
     * {@inheritDoc}
64
     * 
65
     * @param array $config Configuration actuelle de l'adapter
66
     * @param bool $debug Devrions-nous stocker des informations sur les performances ?
67
     */
68
    public function __construct(protected array $config, $viewPathLocator = null, protected bool $debug = BLITZ_DEBUG)
69
    {
70
        helper('assets');
71
        
72
        if (! empty($viewPathLocator)) {
73
            if (is_string($viewPathLocator)) {
74
                $this->viewPath = rtrim($viewPathLocator, '\\/ ') . DS;
75
            }
76
            else if ($viewPathLocator instanceof Locator) {
77
                $this->locator = $viewPathLocator;
78
            }
79
        }
80
        else {
81
            $this->locator = Services::locator();
82
        }
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function setData(array $data = [], ?string $context = null): self
89
    {
90
        if ($context) {
91
            $data = esc($data, $context);
92
        }
93
94
        $this->data = $data;
95
96
        return $this;
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102
    public function getData(): array
103
    {
104
        return $this->data;
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110
    public function addData(array $data = [], ?string $context = null): self
111
    {
112
        if ($context) {
113
            $data = esc($data, $context);
114
        }
115
116
        $this->data = array_merge($this->data, $data);
117
118
        return $this;
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function setVar(string $name, $value = null, ?string $context = null): self
125
    {
126
        if ($context) {
127
            $value = esc($value, $context);
128
        }
129
130
        $this->data[$name] = $value;
131
132
        return $this;
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138
    public function resetData(): self
139
    {
140
        $this->data = [];
141
142
        return $this;
143
    }
144
145
    /**
146
     * {@inheritDoc}
147
     */
148
    public function setLayout(?string $layout): self
149
    {
150
        $this->layout = $layout;
151
152
        return $this;
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158
    public function renderString(string $view, ?array $options = null, bool $saveData = false): string
159
    {
160
        return $this->render($view, $options, $saveData);
161
    }
162
163
    /**
164
     * {@inheritDoc}
165
     */
166
    public function getPerformanceData(): array
167
    {
168
        return $this->performanceData;
169
    }
170
171
    /**
172
     * Consigne les données de performances pour le rendu d'une vue.
173
     */
174
    protected function logPerformance(float $start, float $end, string $view)
175
    {
176
        if ($this->debug) {
177
            $this->performanceData[] = [
178
                'start' => $start,
179
                'end'   => $end,
180
                'view'  => $view,
181
            ];
182
        }
183
    }
184
185
    /**
186
     * Recupère ou modifie le titre de la page
187
     *
188
     * @return self|string
189
     */
190
    public function title(?string $title = null)
191
    {
192
        if (empty($title)) {
193
            return $this->getData()['title'] ?? '';
194
        }
195
196
        return $this->setVar('title', $title);
197
    }
198
199
    /**
200
     * Recupère ou modifie les elements de balises "meta"
201
     *
202
     * @return self|string
203
     */
204
    public function meta(string $key, ?string $value = null)
205
    {
206
        $meta = $this->getData()['meta'] ?? [];
207
208
        if (empty($value)) {
209
            return $meta[$key] ?? '';
210
        }
211
212
        $meta[$key] = esc($value);
213
214
        return $this->setVar('meta', $meta);
215
    }
216
217
    /**
218
     * Recupere le chemin absolue du fichier de vue a rendre
219
     */
220
    protected function getRenderedFile(?array $options, string $view, string $ext = 'php'): string
221
    {
222
        $options = (array) $options;
223
        
224
        $file = str_replace('/', DS, rtrim($options['viewPath'] ?? $this->viewPath, '/\\') . DS . ltrim($view, '/\\'));
225
226
        if (! is_file($file) && $this->locator instanceof Locator) {
227
            $file = $this->locator->locateFile($view, 'Views', empty($ext) ? 'php' : $ext);
0 ignored issues
show
Bug introduced by
The method locateFile() does not exist on BlitzPHP\Autoloader\Locator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

227
            /** @scrutinizer ignore-call */ 
228
            $file = $this->locator->locateFile($view, 'Views', empty($ext) ? 'php' : $ext);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
228
        }
229
230
        // locateFile renverra une chaîne vide si le fichier est introuvable.
231
        if (! is_file($file)) {
232
            throw ViewException::invalidFile($view);
233
        }
234
235
        return $file;
236
    }
237
238
    /**
239
     * Construit la sortie en fonction d'un nom de fichier et de tout données déjà définies.
240
     *
241
     * Options valides :
242
     * - cache Nombre de secondes à mettre en cache pour
243
     * - cache_name Nom à utiliser pour le cache
244
     *
245
     * @param string     $view     Nom de fichier de la source de la vue
246
     * @param array|null $options  Réservé à des utilisations tierces car
247
     *                             il peut être nécessaire de transmettre des
248
     *                             informations supplémentaires à d'autres moteurs de modèles.
249
     * @param bool|null  $saveData Si vrai, enregistre les données pour les appels suivants,
250
     *                             si faux, nettoie les données après affichage,
251
     *                             si null, utilise le paramètre de configuration.
252
     */
253
    abstract public function render(string $view, ?array $options = null, ?bool $saveData = null): string;
254
}
255