Completed
Push — master ( 34bc1a...41d5a5 )
by Thierry
02:48 queued 02:48
created

CallableRepository::setNamespaceOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * CallableRepository.php - Jaxon callable object repository
5
 *
6
 * This class stores all the callable object already created.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2019 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\Request\Plugin\CallableClass;
16
17
use Jaxon\Di\Container;
18
use Jaxon\Exception\SetupException;
19
use Jaxon\Utils\Translation\Translator;
20
21
use function array_merge;
22
use function strlen;
23
use function strncmp;
24
25
class CallableRepository
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableRepository
Loading history...
26
{
27
    /**
28
     * The DI container
29
     *
30
     * @var Container
31
     */
32
    protected $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
33
34
    /**
35
     * @var Translator
36
     */
37
    protected $xTranslator;
38
39
    /**
40
     * The namespace options
41
     *
42
     * These are the options of the registered namespaces.
43
     *
44
     * @var array
45
     */
46
    protected $aNamespaceOptions = [];
47
48
    /**
49
     * The directory options
50
     *
51
     * These are the options of the registered directories.
52
     *
53
     * @var array
54
     */
55
    protected $aDirectoryOptions = [];
56
57
    /**
58
     * The classes
59
     *
60
     * These are all the classes, both registered and found in registered directories.
61
     *
62
     * @var array
63
     */
64
    protected $aClasses = [];
65
66
    /**
67
     * The namespaces
68
     *
69
     * These are all the namespaces found in registered directories.
70
     *
71
     * @var array
72
     */
73
    protected $aNamespaces = [];
74
75
    /**
76
     * The string that will be used to compute the js file hash
77
     *
78
     * @var string
79
     */
80
    protected $sHash = '';
81
82
    /**
83
     * The constructor
84
     *
85
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
86
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
87
     */
88
    public function __construct(Container $di, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
89
    {
90
        $this->di = $di;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
91
        $this->xTranslator = $xTranslator;
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * @return array
96
     */
97
    public function getDirectoryOptions(): array
98
    {
99
        return $this->aDirectoryOptions;
100
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
101
102
    /**
103
     * @param string $sDirectory
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
104
     * @param array $aOptions
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
105
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
106
    public function setDirectoryOptions(string $sDirectory, array $aOptions): void
107
    {
108
        $this->aDirectoryOptions[$sDirectory] = $aOptions;
109
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
110
111
    /**
112
     * @return array
113
     */
114
    public function getNamespaceOptions(): array
115
    {
116
        return $this->aNamespaceOptions;
117
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
118
119
    /**
120
     * @param string $sNamespace
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
121
     * @param array $aOptions
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
122
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
123
    public function setNamespaceOptions(string $sNamespace, array $aOptions): void
124
    {
125
        $this->aNamespaceOptions[$sNamespace] = $aOptions;
126
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
127
128
    /**
129
     * Get all registered namespaces
130
     *
131
     * @return array
132
     */
133
    public function getNamespaces(): array
134
    {
135
        return $this->aNamespaces;
136
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
137
138
    /**
139
     * Get the hash
140
     *
141
     * @return string
142
     */
143
    public function getHash(): string
144
    {
145
        return $this->sHash;
146
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
    /**
149
     * Get a given class options from specified directory options
150
     *
151
     * @param string $sClassName    The class name
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
152
     * @param array $aClassOptions    The default class options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
153
     * @param array $aDirectoryOptions    The directory 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...
154
     *
155
     * @return array
156
     */
157
    public function makeClassOptions(string $sClassName, array $aClassOptions, array $aDirectoryOptions): array
158
    {
159
        if(!isset($aClassOptions['functions']))
160
        {
161
            $aClassOptions['functions'] = [];
162
        }
163
        foreach(['separator', 'protected'] as $sName)
164
        {
165
            if(isset($aDirectoryOptions[$sName]))
166
            {
167
                $aClassOptions[$sName] = $aDirectoryOptions[$sName];
168
            }
169
        }
170
171
        $aFunctionOptions = $aDirectoryOptions['classes'] ?? [];
172
        foreach($aFunctionOptions as $sName => $xValue)
173
        {
174
            if($sName === '*' || strncmp($sClassName, $sName, strlen($sName)) === 0)
175
            {
176
                $aClassOptions['functions'] = array_merge($aClassOptions['functions'], $xValue);
177
            }
178
        }
179
        // This value will be used to compute hash
180
        if(!isset($aClassOptions['timestamp']))
181
        {
182
            $aClassOptions['timestamp'] = 0;
183
        }
184
185
        return $aClassOptions;
186
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
187
188
    /**
189
     *
190
     * @param string $sClassName    The class name
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
191
     * @param array $aClassOptions    The default class options
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
192
     * @param array $aDirectoryOptions    The directory options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
193
     *
194
     * @return void
195
     */
196
    public function addClass(string $sClassName, array $aClassOptions, array $aDirectoryOptions = [])
197
    {
198
        $this->aClasses[$sClassName] = $this->makeClassOptions($sClassName, $aClassOptions, $aDirectoryOptions);
199
        $this->sHash .= $sClassName . $this->aClasses[$sClassName]['timestamp'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 16 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...
200
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
201
202
    /**
203
     *
204
     * @param string $sNamespace    The namespace
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
205
     * @param array $aOptions    The associated options
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
206
     *
207
     * @return void
208
     */
209
    public function addNamespace(string $sNamespace, array $aOptions)
210
    {
211
        $this->aNamespaces[] = $sNamespace;
212
        $this->sHash .= $sNamespace . $aOptions['separator'];
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...
213
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
214
215
    /**
216
     * Find options for a class which is registered with namespace
217
     *
218
     * @param string $sClassName    The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
219
     *
220
     * @return void
221
     */
222
    private function getNamespaceClassOptions(string $sClassName)
223
    {
224
        // Find the corresponding namespace
225
        foreach($this->aNamespaceOptions as $sNamespace => $aOptions)
226
        {
227
            // Check if the namespace matches the class.
228
            if(strncmp($sClassName, $sNamespace . '\\', strlen($sNamespace) + 1) === 0)
229
            {
230
                // Save the class options
231
                $this->aClasses[$sClassName] = $this->makeClassOptions($sClassName,
232
                    ['namespace' => $sNamespace], $aOptions);
233
                return;
234
            }
235
        }
236
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
237
238
    /**
239
     * Find the options associated with a registered class name
240
     *
241
     * @param string $sClassName The class name
242
     *
243
     * @return array
244
     * @throws SetupException
245
     */
246
    public function getClassOptions(string $sClassName): array
247
    {
248
        // Find options for a class registered with namespace.
249
        if(!isset($this->aClasses[$sClassName]))
250
        {
251
            $this->getNamespaceClassOptions($sClassName);
252
            if(!isset($this->aClasses[$sClassName]))
253
            {
254
                // Without a namespace, we need to parse all classes to be able to find one.
255
                $this->di->getClassRegistry()->parseDirectories();
256
            }
257
        }
258
        // Find options for a class registered without namespace.
259
        if(isset($this->aClasses[$sClassName]))
260
        {
261
            return $this->aClasses[$sClassName];
262
        }
263
        $sMessage = $this->xTranslator->trans('errors.class.invalid', ['name' => $sClassName]);
264
        throw new SetupException($sMessage);
265
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
266
267
    /**
268
     * Get callable objects for known classes
269
     *
270
     * @return array
271
     * @throws SetupException
272
     */
273
    public function getCallableObjects(): array
274
    {
275
        $aCallableObjects = [];
276
        foreach($this->aClasses as $sClassName => $aOptions)
277
        {
278
            if(!$this->di->h($sClassName))
279
            {
280
                $this->di->registerCallableClass($sClassName, $aOptions);
281
            }
282
            $aCallableObjects[$sClassName] = $this->di->getCallableObject($sClassName);
283
        }
284
        return $aCallableObjects;
285
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
286
}
287