Passed
Push — v5.x ( 57c2ac...73bdb8 )
by Thierry
16:58 queued 05:55
created

Ajax::paginator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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