LibTrait::js()   A
last analyzed

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