Passed
Push — master ( 6eb156...b34d93 )
by Thierry
04:25
created

Jaxon::paginator()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\App\Bootstrap;
30
use Jaxon\Config\ConfigManager;
31
use Jaxon\Di\Container;
32
use Jaxon\Exception\RequestException;
33
use Jaxon\Exception\SetupException;
34
use Jaxon\Plugin\Code\CodeGenerator;
35
use Jaxon\Plugin\Manager\PackageManager;
36
use Jaxon\Plugin\Manager\PluginManager;
37
use Jaxon\Plugin\Package;
38
use Jaxon\Plugin\ResponsePlugin;
39
use Jaxon\Request\Factory;
40
use Jaxon\Request\Factory\RequestFactory;
41
use Jaxon\Request\Handler\CallbackManager;
42
use Jaxon\Request\Handler\UploadHandler;
43
use Jaxon\Request\Plugin\CallableClass\CallableRegistry;
44
use Jaxon\Response\Manager\ResponseManager;
45
use Jaxon\Response\Response;
46
use Jaxon\Response\ResponseInterface;
47
use Jaxon\Session\SessionInterface;
48
use Jaxon\Ui\Pagination\Paginator;
49
use Jaxon\Ui\View\ViewRenderer;
50
use Jaxon\Utils\Http\UriException;
51
use Jaxon\Utils\Template\TemplateEngine;
52
use Jaxon\Utils\Translation\Translator;
53
54
use Psr\Log\LoggerInterface;
55
56
use function gmdate;
57
use function header;
58
use function headers_sent;
59
use function trim;
60
use function error_reporting;
61
use function ob_end_clean;
62
use function ob_get_level;
63
64
class Jaxon
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Jaxon
Loading history...
65
{
66
    /**
67
     * Package version number
68
     *
69
     * @const string
70
     */
71
    const VERSION = 'Jaxon 4.0.0-dev';
72
73
    /*
74
     * Request plugins
75
     */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
76
    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...
77
    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...
78
    const CALLABLE_FUNCTION = 'CallableFunction';
79
80
    /**
81
     * A static instance on this class
82
     *
83
     * @var Jaxon
84
     */
85
    private static $xInstance = null;
86
87
    /**
88
     * The DI container
89
     *
90
     * @var Container
91
     */
92
    private static $xContainer = null;
93
94
    /**
95
     * @var Bootstrap
96
     */
97
    protected $xBootstrap;
98
99
    /**
100
     * @var Translator
101
     */
102
    protected $xTranslator;
103
104
    /**
105
     * @var ConfigManager
106
     */
107
    protected $xConfigManager;
108
109
    /**
110
     * @var PluginManager
111
     */
112
    protected $xPluginManager;
113
114
    /**
115
     * @var PackageManager
116
     */
117
    protected $xPackageManager;
118
119
    /**
120
     * @var CodeGenerator
121
     */
122
    protected $xCodeGenerator;
123
124
    /**
125
     * @var CallableRegistry
126
     */
127
    protected $xClassRegistry;
128
129
    /**
130
     * @var CallbackManager
131
     */
132
    protected $xCallbackManager;
133
134
    /**
135
     * @var ResponseManager
136
     */
137
    protected $xResponseManager;
138
139
    /**
140
     * @return void
141
     */
142
    private static function initInstance()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
143
    {
144
        // Set the attributes from the container
145
        self::$xInstance->xBootstrap = self::$xContainer->g(Bootstrap::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->xTranslator = self::$xContainer->g(Translator::class);
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...
147
        self::$xInstance->xConfigManager = self::$xContainer->g(ConfigManager::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...
148
        self::$xInstance->xPluginManager = self::$xContainer->g(PluginManager::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...
149
        self::$xInstance->xPackageManager = self::$xContainer->g(PackageManager::class);
150
        self::$xInstance->xCodeGenerator = self::$xContainer->g(CodeGenerator::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->xClassRegistry = self::$xContainer->g(CallableRegistry::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...
152
        self::$xInstance->xCallbackManager = self::$xContainer->g(CallbackManager::class);
153
        self::$xInstance->xResponseManager = self::$xContainer->g(ResponseManager::class);
154
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
155
156
    /**
157
     * Get the static instance
158
     *
159
     * @return Jaxon
160
     */
161
    public static function getInstance(): ?Jaxon
162
    {
163
        if(self::$xInstance === null)
164
        {
165
            self::$xInstance = new Jaxon();
166
            self::$xContainer = new Container(self::$xInstance);
167
            self::initInstance();
168
        }
169
        // Call the on boot callbacks on each call to the jaxon() function.
170
        self::$xInstance->xBootstrap->onBoot();
171
        return self::$xInstance;
172
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
173
174
    /**
175
     * The constructor
176
     */
177
    private function __construct()
178
    {}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
179
180
    /**
181
     * The current Jaxon version
182
     *
183
     * @return string
184
     */
185
    public function getVersion(): string
186
    {
187
        return self::VERSION;
188
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
189
190
    /**
191
     * Get the DI container
192
     *
193
     * @return Container
194
     */
195
    public function di(): ?Container
196
    {
197
        return self::$xContainer;
198
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
199
200
    /**
201
     * Get the logger
202
     *
203
     * @return LoggerInterface
204
     */
205
    public function logger(): LoggerInterface
206
    {
207
        return $this->di()->logger();
208
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
209
210
    /**
211
     * Set the value of a config option
212
     *
213
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
214
     * @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...
215
     *
216
     * @return void
217
     */
218
    public function setOption(string $sName, $sValue)
219
    {
220
        $this->xConfigManager->setOption($sName, $sValue);
221
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
222
223
    /**
224
     * Get the value of a config option
225
     *
226
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
227
     * @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...
228
     *
229
     * @return mixed        The option value, or null if the option is unknown
230
     */
231
    public function getOption(string $sName, $xDefault = null)
232
    {
233
        return $this->xConfigManager->getOption($sName, $xDefault);
234
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
235
236
    /**
237
     * Check the presence of a config option
238
     *
239
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
240
     *
241
     * @return bool        True if the option exists, and false if not
242
     */
243
    public function hasOption(string $sName): bool
244
    {
245
        return $this->xConfigManager->hasOption($sName);
246
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
247
248
    /**
249
     * Read the options from the file, if provided, and return the config
250
     *
251
     * @param string $sConfigFile The full path to the config file
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
252
     * @param string $sConfigSection The section of the config file to be loaded
253
     *
254
     * @return ConfigManager
255
     * @throws SetupException
256
     */
257
    public function config(string $sConfigFile = '', string $sConfigSection = ''): ConfigManager
258
    {
259
        if(!empty(($sConfigFile = trim($sConfigFile))))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
260
        {
261
            $this->xConfigManager->load($sConfigFile, trim($sConfigSection));
262
        }
263
        return $this->xConfigManager;
264
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
265
266
    /**
267
     * Get a translated string
268
     *
269
     * @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...
270
     * @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...
271
     * @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...
272
     *
273
     * @return string
274
     */
275
    public function trans(string $sText, array $aPlaceHolders = [], string $sLanguage = ''): string
276
    {
277
        return $this->xTranslator->trans($sText, $aPlaceHolders, $sLanguage);
278
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
279
280
    /**
281
     * Get the Response object
282
     *
283
     * @return ResponseInterface
284
     */
285
    public function getResponse(): ResponseInterface
286
    {
287
        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...
288
        {
289
            return $xResponse;
290
        }
291
        return $this->di()->getResponse();
292
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
293
294
    /**
295
     * Create a new Jaxon response object
296
     *
297
     * @return Response
298
     */
299
    public function newResponse(): Response
300
    {
301
        return $this->di()->newResponse();
302
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
303
304
    /**
305
     * Register a plugin
306
     *
307
     * Below is a table for priorities and their description:
308
     * - 0 to 999: Plugins that are part of or extensions to the jaxon core
309
     * - 1000 to 8999: User created plugins, typically, these plugins don't care about order
310
     * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list
311
     *
312
     * @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...
313
     * @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...
314
     * @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...
315
     *
316
     * @return void
317
     * @throws SetupException
318
     */
319
    public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000)
320
    {
321
        $this->xPluginManager->registerPlugin($sClassName, $sPluginName, $nPriority);
322
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
323
324
    /**
325
     * Register a package
326
     *
327
     * @param string $sClassName    The package class
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
328
     * @param array $xPkgOptions    The user provided package 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...
329
     *
330
     * @return void
331
     * @throws SetupException
332
     */
333
    public function registerPackage(string $sClassName, array $xPkgOptions = [])
334
    {
335
        $this->xPackageManager->registerPackage($sClassName, $xPkgOptions);
336
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
337
338
    /**
339
     * Register request handlers, including functions, callable classes and directories.
340
     *
341
     * @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...
342
     *        Options include:
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 25 spaces but found 8
Loading history...
343
     *        - 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...
344
     *        - 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...
345
     *        - 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...
346
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
347
     *        When registering a function, this is the name of the function
348
     *        When registering a callable class, this is the class name
349
     *        When registering a callable directory, this is the full path to the directory
350
     * @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...
351
     *
352
     * @return void
353
     * @throws SetupException
354
     */
355
    public function register(string $sType, string $sName, $xOptions = [])
356
    {
357
        $this->xPluginManager->registerCallable($sType, $sName, $xOptions);
358
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
359
360
    /**
361
     * Get an instance of a registered class
362
     *
363
     * @param string $sClassName The class name
364
     *
365
     * @return null|object
366
     * @throws SetupException
367
     */
368
    public function instance(string $sClassName)
369
    {
370
        $xCallable = $this->xClassRegistry->getCallableObject($sClassName);
371
        return ($xCallable) ? $xCallable->getRegisteredObject() : null;
0 ignored issues
show
introduced by
$xCallable is of type Jaxon\Request\Plugin\CallableClass\CallableObject, thus it always evaluated to true.
Loading history...
372
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
373
374
    /**
375
     * Get the factory
376
     *
377
     * @return Factory
378
     */
379
    public function factory(): Factory
380
    {
381
        return $this->di()->g(Factory::class);
382
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
383
384
    /**
385
     * Get a request to a registered class
386
     *
387
     * @param string $sClassName The class name
388
     *
389
     * @return RequestFactory|null
390
     * @throws SetupException
391
     */
392
    public function request(string $sClassName = ''): ?RequestFactory
393
    {
394
        return $this->factory()->request($sClassName);
395
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
396
397
    /**
398
     * Returns the Jaxon Javascript header and wrapper code to be printed into the page
399
     *
400
     * The javascript code returned by this function is dependent on the plugins
401
     * that are included and the functions and classes that are registered.
402
     *
403
     * @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...
404
     * @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...
405
     *
406
     * @return string
407
     * @throws UriException
408
     */
409
    public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string
410
    {
411
        return $this->xCodeGenerator->getScript($bIncludeJs, $bIncludeCss);
412
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
413
414
    /**
415
     * Return the javascript header code and file includes
416
     *
417
     * @return string
418
     */
419
    public function getJs(): string
420
    {
421
        return $this->xCodeGenerator->getJs();
422
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
423
424
    /**
425
     * Return the CSS header code and file includes
426
     *
427
     * @return string
428
     */
429
    public function getCss(): string
430
    {
431
        return $this->xCodeGenerator->getCss();
432
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
433
434
    /**
435
     * Determine if a call is a jaxon request or a page load request
436
     *
437
     * @return bool
438
     */
439
    public function canProcessRequest(): bool
440
    {
441
        return $this->di()->getRequestHandler()->canProcessRequest();
442
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
443
444
    /**
445
     * If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser
446
     *
447
     * This is the main server side engine for Jaxon.
448
     * It handles all the incoming requests, including the firing of events and handling of the response.
449
     * If your RequestURI is the same as your web page, then this function should be called before ANY
450
     * headers or HTML is output from your script.
451
     *
452
     * This function may exit after the request is processed, if the 'core.process.exit' option is set to true.
453
     *
454
     * @return void
455
     *
456
     * @throws RequestException
457
     * @see <Jaxon\Jaxon->canProcessRequest>
458
     */
459
    public function processRequest()
460
    {
461
        // Check to see if headers have already been sent out, in which case we can't do our job
462
        if(headers_sent($sFilename, $nLineNumber))
463
        {
464
            $sMessage = $this->xTranslator->trans('errors.output.already-sent', [
465
                'location' => $sFilename . ':' . $nLineNumber
466
            ]) . "\n" . $this->xTranslator->trans('errors.output.advice');
467
            throw new RequestException($sMessage);
468
        }
469
470
        $this->di()->getRequestHandler()->processRequest();
471
472
        // Clean the processing buffer
473
        if(($this->xConfigManager->getOption('core.process.clean')))
474
        {
475
            $er = error_reporting(0);
476
            while(ob_get_level() > 0)
477
            {
478
                ob_end_clean();
479
            }
480
            error_reporting($er);
481
        }
482
        if($this->xConfigManager->getOption('core.response.send'))
483
        {
484
            $this->sendResponse();
485
        }
486
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
487
488
    /**
489
     * Prints the response to the output stream, thus sending the response to the browser
490
     *
491
     * @return void
492
     */
493
    public function sendResponse()
494
    {
495
        if(empty($sContent = $this->xResponseManager->getOutput()))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
496
        {
497
            return;
498
        }
499
        if($this->di()->getRequest()->getMethod() === 'GET')
500
        {
501
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
502
            header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
503
            header("Cache-Control: no-cache, must-revalidate");
504
            header("Pragma: no-cache");
505
        }
506
        header('content-type: ' . $this->xResponseManager->getContentType());
507
        print $sContent;
508
        if($this->xConfigManager->getOption('core.process.exit'))
509
        {
510
            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...
511
        }
512
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
513
514
    /**
515
     * Get a registered response plugin
516
     *
517
     * @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...
518
     *
519
     * @return ResponsePlugin|null
520
     */
521
    public function plugin(string $sName): ?ResponsePlugin
522
    {
523
        return $this->xPluginManager->getResponsePlugin($sName);
524
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
525
526
    /**
527
     * Get a package instance
528
     *
529
     * @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...
530
     *
531
     * @return Package|null
532
     */
533
    public function package(string $sClassName): ?Package
534
    {
535
        return $this->xPackageManager->getPackage($sClassName);
536
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
537
538
    /**
539
     * Get the upload plugin
540
     *
541
     * @return UploadHandler|null
542
     */
543
    public function upload(): ?UploadHandler
544
    {
545
        return $this->di()->getUploadHandler();
546
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
547
548
    /**
549
     * Get the request callback manager
550
     *
551
     * @return CallbackManager
552
     */
553
    public function callback(): CallbackManager
554
    {
555
        return $this->di()->getCallbackManager();
556
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
557
558
    /**
559
     * Get the template engine
560
     *
561
     * @return TemplateEngine
562
     */
563
    public function template(): TemplateEngine
564
    {
565
        return $this->di()->getTemplateEngine();
566
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
567
568
    /**
569
     * Get the App instance
570
     *
571
     * @return App
572
     */
573
    public function app(): App
574
    {
575
        return $this->di()->getApp();
576
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
577
578
    /**
579
     * Get the view renderer
580
     *
581
     * @return ViewRenderer
582
     */
583
    public function view(): ViewRenderer
584
    {
585
        return $this->di()->getViewRenderer();
586
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
587
588
    /**
589
     * Get the paginator
590
     *
591
     * @return Paginator
592
     */
593
    public function paginator(): Paginator
594
    {
595
        return $this->di()->getPaginator();
596
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
597
598
    /**
599
     * Get the session manager
600
     *
601
     * @return SessionInterface
602
     */
603
    public function session(): SessionInterface
604
    {
605
        return $this->di()->getSessionManager();
606
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
607
608
    /**
609
     * Reset the library and container instances
610
     *
611
     * @return void
612
     * @throws SetupException
613
     */
614
    public function reset()
615
    {
616
        self::$xInstance = null;
617
        self::$xContainer = null;
618
        // Need to register the default plugins.
619
        self::getInstance()->di()->getPluginManager()->registerPlugins();
620
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
621
}
622