Completed
Push — master ( f42a52...0a9499 )
by Thierry
02:50 queued 01:16
created

Jaxon::newResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
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
14
 * @author Jared White
15
 * @author J. Max Wilson
16
 * @author Joseph Woolley
17
 * @author Steffen Konerow
18
 * @author Thierry Feuzeu <[email protected]>
19
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
20
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
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
 */
25
26
namespace Jaxon;
27
28
use Jaxon\Plugin\Plugin;
29
use Jaxon\Utils\DI\Container;
30
use Jaxon\Utils\Config\Reader as ConfigReader;
31
32
class Jaxon
33
{
34
    use Features\Config;
35
36
    /**
37
     * Package version number
38
     *
39
     * @var string
40
     */
41
    private $sVersion = 'Jaxon 3.0.0';
42
43
    /*
44
     * Processing events
45
     */
46
    const PROCESSING_EVENT = 'ProcessingEvent';
47
    const PROCESSING_EVENT_BEFORE = 'BeforeProcessing';
48
    const PROCESSING_EVENT_AFTER = 'AfterProcessing';
49
    const PROCESSING_EVENT_INVALID = 'InvalidRequest';
50
    const PROCESSING_EVENT_ERROR = 'ProcessingError';
51
52
    /*
53
     * Request plugins
54
     */
55
    const CALLABLE_CLASS = 'CallableClass';
56
    const CALLABLE_DIR = 'CallableDir';
57
    const CALLABLE_FUNCTION = 'CallableFunction';
58
    // For uploaded files.
59
    const FILE_UPLOAD = 'FileUpload';
60
    // For compatibility with previous versions
61
    const CALLABLE_OBJECT = 'CallableClass'; // Same as CALLABLE_CLASS
62
    const USER_FUNCTION = 'CallableFunction'; // Same as CALLABLE_FUNCTION
63
64
    /**
65
     * A static instance on this class
66
     *
67
     * @var Jaxon
68
     */
69
    private static $xInstance = null;
70
71
    /**
72
     * The DI container
73
     *
74
     * @var Container
75
     */
76
    private static $xContainer = null;
77
78
    /**
79
     * Get the static instance
80
     *
81
     * @return Jaxon
82
     */
83
    public static function getInstance()
84
    {
85
        if(self::$xInstance == null)
86
        {
87
            self::$xInstance = new Jaxon();
88
        }
89
        return self::$xInstance;
90
    }
91
92
    /**
93
     * The constructor
94
     */
95
    public function __construct()
96
    {
97
        if(self::$xContainer == null)
98
        {
99
            self::$xContainer = new Container();
100
        }
101
        $this->setDefaultOptions();
102
    }
103
104
    /**
105
     * Get the DI container
106
     *
107
     * @return Container
108
     */
109
    public function di()
110
    {
111
        return self::$xContainer;
112
    }
113
114
    /**
115
     * The current Jaxon version
116
     *
117
     * @return string
118
     */
119
    public function getVersion()
120
    {
121
        return $this->sVersion;
122
    }
123
124
    /**
125
     * Get the config reader
126
     *
127
     * @return ConfigReader
128
     */
129
    public function config()
130
    {
131
        return $this->di()->get(ConfigReader::class);
132
    }
133
134
    /**
135
     * Set the default options of all components of the library
136
     *
137
     * @return void
138
     */
139
    private function setDefaultOptions()
140
    {
141
        // The default configuration settings.
142
        $this->di()->getConfig()->setOptions([
143
            'core.version'                      => $this->getVersion(),
144
            'core.language'                     => 'en',
145
            'core.encoding'                     => 'utf-8',
146
            'core.decode_utf8'                  => false,
147
            'core.prefix.function'              => 'jaxon_',
148
            'core.prefix.class'                 => 'Jaxon',
149
            // 'core.request.uri'               => '',
150
            'core.request.mode'                 => 'asynchronous',
151
            'core.request.method'               => 'POST',    // W3C: Method is case sensitive
152
            'core.response.send'                => true,
153
            'core.response.merge.ap'            => true,
154
            'core.response.merge.js'            => true,
155
            'core.debug.on'                     => false,
156
            'core.debug.verbose'                => false,
157
            'core.process.exit'                 => true,
158
            'core.process.clean'                => false,
159
            'core.process.timeout'              => 6000,
160
            'core.error.handle'                 => false,
161
            'core.error.log_file'               => '',
162
            'core.jquery.no_conflict'           => false,
163
            'js.lib.output_id'                  => 0,
164
            'js.lib.queue_size'                 => 0,
165
            'js.lib.load_timeout'               => 2000,
166
            'js.lib.show_status'                => false,
167
            'js.lib.show_cursor'                => true,
168
            'js.app.dir'                        => '',
169
            'js.app.minify'                     => true,
170
            'js.app.options'                    => '',
171
        ]);
172
    }
173
174
    /**
175
     * Get the Global Response object
176
     *
177
     * @return \Jaxon\Response\Response
178
     */
179
    public function getResponse()
180
    {
181
        return $this->di()->getResponse();
182
    }
183
184
    /**
185
     * Create a new Jaxon response object
186
     *
187
     * @return \Jaxon\Response\Response
188
     */
189
    public function newResponse()
190
    {
191
        return $this->di()->newResponse();
192
    }
193
    /**
194
     * Register a plugin
195
     *
196
     * Below is a table for priorities and their description:
197
     * - 0 thru 999: Plugins that are part of or extensions to the jaxon core
198
     * - 1000 thru 8999: User created plugins, typically, these plugins don't care about order
199
     * - 9000 thru 9999: Plugins that generally need to be last or near the end of the plugin list
200
     *
201
     * @param Plugin    $xPlugin        An instance of a plugin
202
     * @param integer   $nPriority      The plugin priority, used to order the plugins
203
     *
204
     * @return void
205
     */
206
    public function registerPlugin(Plugin $xPlugin, $nPriority = 1000)
207
    {
208
        $this->di()->getPluginManager()->registerPlugin($xPlugin, $nPriority);
209
    }
210
211
    /**
212
     * Register request handlers, including functions, callable classes and directories.
213
     *
214
     * @param string        $sType            The type of request handler being registered
215
     *        Options include:
216
     *        - Jaxon::USER_FUNCTION: a function declared at global scope
217
     *        - Jaxon::CALLABLE_CLASS: a class who's methods are to be registered
218
     *        - Jaxon::CALLABLE_DIR: a directory containing classes to be registered
219
     *        - Jaxon::PROCESSING_EVENT:
220
     * @param string        $sCallable| $sEvent
0 ignored issues
show
Documentation introduced by
There is no parameter named $sCallable|. Did you maybe mean $sCallable?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
221
     *        When registering a function, this is the name of the function
222
     *        When registering a callable class, this is the class name
223
     *        When registering a callable directory, this is the full path to the directory
224
     *        When registering an event, this is the event name
225
     * @param array|string|Closure  $aOptions | $sIncludeFile | $sNamespace
0 ignored issues
show
Bug introduced by
There is no parameter named $aOptions. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
226
     *        When registering a function, this is an (optional) array
227
     *             of call options, or the (optional) include file
228
     *        When registering a callable class, this is an (optional) array
229
     *             of call options for the class methods
230
     *        When registering a callable directory, this is an (optional) array
231
     *             of call options for the class methods, or the (optional) namespace
232
     *
233
     * @return mixed
234
     */
235
    public function register($sType, $sCallable, $xOptions = [])
236
    {
237
        if($sType == Jaxon::PROCESSING_EVENT)
238
        {
239
            $sEvent = $sCallable;
240
            $xCallback = $xOptions;
241
            switch($sEvent)
242
            {
243
            case Jaxon::PROCESSING_EVENT_BEFORE:
244
                $this->callback()->before($xCallback);
245
                break;
246
            case Jaxon::PROCESSING_EVENT_AFTER:
247
                $this->callback()->after($xCallback);
248
                break;
249
            case Jaxon::PROCESSING_EVENT_INVALID:
250
                $this->callback()->invalid($xCallback);
251
                break;
252
            case Jaxon::PROCESSING_EVENT_ERROR:
253
                $this->callback()->error($xCallback);
254
                break;
255
            default:
256
                break;
257
            }
258
            return;
259
        }
260
        return $this->di()->getPluginManager()->registerCallable($sType, $sCallable, $xOptions);
261
    }
262
263
    /**
264
     * Get an instance of a registered class
265
     *
266
     * @param string        $sClass             The class name
0 ignored issues
show
Documentation introduced by
There is no parameter named $sClass. Did you maybe mean $sClassName?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
267
     *
268
     * @return mixed
269
     */
270
    public function instance($sClassName)
271
    {
272
        $xCallable = $this->di()->getCallableRepository()->getCallableObject($sClassName);
273
        return ($xCallable) ? $xCallable->getRegisteredObject() : null;
274
    }
275
276
    /**
277
     * Get a request to a registered class
278
     *
279
     * @param string        $sClass             The class name
0 ignored issues
show
Documentation introduced by
There is no parameter named $sClass. Did you maybe mean $sClassName?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
280
     *
281
     * @return \Jaxon\Request\Factory\CallableClass\Request
282
     */
283
    public function request($sClassName)
284
    {
285
        $xInstance = $this->instance($sClassName);
286
        return ($xInstance) ? $xInstance->rq() : null;
287
    }
288
289
    /**
290
     * Returns the Jaxon Javascript header and wrapper code to be printed into the page
291
     *
292
     * The javascript code returned by this function is dependent on the plugins
293
     * that are included and the functions and classes that are registered.
294
     *
295
     * @param boolean        $bIncludeJs            Also get the JS files
296
     * @param boolean        $bIncludeCss        Also get the CSS files
297
     *
298
     * @return string
299
     */
300
    public function getScript($bIncludeJs = false, $bIncludeCss = false)
301
    {
302
        return $this->di()->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss);
303
    }
