Passed
Push — main ( bcc78b...57b38b )
by
unknown
08:40 queued 04:18
created

ApplicationController::render()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 8
nop 3
dl 0
loc 22
ccs 0
cts 9
cp 0
crap 30
rs 9.5555
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
     * @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
        $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));
0 ignored issues
show
Bug introduced by
It seems like str_ireplace('Controller', '', $filename) can also be of type array; however, parameter $string of strtolower() 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 ignore-type  annotation

52
            $filename                                        = strtolower(/** @scrutinizer ignore-type */ str_ireplace('Controller', '', $filename));
Loading history...
53
            $path                                            = implode(DS, [$dirname, $filename]) . DS;
54
        }
55
56
        $viewer = Services::viewer();
57
58
        $viewer->setData($data)->setOptions($options);
59
60
        if (! empty($this->layout) && is_string($this->layout)) {
61
            $viewer->setLayout($this->layout);
62
        }
63
64
        if (! empty($this->viewDatas) && is_array($this->viewDatas)) {
65
            $viewer->addData($this->viewDatas);
66
        }
67
68
        if (empty($data['title'])) {
69
            if (! is_string($controllerName = Dispatcher::getController(false))) {
70
                $controllerName = static::class;
71
            }
72
            $controllerName = str_ireplace(['App\Controllers', 'Controller'], '', $controllerName);
73
74
            $dbt  = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
75
            $func = $dbt[1]['function'] ?? Dispatcher::getMethod();
76
77
            $viewer->setVar('title', $controllerName . ' - ' . $func);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

77
            $viewer->setVar('title', /** @scrutinizer ignore-type */ $controllerName . ' - ' . $func);
Loading history...
78
        }
79
80
        return $viewer->display($path . $view);
81
    }
82
83
    /**
84
     * Charge et rend directement une vue
85
     */
86
    final protected function render(array|string $view = '', ?array $data = [], ?array $options = []): ResponseInterface
87
    {
88
        if (is_array($view)) {
89
            $data    = $view;
90
            $options = $data;
91
92
            $dbt  = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
93
            $view = $dbt[1]['function'] ?? '';
94
        }
95
96
        if (empty($view) && empty($data)) {
97
            $dbt  = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
98
            $view = $dbt[1]['function'] ?? '';
99
        }
100
101
        if (empty($view)) {
102
            $view = Dispatcher::getMethod();
103
        }
104
105
        $view = $this->view($view, $data, $options)->get();
106
107
        return $this->response->withBody(to_stream($view));
108
    }
109
110
    /**
111
     * Defini des donnees à distribuer à toutes les vues
112
     *
113
     * @param mixed $value
114
     */
115
    final protected function addData(array|string $key, $value = null): self
116
    {
117
        $data = $key;
118
119
        if (is_string($key)) {
0 ignored issues
show
introduced by
The condition is_string($key) is always false.
Loading history...
120
            $data = [$key => $value];
121
        }
122
123
        $this->viewDatas = array_merge($this->viewDatas, $data);
124
125
        return $this;
126
    }
127
}
128