blitz-php /
framework
| 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\Router\Dispatcher; |
||||
| 15 | use BlitzPHP\View\View; |
||||
| 16 | use Psr\Http\Message\ResponseInterface; |
||||
| 17 | use ReflectionClass; |
||||
| 18 | use ReflectionException; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * Le contrôleur de base pour les applications MVC |
||||
| 22 | */ |
||||
| 23 | class ApplicationController extends BaseController |
||||
| 24 | { |
||||
| 25 | /** |
||||
| 26 | * Données partagées entre toutes les vue chargées à partir d'un controleur |
||||
| 27 | */ |
||||
| 28 | protected array $viewDatas = []; |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * Layout a utiliser |
||||
| 32 | */ |
||||
| 33 | protected string $layout = ''; |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * Charge une vue |
||||
| 37 | * |
||||
| 38 | * @throws ReflectionException |
||||
| 39 | */ |
||||
| 40 | protected function view(string $view, ?array $data = [], ?array $options = []): View |
||||
| 41 | { |
||||
| 42 | $path = ''; |
||||
| 43 | $data = (array) $data; |
||||
| 44 | $options = (array) $options; |
||||
| 45 | |||||
| 46 | // N'est-il pas namespaced ? on cherche le dossier en fonction du controleur |
||||
| 47 | if (! str_contains($view, '\\')) { |
||||
| 48 | $reflection = new ReflectionClass(static::class); |
||||
| 49 | ['dirname' => $dirname, 'filename' => $filename] = pathinfo($reflection->getFileName()); |
||||
| 50 | $dirname = str_ireplace('Controllers', 'Views', $dirname); |
||||
| 51 | $filename = strtolower(str_ireplace('Controller', '', $filename)); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 52 | |||||
| 53 | $parts = explode('Views', $dirname); |
||||
|
0 ignored issues
–
show
It seems like
$dirname can also be of type array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 54 | $base = array_shift($parts); |
||||
| 55 | $parts = array_map('strtolower', $parts); |
||||
| 56 | $parts = [$base, ...$parts]; |
||||
| 57 | |||||
| 58 | $dirname = implode('Views', $parts); |
||||
| 59 | $path = implode(DS, [$dirname, $filename]) . DS; |
||||
| 60 | |||||
| 61 | if (! is_dir($path)) { |
||||
| 62 | $path = implode(DS, [$dirname]) . DS; |
||||
| 63 | } |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | /** @var View */ |
||||
| 67 | $viewer = service('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); |
||||
|
0 ignored issues
–
show
Are you sure
$controllerName of type array|string can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 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)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 131 | $data = [$key => $value]; |
||||
| 132 | } |
||||
| 133 | |||||
| 134 | $this->viewDatas = array_merge($this->viewDatas, $data); |
||||
| 135 | |||||
| 136 | return $this; |
||||
| 137 | } |
||||
| 138 | } |
||||
| 139 |