Passed
Push — master ( f79a4b...8eafb5 )
by Thierry
02:11
created

Jaxon::getVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 3
b 1
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Jaxon.php - Jaxon class
5
 *
6
 * The Jaxon class uses a modular plug-in system to facilitate the processing
7
 * of special Ajax requests made by a PHP page.
8
 * It generates Javascript that the page must include in order to make requests.
9
 * It handles the output of response commands (see <Jaxon\Response\Response>).
10
 * Many flags and settings can be adjusted to effect the behavior of the Jaxon class
11
 * as well as the client-side javascript.
12
 *
13
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
14
 * @author Jared White
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
15
 * @author J. Max Wilson
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
16
 * @author Joseph Woolley
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
17
 * @author Steffen Konerow
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
18
 * @author Thierry Feuzeu <[email protected]>
19
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
20
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
21
 * @copyright 2016 Thierry Feuzeu <[email protected]>
22
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
23
 * @link https://github.com/jaxon-php/jaxon-core
24
 */
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...
25
26
namespace Jaxon;
27
28
use Jaxon\App\App;
29
use Jaxon\Container\Container;
30
use Jaxon\Contracts\Session;
31
use Jaxon\Exception\RequestException;
32
use Jaxon\Exception\SetupException;
33
use Jaxon\Plugin\Code\Generator as CodeGenerator;
34
use Jaxon\Plugin\Manager as PluginManager;
35
use Jaxon\Plugin\Package;
36
use Jaxon\Plugin\Response as ResponsePlugin;
37
use Jaxon\Request\Factory\Factory;
38
use Jaxon\Request\Factory\RequestFactory;
39
use Jaxon\Request\Handler\Callback;
40
use Jaxon\Request\Handler\Handler as RequestHandler;
41
use Jaxon\Request\Support\CallableRegistry;
42
use Jaxon\Request\Upload\Plugin as UploadPlugin;
43
use Jaxon\Response\Manager as ResponseManager;
44
use Jaxon\Response\AbstractResponse;
45
use Jaxon\Response\Response;
46
use Jaxon\Ui\Dialogs\Dialog;
47
use Jaxon\Ui\View\Renderer;
48
use Jaxon\Utils\Config\Config;
49
use Jaxon\Utils\Config\Exception\DataDepth;
50
use Jaxon\Utils\Config\Exception\FileAccess;
51
use Jaxon\Utils\Config\Exception\FileContent;
52
use Jaxon\Utils\Config\Exception\FileExtension;
53
use Jaxon\Utils\Config\Exception\YamlExtension;
54
use Jaxon\Utils\Config\Reader as ConfigReader;
55
use Jaxon\Utils\Template\Engine as TemplateEngine;
56
use Jaxon\Utils\Translation\Translator;
57
use Jaxon\Utils\Http\UriException;
58
use Psr\Log\LoggerAwareInterface;
59
use Psr\Log\LoggerAwareTrait;
60
use Psr\Log\LoggerInterface;
61
use Psr\Log\NullLogger;
62
use function headers_sent;
63
64
class Jaxon implements LoggerAwareInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Jaxon
Loading history...
65
{
66
    use LoggerAwareTrait;
67
68
    /**
69
     * Package version number
70
     *
71
     * @const string
72
     */
73
    const VERSION = 'Jaxon 4.0.0-dev';
74
75
    /*
76
     * Request plugins
77
     */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
78
    const CALLABLE_CLASS = 'CallableClass';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
79
    const CALLABLE_DIR = 'CallableDir';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
80
    const CALLABLE_FUNCTION = 'CallableFunction';
81
82
    /**
83
     * A static instance on this class
84
     *
85
     * @var Jaxon
86
     */
87
    private static $xInstance = null;
88
89
    /**
90
     * The DI container
91
     *
92
     * @var Container
93
     */
94
    private static $xContainer = null;
95
96
    /**
97
     * @var Config
98
     */
99
    protected $xConfig;
100
101
    /**
102
     * @var Translator
103
     */
104
    protected $xTranslator;
105
106
    /**
107
     * @var ConfigReader
108
     */
109
    protected $xConfigReader;
110
111
    /**
112
     * @var PluginManager
113
     */
114
    protected $xPluginManager;
115
116
    /**
117
     * @var CodeGenerator
118
     */
119
    protected $xCodeGenerator;
120
121
    /**
122
     * @var CallableRegistry
123
     */
124
    protected $xCallableRegistry;
125
126
    /**
127
     * @var RequestHandler
128
     */
129
    protected $xRequestHandler;
130
131
    /**
132
     * @var ResponseManager
133
     */
134
    protected $xResponseManager;
135
136
    /**
137
     * @return void
138
     */
139
    private static function initInstance()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
140
    {
141
        // Save the Jaxon instance in the DI
142
        self::$xContainer->val(Jaxon::class, self::$xInstance);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
143
        // Set the attributes from the container
144
        self::$xInstance->xConfig = self::$xContainer->g(Config::class);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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
        self::$xInstance->xTranslator = self::$xContainer->g(Translator::class);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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
        self::$xInstance->xConfigReader = self::$xContainer->g(ConfigReader::class);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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
        self::$xInstance->xPluginManager = self::$xContainer->g(PluginManager::class);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
148
        self::$xInstance->xCodeGenerator = self::$xContainer->g(CodeGenerator::class);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
149
        self::$xInstance->xCallableRegistry = self::$xContainer->g(CallableRegistry::class);
150
        self::$xInstance->xRequestHandler = self::$xContainer->g(RequestHandler::class);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
151
        self::$xInstance->xResponseManager = self::$xContainer->g(ResponseManager::class);
152
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
153
154
    /**
155
     * Get the static instance
156
     *
157
     * @return Jaxon
158
     */
159
    public static function getInstance(): ?Jaxon
160
    {
161
        if(self::$xInstance === null)
162
        {
163
            self::$xContainer = new Container(self::getDefaultOptions());
164
            self::$xInstance = new Jaxon();
165
            self::initInstance();
166
        }
167
        return self::$xInstance;
168
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
169
170
    /**
171
     * The constructor
172
     */
173
    private function __construct()
174
    {
175
        // Set the default logger
176
        $this->setLogger(new NullLogger());
177
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
178
179
    /**
180
     * The current Jaxon version
181
     *
182
     * @return string
183
     */
184
    public function getVersion(): string
185
    {
186
        return self::VERSION;
187
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
188
189
    /**
190
     * Get the default options of all components of the library
191
     *
192
     * @return array<string,string|bool|integer>
193
     */
194
    private static function getDefaultOptions(): array
195
    {
196
        // The default configuration settings.
197
        return [
198
            'core.version'                      => self::VERSION,
199
            'core.language'                     => 'en',
200
            'core.encoding'                     => 'utf-8',
201
            'core.decode_utf8'                  => false,
202
            'core.prefix.function'              => 'jaxon_',
203
            'core.prefix.class'                 => 'Jaxon',
204
            // 'core.request.uri'               => '',
205
            'core.request.mode'                 => 'asynchronous',
206
            'core.request.method'               => 'POST', // W3C: Method is case sensitive
207
            'core.response.send'                => true,
208
            'core.response.merge.ap'            => true,
209
            'core.response.merge.js'            => true,
210
            'core.debug.on'                     => false,
211
            'core.debug.verbose'                => false,
212
            'core.process.exit'                 => true,
213
            'core.process.clean'                => false,
214
            'core.process.timeout'              => 6000,
215
            'core.error.handle'                 => false,
216
            'core.error.log_file'               => '',
217
            'core.jquery.no_conflict'           => false,
218
            'core.upload.enabled'               => true,
219
            'js.lib.output_id'                  => 0,
220
            'js.lib.queue_size'                 => 0,
221
            'js.lib.load_timeout'               => 2000,
222
            'js.lib.show_status'                => false,
223
            'js.lib.show_cursor'                => true,
224
            'js.app.dir'                        => '',
225
            'js.app.minify'                     => true,
226
            'js.app.options'                    => '',
227
        ];
228
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
229
230
    /**
231
     * Get the DI container
232
     *
233
     * @return Container
234
     */
235
    public function di(): ?Container
236
    {
237
        return self::$xContainer;
238
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
239
240
    /**
241
     * Get the logger
242
     *
243
     * @return LoggerInterface
244
     */
245
    public function logger(): LoggerInterface
246
    {
247
        return $this->logger;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->logger could return the type null which is incompatible with the type-hinted return Psr\Log\LoggerInterface. Consider adding an additional type-check to rule them out.
Loading history...
248
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
249
250
    /**
251
     * Get the library config
252
     *
253
     * @return Config
254
     */
255
    public function config(): Config
256
    {
257
        return $this->xConfig;
258
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
259
260
    /**
261
     * Read a config file
262
     *
263
     * @param string $sConfigFile
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
264
     *
265
     * @return array
266
     * @throws SetupException
267
     */
268
    public function readConfig(string $sConfigFile): array
269
    {
270
        try
271
        {
272
            return $this->xConfigReader->read($sConfigFile);
273
        }
274
        catch(YamlExtension $e)
275
        {
276
            $sMessage = $this->xTranslator->trans('errors.yaml.install');
277
            throw new SetupException($sMessage);
278
        }
279
        catch(FileExtension $e)
280
        {
281
            $sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]);
282
            throw new SetupException($sMessage);
283
        }
284
        catch(FileAccess $e)
285
        {
286
            $sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]);
287
            throw new SetupException($sMessage);
288
        }
289
        catch(FileContent $e)
290
        {
291
            $sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]);
292
            throw new SetupException($sMessage);
293
        }
294
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
295
296
    /**
297
     * Load a config file
298
     *
299
     * @param string $sConfigFile
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
300
     * @param string $sConfigSection
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
301
     *
302
     * @return void
303
     * @throws SetupException
304
     */
305
    public function loadConfig(string $sConfigFile, string $sConfigSection = '')
306
    {
307
        $aConfigOptions = $this->readConfig($sConfigFile);
308
        try
309
        {
310
            // Set up the lib config options.
311
            $this->xConfig->setOptions($aConfigOptions, $sConfigSection);
312
        }
313
        catch(DataDepth $e)
314
        {
315
            $sMessage = $this->xTranslator->trans('errors.data.depth', ['key' => $e->sPrefix, 'depth' => $e->nDepth]);
316
            throw new SetupException($sMessage);
317
        }
318
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
319
320
    /**
321
     * Set the value of a config option
322
     *
323
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
324
     * @param mixed $sValue    The option value
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...
325
     *
326
     * @return void
327
     */
328
    public function setOption(string $sName, $sValue)
329
    {
330
        $this->xConfig->setOption($sName, $sValue);
331
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
332
333
    /**
334
     * Get the value of a config option
335
     *
336
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
337
     * @param mixed|null $xDefault    The default value, to be returned if the option is not defined
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
338
     *
339
     * @return mixed        The option value, or null if the option is unknown
340
     */
341
    public function getOption(string $sName, $xDefault = null)
342
    {
343
        return $this->xConfig->getOption($sName, $xDefault);
344
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
345
346
    /**
347
     * Check the presence of a config option
348
     *
349
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
350
     *
351
     * @return bool        True if the option exists, and false if not
352
     */
353
    public function hasOption(string $sName): bool
354
    {
355
        return $this->xConfig->hasOption($sName);
356
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
357
358
    /**
359
     * Create a new the config set
360
     *
361
     * @return Config            The config manager
362
     * @throws SetupException
363
     */
364
    public function newConfig(): Config
365
    {
366
        return $this->di()->newConfig();
367
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
368
369
    /**
370
     * Get the configured character encoding
371
     *
372
     * @return string
373
     */
374
    public function getCharacterEncoding(): string
375
    {
376
        return trim($this->xConfig->getOption('core.encoding'));
0 ignored issues
show
Bug introduced by
It seems like $this->xConfig->getOption('core.encoding') can also be of type null; however, parameter $string of trim() 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

376
        return trim(/** @scrutinizer ignore-type */ $this->xConfig->getOption('core.encoding'));
Loading history...
377
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
378
379
    /**
380
     * Get a translated string
381
     *
382
     * @param string $sText    The key of the translated string
0 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter name; 4 found
Loading history...
383
     * @param array $aPlaceHolders    The placeholders of the translated string
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...
384
     * @param string $sLanguage    The language of the translated string
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
385
     *
386
     * @return string
387
     */
388
    public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string
389
    {
390
        return $this->xTranslator->trans($sText, $aPlaceHolders, $sLanguage);
391
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
392
393
    /**
394
     * Get the Global Response object
395
     *
396
     * @return AbstractResponse
397
     */
398
    public function getResponse(): AbstractResponse
399
    {
400
        if(($xResponse = $this->xResponseManager->getResponse()))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
401
        {
402
            return $xResponse;
403
        }
404
        return $this->di()->getResponse();
405
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
406
407
    /**
408
     * Create a new Jaxon response object
409
     *
410
     * @return Response
411
     */
412
    public function newResponse(): Response
413
    {
414
        return $this->di()->newResponse();
415
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
416
417
    /**
418
     * Register a plugin
419
     *
420
     * Below is a table for priorities and their description:
421
     * - 0 to 999: Plugins that are part of or extensions to the jaxon core
422
     * - 1000 to 8999: User created plugins, typically, these plugins don't care about order
423
     * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list
424
     *
425
     * @param string $sClassName    The plugin class
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
426
     * @param string $sPluginName    The plugin name
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...
427
     * @param integer $nPriority    The plugin priority, used to order the plugins
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
428
     *
429
     * @return void
430
     * @throws SetupException
431
     */
432
    public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000)
433
    {
434
        $this->xPluginManager->registerPlugin($sClassName, $sPluginName, $nPriority);
435
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
436
437
    /**
438
     * Register a package
439
     *
440
     * @param string $sClassName    The package class
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
441
     * @param array $aOptions    The package 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...
442
     *
443
     * @return void
444
     * @throws SetupException
445
     */
446
    public function registerPackage(string $sClassName, array $aOptions = [])
447
    {
448
        $this->xPluginManager->registerPackage($sClassName, $aOptions);
449
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
450
451
    /**
452
     * Register request handlers, including functions, callable classes and directories.
453
     *
454
     * @param string $sType    The type of request handler being registered
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
455
     *        Options include:
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 25 spaces but found 8
Loading history...
456
     *        - Jaxon::CALLABLE_FUNCTION: a function declared at global scope
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 25 spaces but found 8
Loading history...
457
     *        - Jaxon::CALLABLE_CLASS: a class who's methods are to be registered
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 25 spaces but found 8
Loading history...
458
     *        - Jaxon::CALLABLE_DIR: a directory containing classes to be registered
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 25 spaces but found 8
Loading history...
459
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
460
     *        When registering a function, this is the name of the function
461
     *        When registering a callable class, this is the class name
462
     *        When registering a callable directory, this is the full path to the directory
463
     * @param array|string $xOptions    The related options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
464
     *
465
     * @return void
466
     * @throws SetupException
467
     */
468
    public function register(string $sType, string $sName, $xOptions = [])
469
    {
470
        $this->xPluginManager->registerCallable($sType, $sName, $xOptions);
471
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
472
473
    /**
474
     * Get an instance of a registered class
475
     *
476
     * @param string $sClassName    The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
477
     *
478
     * @return null|object
479
     */
480
    public function instance(string $sClassName)
481
    {
482
        $xCallable = $this->xCallableRegistry->getCallableObject($sClassName);
483
        return ($xCallable) ? $xCallable->getRegisteredObject() : null;
0 ignored issues
show
introduced by
$xCallable is of type Jaxon\Request\Support\CallableObject, thus it always evaluated to true.
Loading history...
484
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
485
486
    /**
487
     * Get the factory
488
     *
489
     * @return Factory
490
     */
491
    public function factory(): Factory
492
    {
493
        return $this->di()->g(Factory::class);
494
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
495
496
    /**
497
     * Get a request to a registered class
498
     *
499
     * @param string $sClassName    The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
500
     *
501
     * @return RequestFactory|null
502
     */
503
    public function request(string $sClassName): ?RequestFactory
504
    {
505
        return $this->factory()->request($sClassName);
506
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
507
508
    /**
509
     * Returns the Jaxon Javascript header and wrapper code to be printed into the page
510
     *
511
     * The javascript code returned by this function is dependent on the plugins
512
     * that are included and the functions and classes that are registered.
513
     *
514
     * @param bool $bIncludeJs    Also get the JS files
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
515
     * @param bool $bIncludeCss    Also get the CSS files
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
516
     *
517
     * @return string
518
     * @throws UriException
519
     */
520
    public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string
521
    {
522
        return $this->xCodeGenerator->getScript($bIncludeJs, $bIncludeCss);
523
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
524
525
    /**
526
     * Print the jaxon Javascript header and wrapper code into your page
527
     *
528
     * The javascript code returned by this function is dependent on the plugins
529
     * that are included and the functions and classes that are registered.
530
     *
531
     * @param bool $bIncludeJs    Also print the JS files
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
532
     * @param bool $bIncludeCss    Also print the CSS files
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
533
     *
534
     * @return void
535
     * @throws UriException
536
     */
537
    public function printScript(bool $bIncludeJs = false, bool $bIncludeCss = false)
538
    {
539
        print $this->getScript($bIncludeJs, $bIncludeCss);
540
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
541
542
    /**
543
     * Return the javascript header code and file includes
544
     *
545
     * @return string
546
     */
547
    public function getJs(): string
548
    {
549
        return $this->xCodeGenerator->getJs();
550
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
551
552
    /**
553
     * Return the CSS header code and file includes
554
     *
555
     * @return string
556
     */
557
    public function getCss(): string
558
    {
559
        return $this->xCodeGenerator->getCss();
560
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
561
562
    /**
563
     * Determine if a call is a jaxon request or a page load request
564
     *
565
     * @return bool
566
     */
567
    public function canProcessRequest(): bool
568
    {
569
        return $this->xRequestHandler->canProcessRequest();
570
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
571
572
    /**
573
     * If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser
574
     *
575
     * This is the main server side engine for Jaxon.
576
     * It handles all the incoming requests, including the firing of events and handling of the response.
577
     * If your RequestURI is the same as your web page, then this function should be called before ANY
578
     * headers or HTML is output from your script.
579
     *
580
     * This function may exit after the request is processed, if the 'core.process.exit' option is set to true.
581
     *
582
     * @return void
583
     *
584
     * @throws RequestException
585
     * @see <Jaxon\Jaxon->canProcessRequest>
586
     */
587
    public function processRequest()
588
    {
589
        // Check to see if headers have already been sent out, in which case we can't do our job
590
        if(headers_sent($filename, $linenumber))
591
        {
592
            echo $this->xTranslator->trans('errors.output.already-sent', [
593
                'location' => $filename . ':' . $linenumber
594
            ]), "\n", $this->xTranslator->trans('errors.output.advice');
595
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
596
        }
597
598
        $this->xRequestHandler->processRequest();
599
600
        if(($this->xConfig->getOption('core.response.send')))
601
        {
602
            $this->xResponseManager->sendOutput();
603
            if(($this->xConfig->getOption('core.process.exit')))
604
            {
605
                exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
606
            }
607
        }
608
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
609
610
    /**
611
     * Get a registered response plugin
612
     *
613
     * @param string $sName    The name of the plugin
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
614
     *
615
     * @return ResponsePlugin
616
     */
617
    public function plugin(string $sName): ResponsePlugin
618
    {
619
        return $this->xPluginManager->getResponsePlugin($sName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->xPluginMan...tResponsePlugin($sName) could return the type null which is incompatible with the type-hinted return Jaxon\Plugin\Response. Consider adding an additional type-check to rule them out.
Loading history...
620
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
621
622
    /**
623
     * Get a package instance
624
     *
625
     * @param string $sClassName    The package class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
626
     *
627
     * @return Package
628
     */
629
    public function package(string $sClassName): Package
630
    {
631
        return $this->xPluginManager->getPackage($sClassName);
632
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
633
634
    /**
635
     * Get the upload plugin
636
     *
637
     * @return UploadPlugin|null
638
     */
639
    public function upload(): ?UploadPlugin
640
    {
641
        return $this->di()->getUploadPlugin();
642
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
643
644
    /**
645
     * Get the request callback manager
646
     *
647
     * @return Callback
648
     */
649
    public function callback(): Callback
650
    {
651
        return $this->xRequestHandler->getCallbackManager();
652
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
653
654
    /**
655
     * Get the dialog wrapper
656
     *
657
     * @return Dialog
658
     */
659
    public function dialog(): Dialog
660
    {
661
        return $this->di()->getDialog();
662
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
663
664
    /**
665
     * Get the template engine
666
     *
667
     * @return TemplateEngine
668
     */
669
    public function template(): TemplateEngine
670
    {
671
        return $this->di()->getTemplateEngine();
672
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
673
674
    /**
675
     * Get the App instance
676
     *
677
     * @return App
678
     */
679
    public function app(): App
680
    {
681
        return $this->di()->getApp();
682
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
683
684
    /**
685
     * Get the view renderer
686
     *
687
     * @return Renderer
688
     */
689
    public function view(): Renderer
690
    {
691
        return $this->di()->getViewRenderer();
692
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
693
694
    /**
695
     * Get the session manager
696
     *
697
     * @return Session
698
     */
699
    public function session(): Session
700
    {
701
        return $this->di()->getSessionManager();
702
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
703
}
704