Passed
Push — master ( cd350b...3a08d5 )
by Thierry
03:10
created

CodeGenerator::renderCodes()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
nc 4
nop 0
dl 0
loc 23
rs 9.7666
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * CodeGenerator.php - Jaxon code generator
5
 *
6
 * Generate HTML, CSS and Javascript code for Jaxon.
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 2016 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
Missing @category tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
14
15
namespace Jaxon\Plugin\Code;
16
17
use Jaxon\Jaxon;
18
use Jaxon\Plugin\Plugin;
19
use Jaxon\Utils\Http\UriException;
20
use Jaxon\Utils\Template\Engine as TemplateEngine;
21
22
use function ksort;
23
use function md5;
24
use function trim;
25
use function rtrim;
26
use function is_subclass_of;
27
28
class CodeGenerator
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CodeGenerator
Loading history...
29
{
30
    /**
31
     * @var Jaxon
32
     */
33
    private $jaxon;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
34
35
    /**
36
     * The Jaxon template engine
37
     *
38
     * @var TemplateEngine
39
     */
40
    protected $xTemplateEngine;
41
42
    /**
43
     * @var AssetManager
44
     */
45
    private $xAssetManager;
46
47
    /**
48
     * The class names of objects that generate code
49
     *
50
     * @var array<string>
51
     */
52
    protected $aClassNames = [];
53
54
    /**
55
     * @var string
56
     */
57
    protected $sJsOptions;
58
59
    /**
60
     * @var string
61
     */
62
    protected $sCss = '';
63
64
    /**
65
     * @var string
66
     */
67
    protected $sJs = '';
68
69
    /**
70
     * @var string
71
     */
72
    protected $sJsScript = '';
73
74
    /**
75
     * @var string
76
     */
77
    protected $sJsReadyScript = '';
78
79
    /**
80
     * @var string
81
     */
82
    protected $sJsInlineScript = '';
83
84
    /**
85
     * @var string
86
     */
87
    protected $bGenerated = false;
88
89
    /**
90
     * The constructor
91
     *
92
     * @param Jaxon $jaxon
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
93
     * @param TemplateEngine $xTemplateEngine
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
94
     * @param AssetManager $xAssetManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
95
     */
96
    public function __construct(Jaxon $jaxon, TemplateEngine $xTemplateEngine, AssetManager $xAssetManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
97
    {
98
        $this->jaxon = $jaxon;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
99
        $this->xTemplateEngine = $xTemplateEngine;
100
        $this->xAssetManager = $xAssetManager;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
101
        $this->sJsOptions = $xAssetManager->getJsOptions();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
102
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
103
104
    /**
105
     * Add a new generator to the list
106
     *
107
     * @param string $sClassName    The code generator class
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
108
     * @param int $nPriority    The desired priority, used to order the plugins
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
109
     *
110
     * @return void
111
     */
112
    public function addGenerator(string $sClassName, int $nPriority)
113
    {
114
        while(isset($this->aClassNames[$nPriority]))
115
        {
116
            $nPriority++;
117
        }
118
        $this->aClassNames[$nPriority] = $sClassName;
119
        // Sort the array by ascending keys
120
        ksort($this->aClassNames);
121
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
122
123
    /**
124
     * Generate a hash for all the javascript code generated by the library
125
     *
126
     * @return string
127
     */
128
    private function getHash(): string
129
    {
130
        $sHash = $this->jaxon->getVersion();
131
        foreach($this->aClassNames as $sClassName)
132
        {
133
            $xGenerator = $this->jaxon->di()->get($sClassName);
134
            $sHash .= $xGenerator->getHash();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
135
        }
136
        return md5($sHash);
137
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
138
139
    /**
140
     * Render a template in the 'plugins' subdir
141
     *
142
     * @param string $sTemplate    The template filename
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
143
     * @param array $aVars    The template variables
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
144
     *
145
     * @return string
146
     */
147
    private function render(string $sTemplate, array $aVars = []): string
148
    {
149
        $aVars['sJsOptions'] = $this->sJsOptions;
150
        return $this->xTemplateEngine->render("jaxon::plugins/$sTemplate", $aVars);
151
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
152
153
    /**
154
     * Generate the Jaxon CSS and js codes for a given plugin
155
     *
156
     * @param string $sClassName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
157
     *
158
     * @return void
159
     * @throws UriException
160
     */
161
    private function generatePluginCodes(string $sClassName)
162
    {
163
        $xGenerator = $this->jaxon->di()->get($sClassName);
164
        if(!is_subclass_of($xGenerator, Plugin::class) || $this->xAssetManager->shallIncludeAssets($xGenerator))
165
        {
166
            // HTML tags for CSS
167
            $this->sCss .= trim($xGenerator->getCss(), " \n") . "\n";
168
            // HTML tags for js
169
            $this->sJs .= trim($xGenerator->getJs(), " \n") . "\n";
170
        }
171
        // Javascript code
172
        $this->sJsScript .= trim($xGenerator->getScript(), " \n") . "\n";
173
        if($xGenerator->readyEnabled())
174
        {
175
            if($xGenerator->readyInlined())
176
            {
177
                // Ready code which must be inlined in HTML.
178
                $this->sJsInlineScript .= trim($xGenerator->getReadyScript(), " \n") . "\n";
179
            }
180
            else
181
            {
182
                // Ready code which can be exported to an external file.
183
                $this->sJsReadyScript .= trim($xGenerator->getReadyScript(), " \n") . "\n";
184
            }
185
        }
186
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
187
188
    /**
189
     * Render the generated CSS ans js codes
190
     *
191
     * @return void
192
     * @throws UriException
193
     */
194
    private function renderCodes()
195
    {
196
        $this->sCss = trim($this->sCss, " \n");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
197
        $this->sJs = trim($this->sJs, " \n");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
198
        $this->sJsScript = trim($this->sJsScript, " \n");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
199
        $this->sJsReadyScript = trim($this->sJsReadyScript, " \n");
200
        $this->sJsInlineScript = trim($this->sJsInlineScript, " \n");
201
        if(($this->sJsReadyScript))
202
        {
203
            $sScript = rtrim($this->sJsReadyScript, ";\n") . ";\n";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
204
            $this->sJsReadyScript = $this->render('ready.js', ['sScript' => $sScript]);
205
        }
206
        if(($this->sJsInlineScript))
207
        {
208
            $sScript = rtrim($this->sJsInlineScript, ";\n") . ";\n";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
209
            $this->sJsInlineScript = $this->render('ready.js', ['sScript' => $sScript]);
210
        }
211
        // Prepend Jaxon javascript files to HTML tags for Js
212
        $aJsFiles = $this->xAssetManager->getJsLibFiles();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
213
        $this->sJs = $this->render('includes.js', ['aUrls' => $aJsFiles]) . "\n" . $this->sJs;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
214
        $sJsConfigVars = $this->render('config.js', $this->xAssetManager->getOptionVars());
215
        // These three parts are always rendered together
216
        $this->sJsScript = $sJsConfigVars . "\n" . $this->sJsScript . "\n" . $this->sJsReadyScript;
217
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
218
219
    /**
220
     * Generate the Jaxon CSS ans js codes
221
     *
222
     * @return void
223
     * @throws UriException
224
     */
225
    private function generateCodes()
226
    {
227
        if($this->bGenerated)
228
        {
229
            return;
230
        }
231
232
        foreach($this->aClassNames as $sClassName)
233
        {
234
            $this->generatePluginCodes($sClassName);
235
        }
236
        $this->renderCodes();
237
238
        // The codes are already generated.
239
        $this->bGenerated = true;
0 ignored issues
show
Documentation Bug introduced by
The property $bGenerated was declared of type string, but true is of type true. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
240
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
241
242
    /**
243
     * Get the HTML tags to include Jaxon CSS code and files into the page
244
     *
245
     * @return string
246
     * @throws UriException
247
     */
248
    public function getCss(): string
249
    {
250
        $this->generateCodes();
251
        return $this->sCss;
252
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
253
254
    /**
255
     * Get the HTML tags to include Jaxon javascript files into the page
256
     *
257
     * @return string
258
     * @throws UriException
259
     */
260
    public function getJs(): string
261
    {
262
        $this->generateCodes();
263
        return $this->sJs;
264
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
265
266
    /**
267
     * Get the javascript code to be sent to the browser
268
     *
269
     * @param bool $bIncludeJs Also get the JS files
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
270
     * @param bool $bIncludeCss Also get the CSS files
271
     *
272
     * @return string
273
     * @throws UriException
274
     */
275
    public function getScript(bool $bIncludeJs, bool $bIncludeCss): string
276
    {
277
        $this->generateCodes();
278
        $sScript = '';
279
        if(($bIncludeCss))
280
        {
281
            $sScript .= $this->getCss() . "\n";
282
        }
283
        if(($bIncludeJs))
284
        {
285
            $sScript .= $this->getJs() . "\n";
286
        }
287
288
        if($this->xAssetManager->shallCreateJsFiles() &&
289
            ($sUrl = $this->xAssetManager->createJsFiles($this->getHash(), $this->sJsScript)))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
290
        {
291
            return $sScript . $this->render('include.js', ['sUrl' => $sUrl]) . "\n" .
292
                $this->render('wrapper.js', ['sScript' => $this->sJsInlineScript]);
293
        }
294
        return $sScript . $this->render('wrapper.js',
295
            ['sScript' => $this->sJsScript . "\n" . $this->sJsInlineScript]);
296
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
297
}
298