Passed
Push — main ( 82104f...ee6fb2 )
by Thierry
05:16
created

ConfigScriptGenerator::getScript()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
nc 4
nop 0
dl 0
loc 21
rs 9.7
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * ConfigScriptGenerator.php
5
 *
6
 * Generate the config script for Jaxon.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2025 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\Plugin\Code;
16
17
use Jaxon\App\Config\ConfigManager;
18
use Jaxon\Plugin\AbstractCodeGenerator;
19
use Jaxon\Plugin\JsCode;
20
use Jaxon\Plugin\JsCodeGeneratorInterface;
21
use Jaxon\Request\Handler\ParameterReader;
22
use Jaxon\Utils\Http\UriException;
23
use Jaxon\Utils\Template\TemplateEngine;
24
25
class ConfigScriptGenerator extends AbstractCodeGenerator implements JsCodeGeneratorInterface
26
{
27
    /**
28
     * The constructor
29
     *
30
     * @param ParameterReader $xParameterReader
31
     * @param TemplateEngine $xTemplateEngine
32
     * @param ConfigManager $xConfigManager
33
     */
34
    public function __construct(private ParameterReader $xParameterReader,
35
        private TemplateEngine $xTemplateEngine, private ConfigManager $xConfigManager)
36
    {}
37
38
    /**
39
     * Get the value of a config option
40
     *
41
     * @param string $sName The option name
42
     *
43
     * @return mixed
44
     */
45
    private function option(string $sName): mixed
46
    {
47
        return $this->xConfigManager->getOption($sName);
48
    }
49
50
    /**
51
     * @inheritDoc
52
     * @throws UriException
53
     */
54
    public function getJsCode(): JsCode
55
    {
56
        // It is important to call $this->xParameterReader->uri() only if necessary.
57
        $sUri = $this->option('core.request.uri') ?: $this->xParameterReader->uri();
58
        $aOptions = [
59
            'sResponseType'      => 'JSON',
60
            'sVersion'           => $this->option('core.version'),
61
            'sLanguage'          => $this->option('core.language'),
62
            'sRequestURI'        => $sUri,
63
            'sDefaultMode'       => $this->option('core.request.mode'),
64
            'sDefaultMethod'     => $this->option('core.request.method'),
65
            'sCsrfMetaName'      => $this->option('core.request.csrf_meta'),
66
            'bLoggingEnabled'    => $this->xConfigManager->loggingEnabled(),
67
            'bDebug'             => $this->option('core.debug.on'),
68
            'bVerboseDebug'      => $this->option('core.debug.verbose'),
69
            'sDebugOutputID'     => $this->option('core.debug.output_id'),
70
            'nResponseQueueSize' => $this->option('js.lib.queue_size'),
71
            'sStatusMessages'    => $this->option('js.lib.show_status') ? 'true' : 'false',
72
            'sWaitCursor'        => $this->option('js.lib.show_cursor') ? 'true' : 'false',
73
        ];
74
        $sJsCode = $this->xTemplateEngine->render('jaxon::plugins/config.js', $aOptions);
75
76
        return new JsCode(sCodeBefore: $sJsCode);
77
    }
78
}
79