304
305
    /**
306
     * Print the jaxon Javascript header and wrapper code into your page
307
     *
308
     * The javascript code returned by this function is dependent on the plugins
309
     * that are included and the functions and classes that are registered.
310
     *
311
     * @param boolean        $bIncludeJs            Also print the JS files
312
     * @param boolean        $bIncludeCss        Also print the CSS files
313
     *
314
     * @return void
315
     */
316
    public function printScript($bIncludeJs = false, $bIncludeCss = false)
317
    {
318
        print $this->getScript($bIncludeJs, $bIncludeCss);
319
    }
320
321
    /**
322
     * Return the javascript header code and file includes
323
     *
324
     * @return string
325
     */
326
    public function getJs()
327
    {
328
        return $this->di()->getCodeGenerator()->getJs();
329
    }
330
331
    /**
332
     * Return the CSS header code and file includes
333
     *
334
     * @return string
335
     */
336
    public function getCss()
337
    {
338
        return $this->di()->getCodeGenerator()->getCss();
339
    }
340
341
    /**
342
     * Determine if a call is a jaxon request or a page load request
343
     *
344
     * @return boolean
345
     */
346
    public function canProcessRequest()
347
    {
348
        return $this->di()->getRequestHandler()->canProcessRequest();
349
    }
350
351
    /**
352
     * If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser
353
     *
354
     * This is the main server side engine for Jaxon.
355
     * It handles all the incoming requests, including the firing of events and handling of the response.
356
     * If your RequestURI is the same as your web page, then this function should be called before ANY
357
     * headers or HTML is output from your script.
358
     *
359
     * This function may exit after the request is processed, if the 'core.process.exit' option is set to true.
360
     *
361
     * @return void
362
     *
363
     * @see <Jaxon\Jaxon->canProcessRequest>
364
     */
