Passed
Push — master ( 0602c9...8f96ba )
by Thierry
02:02
created

Manager::addNamespaces()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 34
rs 8.8333
c 0
b 0
f 0
cc 7
nc 10
nop 2
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
class Manager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Manager
Loading history...
11
{
12
    /**
13
     * @var Container
14
     */
15
    protected $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
16
17
    /**
18
     * The default namespace
19
     *
20
     * @var string
21
     */
22
    protected $sDefaultNamespace = '';
23
24
    /**
25
     * The view namespaces
26
     *
27
     * @var array
28
     */
29
    protected $aNamespaces = [];
30
31
    /**
32
     * The class constructor
33
     *
34
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
35
     */
36
    public function __construct(Container $di)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
37
    {
38
        $this->di = $di;
39
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
40
41
    /**
42
     * Get the default namespace
43
     *
44
     * @return string
45
     */
46
    public function getDefaultNamespace()
47
    {
48
        return $this->sDefaultNamespace;
49
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
50
51
    /**
52
     * Get the view namespaces
53
     *
54
     * @return array
55
     */
56
    public function getNamespaces()
57
    {
58
        return $this->aNamespaces;
59
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
60
61
    /**
62
     * Find a view namespace by its name.
63
     *
64
     * @param string        $sNamespace         The namespace name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
65
     *
66
     * @return array|null
67
     */
68
    public function getNamespace($sNamespace)
69
    {
70
        return \array_key_exists($sNamespace, $this->aNamespaces) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
71
            $this->aNamespaces[$sNamespace] : null;
72
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
73
74
    /**
75
     * Add a view namespace, and set the corresponding renderer.
76
     *
77
     * @param string        $sNamespace         The corresponding renderer name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
78
     * @param array         $aNamespace         The namespace options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
79
     *
80
     * @return void
81
     */
82
    private function _addNamespace($sNamespace, array $aNamespace)
83
    {
84
        $this->aNamespaces[$sNamespace] = $aNamespace;
85
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
86
87
    /**
88
     * Add a view namespace, and set the corresponding renderer.
89
     *
90
     * @param string        $sNamespace         The namespace name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
91
     * @param string        $sDirectory         The namespace directory
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
92
     * @param string        $sExtension         The extension to append to template names
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
93
     * @param string        $sRenderer          The corresponding renderer name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 10 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
94
     *
95
     * @return void
96
     */
97
    public function addNamespace($sNamespace, $sDirectory, $sExtension, $sRenderer)
98
    {
99
        $aNamespace = [
100
            'directory' => $sDirectory,
101
            'extension' => $sExtension,
102
            'renderer' => $sRenderer,
103
        ];
104
        $this->_addNamespace($sNamespace, $aNamespace);
105
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
106
107
    /**
108
     * Set the view namespaces.
109
     *
110
     * @param Config    $xLibConfig     The config options provided in the library
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
111
     * @param Config    $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 type; 4 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
112
     *
113
     * @return void
114
     */
115
    public function addNamespaces($xLibConfig, $xAppConfig = null)
116
    {
117
        $this->sDefaultNamespace = $xLibConfig->getOption('options.views.default', '');
118
119
        $sPackage = $xLibConfig->getOption('package', '');
120
121
        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...
122
        {
123
            foreach($aNamespaces as $sNamespace => $sOption)
124
            {
125
                // If no default namespace is defined, use the first one as default.
126
                if($this->sDefaultNamespace == '')
127
                {
128
                    $this->sDefaultNamespace = (string)$sNamespace;
129
                }
130
                // Save the namespace
131
                $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...
132
                $aNamespace['package'] = $sPackage;
133
                if(!\array_key_exists('renderer', $aNamespace))
134
                {
135
                    $aNamespace['renderer'] = 'jaxon'; // 'jaxon' is the default renderer.
136
                }
137
138
                // If the lib config has defined a template option, then its value must be
139
                // read from the app config.
140
                if($xAppConfig !== null && \array_key_exists('template', $aNamespace))
141
                {
142
                    $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...
143
                    $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...
144
                    $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...
145
                    $aNamespace['directory'] = \rtrim($aNamespace['directory'], '/') . '/' . $sTemplate;
146
                }
147
148
                $this->_addNamespace($sNamespace, $aNamespace);
149
            }
150
        }
151
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
152
153
    /**
154
     * Get the view renderer
155
     *
156
     * @param string        $sId        The unique identifier of the view renderer
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 8 found
Loading history...
157
     *
158
     * @return View
159
     */
160
    public function getRenderer($sId)
161
    {
162
        // Return the view renderer with the given id
163
        return $this->di->get('jaxon.app.view.' . $sId);
164
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
165
166
    /**
167
     * Add a view renderer with an id
168
     *
169
     * @param string        $sId        The unique identifier of the view renderer
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 8 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 8 found
Loading history...
170
     * @param Closure       $xClosure   A closure to create the view instance
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 7 found
Loading history...
171
     *
172
     * @return void
173
     */
174
    public function addRenderer($sId, Closure $xClosure)
175
    {
176
        $sBaseId = 'jaxon.app.view.user.' . $sId;
177
        // Return the user defined view renderer
178
        $this->di->set($sBaseId, $xClosure);
179
        // Return the initialized view renderer
180
        $this->di->set('jaxon.app.view.' . $sId, function($di) use ($sBaseId, $sId) {
181
            // Get the defined renderer
182
            $xRenderer = $di->get($sBaseId);
183
184
            // Init the renderer with the template namespaces
185
            $aNamespaces = \array_filter($this->aNamespaces, function($aNamespace) use($sId) {
186
                return $aNamespace['renderer'] === $sId;
187
            });
188
            foreach($aNamespaces as $sNamespace => $aNamespace)
189
            {
190
                $xRenderer->addNamespace($sNamespace, $aNamespace['directory'], $aNamespace['extension']);
191
            }
192
            return $xRenderer;
193
        });
194
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
195
196
    /**
197
     * Get the view renderer for a given namespace
198
     *
199
     * @param string        $sNamespace         The namespace name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
200
     *
201
     * @return View|null
202
     */
203
    public function getNamespaceRenderer($sNamespace)
204
    {
205
        $aNamespace = $this->getNamespace($sNamespace);
206
        if(!$aNamespace)
207
        {
208
            return null;
209
        }
210
        // Return the view renderer with the configured id
211
        return $this->getRenderer($aNamespace['renderer']);
212
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
213
}
214