Passed
Push — master ( 65d999...598f6d )
by Thierry
02:20
created

Ajax   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 40
dl 0
loc 260
rs 10
c 1
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A newResponse() 0 3 1
A getInstance() 0 13 2
A getResponse() 0 3 1
A __construct() 0 2 1
A registerPlugin() 0 3 1
A psr() 0 3 1
A init() 0 9 1
A upload() 0 3 1
A config() 0 7 2
A register() 0 3 1
A processRequest() 0 12 3
A reset() 0 5 1
A view() 0 3 1
A app() 0 3 1
A registerPackage() 0 3 1
A paginator() 0 3 1
A template() 0 3 1
A dialog() 0 3 1
A session() 0 3 1
A getVersion() 0 3 1
1
<?php
2
3
/**
4
 * Ajax.php
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\App;
27
28
use Jaxon\App\Config\ConfigManager;
29
use Jaxon\App\Dialog\Library\DialogLibraryManager;
30
use Jaxon\App\I18n\Translator;
31
use Jaxon\App\Session\SessionInterface;
32
use Jaxon\App\Traits\AjaxSendTrait;
33
use Jaxon\App\Traits\AjaxTrait;
34
use Jaxon\App\View\ViewRenderer;
35
use Jaxon\Di\Container;
36
use Jaxon\Exception\RequestException;
37
use Jaxon\Exception\SetupException;
38
use Jaxon\Jaxon;
39
use Jaxon\Plugin\Manager\PluginManager;
40
use Jaxon\Request\Call\Paginator;
41
use Jaxon\Request\Factory\Psr\PsrFactory;
42
use Jaxon\Request\Handler\UploadHandler;
43
use Jaxon\Response\Manager\ResponseManager;
44
use Jaxon\Response\Response;
45
use Jaxon\Response\ResponseInterface;
46
use Jaxon\Utils\Template\TemplateEngine;
47
48
use function trim;
49
50
class Ajax
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Ajax
Loading history...
51
{
52
    use AjaxTrait;
53
    use AjaxSendTrait;
54
55
    /**
56
     * @var Ajax
57
     */
58
    private static $xInstance = null;
59
60
    /**
61
     * @var Bootstrap
62
     */
63
    protected $xBootstrap;
64
65
    /**
66
     * @param Container $xContainer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
67
     *
68
     * @return void
69
     */
70
    private function init(Container $xContainer)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
