Passed
Push — master ( 882e76...c8b5d2 )
by Thierry
10:05
created

AjaxTrait::ajaxResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Ajax.php
5
 *
6
 * The Jaxon library.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
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...
14
15
namespace Jaxon\App\Traits;
16
17
use Jaxon\App\Config\ConfigManager;
18
use Jaxon\App\I18n\Translator;
19
use Jaxon\Di\Container;
20
use Jaxon\Exception\SetupException;
21
use Jaxon\Plugin\Manager\PluginManager;
22
use Jaxon\Plugin\Package;
23
use Jaxon\Plugin\ResponsePlugin;
24
use Jaxon\Request\Factory\Factory;
25
use Jaxon\Request\Factory\RequestFactory;
26
use Jaxon\Request\Handler\CallbackManager;
27
use Jaxon\Response\Manager\ResponseManager;
28
use Jaxon\Utils\Http\UriException;
29
use Psr\Log\LoggerInterface;
30
31
use function trim;
32
33
trait AjaxTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait AjaxTrait
Loading history...
34
{
35
    /**
36
     * @var Container
37
     */
38
    protected $xContainer = null;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
39
40
    /**
41
     * @var ConfigManager
42
     */
43
    protected $xConfigManager;
44
45
    /**
46
     * @var ResponseManager
47
     */
48
    protected $xResponseManager;
49
50
    /**
51
     * @var PluginManager
52
     */
53
    protected $xPluginManager;
54
55
    /**
56
     * @var Translator
57
     */
58
    protected $xTranslator;
59
60
    /**
61
     * @return Container
62
     */
63
    public function di(): ?Container
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
64
    {
65
        return $this->xContainer;
66
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
67
68
    /**
69
     * @return LoggerInterface
70
     */
71
    public function logger(): LoggerInterface
72
    {
73
        return $this->di()->getLogger();
74
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
75
76
    /**
77
     * @return Translator
78
     */
79
    public function translator(): Translator
80
    {
81
        return $this->xTranslator;
82
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
83
84
    /**
85
     * Set the value of a config option
86
     *
87
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
88
     * @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...
89
     *
90
     * @return void
91
     */
92
    public function setOption(string $sName, $sValue)
93
    {
94
        $this->xConfigManager->setOption($sName, $sValue);
95
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
96
97
    /**
98
     * Get the value of a config option
99
     *
100
     * @param string $sName    The option name
101
     * @param mixed $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...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
102
     *
103
     * @return mixed
104
     */
105
    public function getOption(string $sName, $xDefault = null)
106
    {
107
        return $this->xConfigManager->getOption($sName, $xDefault);
108
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
109
110
    /**
111
     * Check the presence of a config option
112
     *
113
     * @param string $sName    The option name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
114
     *
115
     * @return bool
116
     */
117
    public function hasOption(string $sName): bool
118
    {
119
        return $this->xConfigManager->hasOption($sName);
120
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
121
122
    /**
123
     * Get the configured character encoding
124
     *
125
     * @return string
126
     */
127
    public function getCharacterEncoding(): string
128
    {
129
        return trim($this->getOption('core.encoding', ''));
130
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
131
132
    /**
133
     * Get the content type of the HTTP response
134
     *
135
     * @return string
136
     */
137
    public function getContentType(): string
138
    {
139
        return $this->xResponseManager->getContentType();
140
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
141
142
    /**
143
     * Get the factory for request and parameter factories
144
     *
145
     * @return Factory
146
     */
147
    public function factory(): Factory
148
    {
149
        return $this->di()->getFactory();
150
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
151
152
    /**
153
     * Get a request to a registered class
154
     *
155
     * @param string $sClassName The class name
156
     *
157
     * @return RequestFactory|null
158
     * @throws SetupException
159
     */
160
    public function request(string $sClassName = ''): ?RequestFactory
161
    {
162
        return $this->factory()->request($sClassName);
163
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
164
165
    /**
166
     * Get the HTML tags to include Jaxon javascript files into the page.
167
     *
168
     * @return string
169
     */
170
    public function getJs(): string
171
    {
172
        return $this->di()->getCodeGenerator()->getJs();
173
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
174
175
    /**
176
     * Get the HTML tags to include Jaxon javascript files into the page.
177
     *
178
     * @return string  the javascript code
179
     */
180
    public function js(): string
181
    {
182
        return $this->di()->getCodeGenerator()->getJs();
183
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
184
185
    /**
186
     * Get the HTML tags to include Jaxon CSS code and files into the page.
187
     *
188
     * @return string
189
     */
190
    public function getCss(): string
191
    {
192
        return $this->di()->getCodeGenerator()->getCss();
193
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
194
195
    /**
196
     * Get the HTML tags to include Jaxon CSS code and files into the page.
197
     *
198
     * @return string
199
     */
200
    public function css(): string
201
    {
202
        return $this->di()->getCodeGenerator()->getCss();
203
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
204
205
    /**
206
     * Returns the js header and wrapper code to be printed into the page
207
     *
208
     * The javascript code returned by this function depends on the plugins
209
     * that are included and the functions and classes that are registered.
210
     *
211
     * @param bool $bIncludeJs    Also get the js code
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
212
     * @param bool $bIncludeCss    Also get the css code
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
213
     *
214
     * @return string
215
     * @throws UriException
216
     */
217
    public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string
218
    {
219
        return $this->di()->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss);
220
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
221
222
    /**
223
     * Returns the js header and wrapper code to be printed into the page
224
     *
225
     * @param bool $bIncludeJs    Also get the js code
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
226
     * @param bool $bIncludeCss    Also get the css code
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
227
     *
228
     * @return string  the javascript code
229
     * @throws UriException
230
     */
231
    public function script(bool $bIncludeJs = false, bool $bIncludeCss = false): string
232
    {
233
        return $this->di()->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss);
234
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
235
236
    /**
237
     * Determine if a call is a jaxon request or a page load request
238
     *
239
     * @return bool
240
     */
241
    public function canProcessRequest(): bool
242
    {
243
        return $this->di()->getRequestHandler()->canProcessRequest();
244
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
245
246
    /**
247
     * Get a registered response plugin
248
     *
249
     * @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...
250
     *
251
     * @return ResponsePlugin|null
252
     */
253
    public function plugin(string $sName): ?ResponsePlugin
254
    {
255
        return $this->xPluginManager->getResponsePlugin($sName);
256
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
257
258
    /**
259
     * Get a package instance
260
     *
261
     * @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...
262
     *
263
     * @return Package|null
264
     */
265
    public function package(string $sClassName): ?Package
266
    {
267
        return $this->di()->getPackageManager()->getPackage($sClassName);
268
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
269
270
    /**
271
     * Get the callback manager
272
     *
273
     * @return CallbackManager
274
     */
275
    public function callback(): CallbackManager
276
    {
277
        return $this->di()->getCallbackManager();
278
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
279
}
280