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\Controllers; |
13
|
|
|
|
14
|
|
|
use BlitzPHP\Container\Services; |
15
|
|
|
use BlitzPHP\Router\Dispatcher; |
16
|
|
|
use BlitzPHP\View\View; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use ReflectionClass; |
19
|
|
|
use ReflectionException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Le contrôleur de base pour les applications MVC |
23
|
|
|
*/ |
24
|
|
|
class ApplicationController extends BaseController |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Données partagées entre toutes les vue chargées à partir d'un controleur |
28
|
|
|
*/ |
29
|
|
|
protected array $viewDatas = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Layout a utiliser |
33
|
|
|
*/ |
34
|
|
|
protected string $layout = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Charge une vue |
38
|
|
|
* |
39
|
|
|
* @throws ReflectionException |
40
|
|
|
*/ |
41
|
|
|
protected function view(string $view, ?array $data = [], ?array $options = []): View |
42
|
|
|
{ |
43
|
|
|
$path = ''; |
44
|
|
|
$data = (array) $data; |
45
|
|
|
$options = (array) $options; |
46
|
|
|
|
47
|
|
|
// N'est-il pas namespaced ? on cherche le dossier en fonction du controleur |
48
|
|
|
if (! str_contains($view, '\\')) { |
49
|
|
|
$reflection = new ReflectionClass(static::class); |
50
|
|
|
['dirname' => $dirname, 'filename' => $filename] = pathinfo($reflection->getFileName()); |
51
|
|
|
$dirname = str_ireplace('Controllers', 'Views', $dirname); |
52
|
|
|
$filename = strtolower(str_ireplace('Controller', '', $filename)); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$parts = explode('Views', $dirname); |
|
|
|
|
55
|
|
|
$base = array_shift($parts); |
56
|
|
|
$parts = array_map('strtolower', $parts); |
57
|
|
|
$parts = [$base, ...$parts]; |
58
|
|
|
|
59
|
|
|
$dirname = implode('Views', $parts); |
60
|
|
|
$path = implode(DS, [$dirname, $filename]) . DS; |
61
|
|
|
|
62
|
|
|
if (! is_dir($path)) { |
63
|
|
|
$path = implode(DS, [$dirname]) . DS; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$viewer = Services::viewer(); |
68
|
|
|
|
69
|
|
|
$viewer->setData($data)->options($options); |
70
|
|
|
|
71
|
|
|
if ($this->layout !== '') { |
72
|
|
|
$viewer->layout($this->layout); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($this->viewDatas !== [] && is_array($this->viewDatas)) { |
76
|
|
|
$viewer->addData($this->viewDatas); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (empty($data['title'])) { |
80
|
|
|
if (! is_string($controllerName = Dispatcher::getController(false))) { |
81
|
|
|
$controllerName = static::class; |
82
|
|
|
} |
83
|
|
|
$controllerName = str_ireplace(['App\Controllers', 'Controller'], '', $controllerName); |
84
|
|
|
|
85
|
|
|
$dbt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
86
|
|
|
$func = $dbt[1]['function'] ?? Dispatcher::getMethod(); |
87
|
|
|
|
88
|
|
|
$viewer->setVar('title', $controllerName . ' - ' . $func); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $viewer->display($path . $view); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Charge et rend directement une vue |
96
|
|
|
*/ |
97
|
|
|
final protected function render(array|string $view = '', ?array $data = [], ?array $options = []): ResponseInterface |
98
|
|
|
{ |
99
|
|
|
if (is_array($view)) { |
100
|
|
|
$data = $view; |
101
|
|
|
$options = $data; |
102
|
|
|
|
103
|
|
|
$dbt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
104
|
|
|
$view = $dbt[1]['function'] ?? ''; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (($view === '' || $view === '0') && ($data === null || $data === [])) { |
108
|
|
|
$dbt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
109
|
|
|
$view = $dbt[1]['function'] ?? ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($view === '' || $view === '0') { |
113
|
|
|
$view = Dispatcher::getMethod(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$view = $this->view($view, $data, $options)->get(); |
117
|
|
|
|
118
|
|
|
return $this->response->withBody(to_stream($view)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Defini des donnees à distribuer à toutes les vues |
123
|
|
|
* |
124
|
|
|
* @param mixed $value |
125
|
|
|
*/ |
126
|
|
|
final protected function addData(array|string $key, $value = null): self |
127
|
|
|
{ |
128
|
|
|
$data = $key; |
129
|
|
|
|
130
|
|
|
if (is_string($key)) { |
|
|
|
|
131
|
|
|
$data = [$key => $value]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->viewDatas = array_merge($this->viewDatas, $data); |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|