71
    {
72
        $this->xContainer = $xContainer;
73
        // Set the attributes from the container
74
        $this->xBootstrap = $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...
75
        $this->xTranslator = $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...
76
        $this->xConfigManager = $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...
77
        $this->xPluginManager = $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...
78
        $this->xResponseManager = $xContainer->g(ResponseManager::class);
79
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
80
81
    /**
82
     * @return Ajax
83
     */
84
    public static function getInstance(): Ajax
85
    {
86
        if(self::$xInstance === null)
87
        {
88
            // First call: create and initialize the instances.
89
            self::$xInstance = new Ajax();
90
            self::$xInstance->init(new Container(self::$xInstance));
91
            return self::$xInstance;
92
        }
93
94
        // Call the on boot callbacks on each call to the jaxon() function, except the first.
95
        self::$xInstance->xBootstrap->onBoot();
96
        return self::$xInstance;
97
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
98
99
    /**
100
     * The constructor
101
     */
102
    private function __construct()
103
    {}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
104
105
    /**
106
     * @return string
107
     */
108
    public function getVersion(): string
109
    {
110
        return Jaxon::VERSION;
111
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
112
113
    /**
114
     * Read the options from the file, if provided, and return the config
115
     *
116
     * @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...
117
     * @param string $sConfigSection The section of the config file to be loaded
118
     *
119
     * @return ConfigManager
120
     * @throws SetupException
121
     */
122
    public function config(string $sConfigFile = '', string $sConfigSection = ''): ConfigManager
123
    {
124
        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...
125
        {
126
            $this->xConfigManager->load($sConfigFile, trim($sConfigSection));
127
        }
128
        return $this->xConfigManager;
129
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
130
131
    /**
132
     * Get the global Response object
133
     *
134
     * @return ResponseInterface
135
     */
136
    public function getResponse(): ResponseInterface
137
    {
138
        return $this->xResponseManager->getResponse();
139
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
140
141
    /**
142
     * Create a new Jaxon response object
143
     *
144
     * @return Response
145
     */
146
    public function newResponse(): Response
147
    {
148
        return $this->di()->newResponse();
149
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
150
151
    /**
152
     * Register a plugin
153
     *
154
     * Below is a table for priorities and their description:
155
     * - 0 to 999: Plugins that are part of or extensions to the jaxon core
156
     * - 1000 to 8999: User created plugins, typically, these plugins don't care about order
157
     * - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list
158
     *
159
     * @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...
160
     * @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...
161
     * @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...
162
     *
163
     * @return void
164
     * @throws SetupException
165
     */
166
    public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000)
167
    {
168
        $this->xPluginManager->registerPlugin($sClassName, $sPluginName, $nPriority);
169
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
170
171
    /**
172
     * Register a package
173
     *
174
     * @param string $sClassName    The package class
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
175
     * @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...
176
     *
177
     * @return void
178
     * @throws SetupException
179
     */
180
    public function registerPackage(string $sClassName, array $xPkgOptions = [])
181
    {
182
        $this->di()->getPackageManager()->registerPackage($sClassName, $xPkgOptions);
183
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
184
185
    /**
186
     * @return UploadHandler|null
187
     */
188
    public function upload(): ?UploadHandler
189
    {
190
        return $this->di()->getUploadHandler();
191
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
192
193
    /**
194
     * Register request handlers, including functions, callable classes and directories.
195
     *
196
     * @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...
197
     *        Options include:
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 25 spaces but found 8
Loading history...
198
     *        - 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...
199
     *        - 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...
200
     *        - 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...
201
     * @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...
202
     *        When registering a function, this is the name of the function
203
     *        When registering a callable class, this is the class name
204
     *        When registering a callable directory, this is the full path to the directory
205
     * @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...
206
     *
207
     * @return void
208
     * @throws SetupException
209
     */
210
    public function register(string $sType, string $sName, $xOptions = [])
211
    {
212
        $this->xPluginManager->registerCallable($sType, $sName, $xOptions);
213
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
214
215
    /**
216
     * If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser
217
     *
218
     * This is the main server side engine for Jaxon.
219
     * It handles all the incoming requests, including the firing of events and handling of the response.
220
     * If your RequestURI is the same as your web page, then this function should be called before ANY
221
     * headers or HTML is output from your script.
222
     *
223
     * This function may exit after the request is processed, if the 'core.process.exit' option is set to true.
224
     *
225
     * @return void
226
     *
227
     * @throws RequestException
228
     * @see <AjaxTrait::canProcessRequest>
229
     */
230
    public function processRequest()
231
    {
232
        // Process the jaxon request
233
        $this->di()->getRequestHandler()->processRequest();
234
235
        if($this->xConfigManager->getOption('core.response.send'))
236
        {
237
            $this->sendResponse();
238
        }
239
        if($this->xConfigManager->getOption('core.process.exit'))
240
        {
241
            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...
242
        }
243
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
244
245
    /**
246
     * @return PsrFactory
247
     */
248
    public function psr(): PsrFactory
249
    {
250
        return $this->di()->getPsrFactory();
251
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
252
253
    /**
254
     * @return AppInterface
255
     */
256
    public function app(): AppInterface
257
    {
258
        return $this->di()->getApp();
259
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
260
261
    /**
262
     * @return TemplateEngine
263
     */
264
    public function template(): TemplateEngine
265
    {
266
        return $this->di()->getTemplateEngine();
267
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
268
269
    /**
270
     * @return ViewRenderer
271
     */
272
    public function view(): ViewRenderer
273
    {
274
        return $this->di()->getViewRenderer();
275
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
276
277
    /**
278
     * @return Paginator
279
     */
280
    public function paginator(): Paginator
281
    {
282
        return $this->di()->getPaginator();
283
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
284
285
    /**
286
     * @return DialogLibraryManager
287
     */
288
    public function dialog(): DialogLibraryManager
289
    {
290
        return $this->di()->getDialogLibraryManager();
291
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
292
293
    /**
294
     * @return SessionInterface|null
295
     */
296
    public function session(): ?SessionInterface
297
    {
298
        return $this->di()->getSessionManager();
299
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
300
301
    /**
302
     * @return void
303
     * @throws SetupException
304
     */
305
    public function reset()
306
    {
307
        self::$xInstance = null;
308
        // Need to register the default plugins.
309
        self::getInstance()->di()->getPluginManager()->registerPlugins();
310
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
311
}
312