Code::mergeJsCode()   B
last analyzed

Complexity

Conditions 8
Paths 24

Size

Total Lines 40
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 17
c 1
b 0
f 0
nc 24
nop 3
dl 0
loc 40
rs 8.4444
1
<?php
2
3
/**
4
 * Code.php
5
 *
6
 * Javascript and CSS codes generated by a Jaxon plugin.
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\Plugin\CodeGeneratorInterface;
18
use Jaxon\Plugin\CssCodeGeneratorInterface;
19
use Jaxon\Plugin\JsCodeGeneratorInterface;
20
use Closure;
21
22
use function is_array;
23
use function is_string;
24
25
class Code
26
{
27
    /**
28
     * @var array
29
     */
30
    protected $aCssTags = [];
31
32
    /**
33
     * @var array
34
     */
35
    protected $aCssCodes = [];
36
37
    /**
38
     * @var array
39
     */
40
    protected $aJsTags = [];
41
42
    /**
43
     * @var array
44
     */
45
    protected $aJsCodes = [];
46
47
    /**
48
     * @var array
49
     */
50
    protected $aJsCodesBefore = [];
51
52
    /**
53
     * @var array
54
     */
55
    protected $aJsCodesAfter = [];
56
57
    /**
58
     * @var string
59
     */
60
    protected CONST TRIM = " \t\r\n";
61
62
    /**
63
     * @return array
64
     */
65
    public function cssTags(): array
66
    {
67
        return $this->aCssTags;
68
    }
69
70
    /**
71
     * @param string $sTag
72
     *
73
     * @return void
74
     */
75
    public function addCssTag(string $sTag): void
76
    {
77
        $this->aCssTags[] = $sTag;
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function cssCodes(): array
84
    {
85
        return $this->aCssCodes;
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function jsTags(): array
92
    {
93
        return $this->aJsTags;
94
    }
95
96
    /**
97
     * @param string $sTag
98
     *
99
     * @return void
100
     */
101
    public function addJsTag(string $sTag): void
102
    {
103
        $this->aJsTags[] = $sTag;
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function jsCodes(): array
110
    {
111
        return [
112
            ...$this->aJsCodesBefore,
113
            ...$this->aJsCodes,
114
            ...$this->aJsCodesAfter,
115
        ];
116
    }
117
118
    /**
119
     * @param CodeGeneratorInterface $xCodeGenerator
120
     *
121
     * @return void
122
     */
123
    public function mergeCode(CodeGeneratorInterface $xGenerator, bool $bIncludeAssets): void
124
    {
125
        if($bIncludeAssets)
126
        {
127
            // HTML tags for CSS
128
            if(($sCssTags = trim($xGenerator->getCss(), self::TRIM)) !== '')
129
            {
130
                $this->aCssTags[] = $sCssTags;
131
            }
132
            // HTML tags for js
133
            if(($sJsTags = trim($xGenerator->getJs(), self::TRIM)) !== '')
134
            {
135
                $this->aJsTags[] = $sJsTags;
136
            }
137
        }
138
        // Javascript code
139
        if(($sJsScript = trim($xGenerator->getScript(), self::TRIM)) !== '')
140
        {
141
            $this->aJsCodes[] = $sJsScript;
142
        }
143
    }
144
145
    /**
146
     * @param CodeGeneratorInterface $xCodeGenerator
147
     * @param Closure $renderTag
148
     *
149
     * @return void
150
     */
151
    public function mergeCssCode(CssCodeGeneratorInterface $xGenerator,
152
        Closure $renderTag, bool $bIncludeAssets): void
153
    {
154
        $xCode = $xGenerator->getCssCode();
155
        if($bIncludeAssets)
156
        {
157
            // CSS html tags
158
            if(($aCssHtml = trim($xCode->html(), self::TRIM)) !== '')
159
            {
160
                $this->aCssTags[] = $aCssHtml;
161
            }
162
            // HTML tags for CSS
163
            foreach($xCode->urls() as $xUrl)
164
            {
165
                $aCssFile = match(true) {
166
                    is_string($xUrl) => ['uri' => $xUrl, 'options' => []],
167
                    is_array($xUrl) => $xUrl,
168
                    default => null,
169
                };
170
                if($aCssFile !== null)
171
                {
172
                    // Save the HTML tag for the file.
173
                    $this->aCssTags[] = $renderTag($aCssFile);
174
                }
175
            }
176
        }
177
        // CSS code
178
        if(($aCssCode = trim($xCode->code(), self::TRIM)) !== '')
179
        {
180
            $this->aCssCodes[] = $aCssCode;
181
        }
182
    }
183
184
    /**
185
     * @param CodeGeneratorInterface $xCodeGenerator
186
     * @param Closure $renderTag
187
     *
188
     * @return void
189
     */
190
    public function mergeJsCode(JsCodeGeneratorInterface $xGenerator,
191
        Closure $renderTag, bool $bIncludeAssets): void
192
    {
193
        $xCode = $xGenerator->getJsCode();
194
        if($bIncludeAssets)
195
        {
196
            // Javascript html tags
197
            if(($aJsHtml = trim($xCode->html(), self::TRIM)) !== '')
198
            {
199
                $this->aJsTags[] = $aJsHtml;
200
            }
201
            // HTML tags for js
202
            foreach($xCode->urls() as $xUrl)
203
            {
204
                $aJsFile = match(true) {
205
                    is_string($xUrl) => ['uri' => $xUrl, 'options' => []],
206
                    is_array($xUrl) => $xUrl,
207
                    default => null,
208
                };
209
                if($aJsFile !== null)
210
                {
211
                    // Save the HTML tag for the file.
212
                    $this->aJsTags[] = $renderTag($aJsFile);
213
                }
214
            }
215
        }
216
        // Javascript codes
217
        if(($aJsCode = trim($xCode->code(), self::TRIM)) !== '')
218
        {
219
            $this->aJsCodes[] = $aJsCode;
220
        }
221
        // Javascript codes before the main code
222
        if(($aJsCode = trim($xCode->before(), self::TRIM)) !== '')
223
        {
224
            $this->aJsCodesBefore[] = $aJsCode;
225
        }
226
        // Javascript codes after the main code
227
        if(($aJsCode = trim($xCode->after(), self::TRIM)) !== '')
228
        {
229
            $this->aJsCodesAfter[] = $aJsCode;
230
        }
231
    }
232
}
233