Passed
Push — main ( 4cd58e...3399b6 )
by Dimitri
03:02
created

ApplicationController::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\Loader\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
     * @var array Données partagées entre toutes les vue chargées à partir d'un controleur
28
     */
29
    protected $viewDatas = [];
30
31
    /**
32
     * @var string Layout a utiliser
33
     */
34
    protected $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
45
        // N'est-il pas namespaced ? on cherche le dossier en fonction du controleur
46
        if (strpos($view, '\\') === false) {
47
            $reflection = new ReflectionClass(static::class);
48
            $path       = str_replace([CONTROLLER_PATH, 'Controller', '.php'], '', $reflection->getFileName());
49
            $path       = strtolower($path) . '/';
50
        }
51
52
        $viewer = Services::viewer();
53
54
        $viewer->setData($data)->setOptions($options);
55
56
        if (! empty($this->layout) && is_string($this->layout)) {
57
            $viewer->setLayout($this->layout);
58
        }
59
60
        if (! empty($this->viewDatas) && is_array($this->viewDatas)) {
61
            $viewer->addData($this->viewDatas);
62
        }
63
64
        if (! is_string($controllerName = Dispatcher::getController(false))) {
65
            $controllerName = static::class;
66
        }
67
68
        return $viewer->display($path . $view)->setVar('title', str_ireplace('Controller', '', $controllerName) . ' - ' . Dispatcher::getMethod());
0 ignored issues
show
Bug introduced by
Are you sure str_ireplace('Controller', '', $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 ignore-type  annotation

68
        return $viewer->display($path . $view)->setVar('title', /** @scrutinizer ignore-type */ str_ireplace('Controller', '', $controllerName) . ' - ' . Dispatcher::getMethod());
Loading history...
69
    }
70
71
    /**
72
     * Charge et rend directement une vue
73
     */
74
    final protected function render(?string $view = null, ?array $data = [], ?array $options = []): ResponseInterface
75
    {
76
        if (empty($view)) {
77
            $view = Dispatcher::getMethod();
78
        }
79
80
        $view = $this->view($view, $data, $options)->get();
81
82
        return $this->response->withBody(to_stream($view));
83
    }
84
85
    /**
86
     * Defini des donnees à distribuer à toutes les vues
87
     *
88
     * @param mixed $value
89
     */
90
    final protected function addData(string|array $key, $value = null): self
91
    {
92
        $data = $key;
93
94
        if (is_string($key)) {
0 ignored issues
show
introduced by
The condition is_string($key) is always false.
Loading history...
95
            $data = [$key => $value];
96
        }
97
98
        $this->viewDatas = array_merge($this->viewDatas, $data);
99
100
        return $this;
101
    }
102
}
103