Issues (2884)

src/App/Ajax.php (54 issues)

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