365
    public function processRequest()
366
    {
367
        return $this->di()->getRequestHandler()->processRequest();
368
    }
369
370
    /**
371
     * Get a registered response plugin
372
     *
373
     * @param string        $sName                The name of the plugin
374
     *
375
     * @return \Jaxon\Plugin\Response
376
     */
377
    public function plugin($sName)
378
    {
379
        return $this->di()->getPluginManager()->getResponsePlugin($sName);
380
    }
381
382
    /**
383
     * Get the upload plugin
384
     *
385
     * @return \Jaxon\Request\Plugin\FileUpload
386
     */
387
    public function upload()
388
    {
389
        return $this->di()->getPluginManager()->getRequestPlugin(self::FILE_UPLOAD);
390
    }
391
392
    /**
393
     * Get the request callback manager
394
     *
395
     * @return \Jaxon\Request\Handler\Callback
396
     */
397
    public function callback()
398
    {
399
        return $this->di()->getRequestHandler()->getCallbackManager();
400
    }
401
402
    /**
403
     * Get the dialog wrapper
404
     *
405
     * @return \Jaxon\Utils\Dialogs\Dialog
406
     */
407
    public function dialog()
408
    {
409
        return $this->di()->getDialog();
410
    }
411
412
    /**
413
     * Get the template engine
414
     *
415
     * @return \Jaxon\Utils\Template\Engine
416
     */
417
    public function template()
418
    {
419
        return $this->di()->getTemplateEngine();
420
    }
421
422
    /**
423
     * Get the App instance
424
     *
425
     * @return \Jaxon\App\App
426
     */
427
    public function app()
428
    {
429
        return $this->di()->getApp();
430
    }
431
432
    /**
433
     * Get the view renderer
434
     *
435
     * @return Jaxon\Utils\View\Renderer
436
     */
437
    public function view()
438
    {
439
        return $this->di()->getViewRenderer();
440
    }
441
442
    /**
443
     * Get the session manager
444
     *
445
     * @return Jaxon\Contracts\Session
446
     */
447
    public function session()
448
    {
449
        return $this->di()->getSessionManager();
450
    }
451
}
452