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

Code::mergeJsCode()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 35
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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