Passed
Push — master ( 7c83a0...cd350b )
by Thierry
02:08
created

ViewManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Ui\View;
4
5
use Closure;
6
use Jaxon\Container\Container;
7
use Jaxon\Contracts\View;
8
use Jaxon\Utils\Config\Config;
9
10
use function array_filter;
11
use function is_array;
12
use function rtrim;
13
14
class ViewManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ViewManager
Loading history...
15
{
16
    /**
17
     * @var Container
18
     */
19
    protected $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
20
21
    /**
22
     * The default namespace
23
     *
24
     * @var string
25
     */
26
    protected $sDefaultNamespace = '';
27
28
    /**
29
     * The view namespaces
30
     *
31
     * @var array
32
     */
33
    protected $aNamespaces = [];
34
35
    /**
36
     * The class constructor
37
     *
38
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
39
     */
40
    public function __construct(Container $di)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
41
    {
42
        $this->di = $di;
43
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
44
45
    /**
46
     * Get the default namespace
47
     *
48
     * @return string
49
     */
50
    public function getDefaultNamespace(): string
51
    {
52
        return $this->sDefaultNamespace;
53
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
54
55
    /**
56
     * Get the view namespaces
57
     *
58
     * @return array
59
     */
60
    public function getNamespaces(): array
61
    {
62
        return $this->aNamespaces;
63
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
64
65
    /**
66
     * Find a view namespace by its name.
67
     *
68
     * @param string $sNamespace    The namespace name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
69
     *
70
     * @return array|null
71
     */
72
    public function getNamespace(string $sNamespace): ?array
73
    {
74
        return $this->aNamespaces[$sNamespace] ?? null;
75
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
76
77
    /**
78
     * Add a view namespace, and set the corresponding renderer.
79
     *
80
     * @param string $sNamespace    The corresponding renderer name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
81
     * @param array $aNamespace    The namespace options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
82
     *
83
     * @return void
84
     */
85
    private function _addNamespace(string $sNamespace, array $aNamespace)
86
    {
87
        $this->aNamespaces[$sNamespace] = $aNamespace;
88
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
89
90
    /**
91
     * Add a view namespace, and set the corresponding renderer.
92
     *
93
     * @param string $sNamespace    The namespace name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
94
     * @param string $sDirectory    The namespace directory
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
95
     * @param string $sExtension    The extension to append to template names
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
96
     * @param string $sRenderer    The corresponding renderer name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
97
     *
98
     * @return void
99
     */
100
    public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension, string $sRenderer)
101
    {
102
        $aNamespace = [
103
            'directory' => $sDirectory,
104
            'extension' => $sExtension,
105
            'renderer' => $sRenderer,
106
        ];
107
        $this->_addNamespace($sNamespace, $aNamespace);
108
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
109
110
    /**
111
     * Set the view namespaces.
112
     *
113
     * @param Config $xLibConfig    The config options provided in the library
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
114
     * @param Config|null $xAppConfig    The config options provided in the app section of the global config file.
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
115
     *
116
     * @return void
117
     */
118
    public function addNamespaces(Config $xLibConfig, ?Config $xAppConfig = null)
119
    {
120
        $this->sDefaultNamespace = $xLibConfig->getOption('options.views.default', '');
121
122
        $sPackage = $xLibConfig->getOption('package', '');
123
124
        if(is_array($aNamespaces = $xLibConfig->getOptionNames('views')))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
introduced by
The condition is_array($aNamespaces = ...etOptionNames('views')) is always true.
Loading history...
125
        {
126
            foreach($aNamespaces as $sNamespace => $sOption)
127
            {
128
                // If no default namespace is defined, use the first one as default.
129
                if($this->sDefaultNamespace == '')
130
                {
131
                    $this->sDefaultNamespace = (string)$sNamespace;
132
                }
133
                // Save the namespace
134
                $aNamespace = $xLibConfig->getOption($sOption);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
135
                $aNamespace['package'] = $sPackage;
136
                if(!isset($aNamespace['renderer']))
137
                {
138
                    $aNamespace['renderer'] = 'jaxon'; // 'jaxon' is the default renderer.
139
                }
140
141
                // If the lib config has defined a template option, then its value must be
142
                // read from the app config.
143
                if($xAppConfig !== null && isset($aNamespace['template']))
144
                {
145
                    $sTemplateOption = $xLibConfig->getOption($sOption . '.template.option');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
146
                    $sTemplateDefault = $xLibConfig->getOption($sOption . '.template.default');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
147
                    $sTemplate = $xAppConfig->getOption($sTemplateOption, $sTemplateDefault);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Bug introduced by
It seems like $sTemplateOption can also be of type null; however, parameter $sName of Jaxon\Utils\Config\Config::getOption() 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

147
                    $sTemplate = $xAppConfig->getOption(/** @scrutinizer ignore-type */ $sTemplateOption, $sTemplateDefault);
Loading history...
148
                    $aNamespace['directory'] = rtrim($aNamespace['directory'], '/') . '/' . $sTemplate;
149
                }
150
151
                $this->_addNamespace($sNamespace, $aNamespace);
152
            }
153
        }
154
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
155
156
    /**
157
     * Get the view renderer
158
     *
159
     * @param string $sId    The unique identifier of the view renderer
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
160
     *
161
     * @return View
162
     */
163
    public function getRenderer(string $sId): View
164
    {
165
        // Return the view renderer with the given id
166
        return $this->di->get('jaxon.app.view.' . $sId);
167
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
168
169
    /**
170
     * Add a view renderer with an id
171
     *
172
     * @param string $sId    The unique identifier of the view renderer
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
173
     * @param Closure $xClosure    A closure to create the view instance
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
174
     *
175
     * @return void
176
     */
177
    public function addRenderer(string $sId, Closure $xClosure)
178
    {
179
        // Return the initialized view renderer
180
        $this->di->set('jaxon.app.view.' . $sId, function($di) use ($sId, $xClosure) {
181
            // Get the defined renderer
182
            $xRenderer = $xClosure($di);
183
            // Init the renderer with the template namespaces
184
            $aNamespaces = array_filter($this->aNamespaces, function($aNamespace) use($sId) {
185
                return $aNamespace['renderer'] === $sId;
186
            });
187
            foreach($aNamespaces as $sNamespace => $aNamespace)
188
            {
189
                $xRenderer->addNamespace($sNamespace, $aNamespace['directory'], $aNamespace['extension']);
190
            }
191
            return $xRenderer;
192
        });
193
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
194
195
    /**
196
     * Get the view renderer for a given namespace
197
     *
198
     * @param string $sNamespace    The namespace name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
199
     *
200
     * @return View|null
201
     */
202
    public function getNamespaceRenderer(string $sNamespace): ?View
203
    {
204
        $aNamespace = $this->getNamespace($sNamespace);
205
        if(!$aNamespace)
206
        {
207
            return null;
208
        }
209
        // Return the view renderer with the configured id
210
        return $this->getRenderer($aNamespace['renderer']);
211
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
212
}
213