Passed
Push — master ( 22a324...09a587 )
by Thierry
06:55 queued 03:38
created

CodeGenerator::getJsScript()   A

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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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\Di\Container;
18
use Jaxon\Plugin\Manager\PluginManager;
19
use Jaxon\Plugin\Plugin;
20
use Jaxon\Utils\Http\UriException;
21
use Jaxon\Utils\Template\TemplateEngine;
22
23
use function array_reduce;
24
use function md5;
25
use function trim;
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 string
32
     */
33
    private $sVersion;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
34
35
    /**
36
     * @var Container
37
     */
38
    private $di;
39
40
    /**
41
     * @var PluginManager
42
     */
43
    protected $xPluginManager;
44
45
    /**
46
     * The Jaxon template engine
47
     *
48
     * @var TemplateEngine
49
     */
50
    protected $xTemplateEngine;
51
52
    /**
53
     * @var AssetManager
54
     */
55
    private $xAssetManager;
56
57
    /**
58
     * @var string
59
     */
60
    protected $sJsOptions;
61
62
    /**
63
     * @var string
64
     */
65
    protected $sCss = '';
66
67
    /**
68
     * @var string
69
     */
70
    protected $sJs = '';
71
72
    /**
73
     * @var string
74
     */
75
    protected $sJsScript = '';
76
77
    /**
78
     * @var string
79
     */
80
    protected $sJsReadyScript = '';
81
82
    /**
83
     * @var string
84
     */
85
    protected $sJsInlineScript = '';
86
87
    /**
88
     * @var string
89
     */
90
    protected $bGenerated = false;
91
92
    /**
93
     * The constructor
94
     *
95
     * @param string $sVersion
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
96
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
97
     * @param PluginManager $xPluginManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
98
     * @param TemplateEngine $xTemplateEngine
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
99
     */
100
    public function __construct(string $sVersion, Container $di, PluginManager $xPluginManager, TemplateEngine $xTemplateEngine)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
101
    {
102
        $this->sVersion = $sVersion;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
103
        $this->di = $di;
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...
104
        $this->xPluginManager = $xPluginManager;
105
        $this->xTemplateEngine = $xTemplateEngine;
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * Generate a hash for all the javascript code generated by the library
110
     *
111
     * @return string
112
     */
113
    public function getHash(): string
114
    {
115
        return md5(array_reduce($this->xPluginManager->getCodeGenerators(), function($sHash, $sClassName) {
116
            return $sHash . $this->di->g($sClassName)->getHash();
117
        }, $this->sVersion));
118
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
119
120
    /**
121
     * Render a template in the 'plugins' subdir
122
     *
123
     * @param string $sTemplate    The template filename
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
124
     * @param array $aVars    The template variables
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
125
     *
126
     * @return string
127
     */
128
    private function render(string $sTemplate, array $aVars = []): string
129
    {
130
        $aVars['sJsOptions'] = $this->sJsOptions;
131
        return $this->xTemplateEngine->render("jaxon::plugins/$sTemplate", $aVars);
132
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
133
134
    /**
135
     * Generate the Jaxon CSS and js codes for a given plugin
136
     *
137
     * @param string $sClassName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
138
     *
139
     * @return void
140
     */
141
    private function generatePluginCodes(string $sClassName)
142
    {
143
        $xGenerator = $this->di->g($sClassName);
144
        if(!is_subclass_of($xGenerator, Plugin::class) || $this->xAssetManager->shallIncludeAssets($xGenerator))
145
        {
146
            // HTML tags for CSS
147
            $this->sCss = trim($this->sCss) . "\n" . trim($xGenerator->getCss(), " \n");
148
            // HTML tags for js
149
            $this->sJs = trim($this->sJs) . "\n" . trim($xGenerator->getJs(), " \n");
150
        }
151
        // Javascript code
152
        $this->sJsScript = trim($this->sJsScript) . "\n\n" . trim($xGenerator->getScript(), " \n");
153
        if($xGenerator->readyEnabled())
154
        {
155
            $sScriptAttr = $xGenerator->readyInlined() ? 'sJsInlineScript' : 'sJsReadyScript';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
156
            $this->$sScriptAttr = trim($this->$sScriptAttr) . "\n\n" . trim($xGenerator->getReadyScript(), " \n");
157
        }
158
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
159
160
    /**
161
     * Render the generated CSS ans js codes
162
     *
163
     * @return void
164
     */
165
    private function renderCodes()
166
    {
167
        $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...
168
        $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...
169
        $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...
170
        $this->sJsReadyScript = trim($this->sJsReadyScript, " \n");
171
        $this->sJsInlineScript = trim($this->sJsInlineScript, " \n");
172
        if(($this->sJsReadyScript))
173
        {
174
            $this->sJsReadyScript = $this->render('ready.js', ['sScript' => $this->sJsReadyScript . "\n"]);
175
        }
176
        if(($this->sJsInlineScript))
177
        {
178
            $this->sJsInlineScript = $this->render('ready.js', ['sScript' => $this->sJsInlineScript . "\n"]);
179
        }
180
        // Prepend Jaxon javascript files to HTML tags for Js
181
        $aJsFiles = $this->xAssetManager->getJsLibFiles();
182
        $this->sJs = trim($this->render('includes.js', ['aUrls' => $aJsFiles])) . "\n\n" . $this->sJs;
183
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
184
185
    /**
186
     * Generate the Jaxon CSS ans js codes
187
     *
188
     * @return void
189
     */
190
    private function generateCodes()
191
    {
192
        if($this->bGenerated)
193
        {
194
            return;
195
        }
196
197
        $this->xAssetManager = $this->di->getAssetManager();
198
        $this->sJsOptions = $this->xAssetManager->getJsOptions();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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
        foreach($this->xPluginManager->getCodeGenerators() as $sClassName)
200
        {
201
            $this->generatePluginCodes($sClassName);
202
        }
203
        $this->renderCodes();
204
205
        $sJsConfigVars = $this->render('config.js', $this->xAssetManager->getOptionVars());
206
        // These three parts are always rendered together
207
        $this->sJsScript = trim($sJsConfigVars) . "\n\n" .
208
            trim($this->sJsScript) . "\n\n" . trim($this->sJsReadyScript);
209
210
        // The codes are already generated.
211
        $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...
212
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
213
214
    /**
215
     * Get the HTML tags to include Jaxon CSS code and files into the page
216
     *
217
     * @return string
218
     */
219
    public function getCss(): string
220
    {
221
        $this->generateCodes();
222
        return $this->sCss;
223
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
224
225
    /**
226
     * Get the HTML tags to include Jaxon javascript files into the page
227
     *
228
     * @return string
229
     */
230
    public function getJs(): string
231
    {
232
        $this->generateCodes();
233
        return $this->sJs;
234
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
235
236
    /**
237
     * Get the generated javascript code
238
     *
239
     * @return string
240
     */
241
    public function getJsScript(): string
242
    {
243
        return $this->sJsScript;
244
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
245
246
    /**
247
     * Get the javascript code to be sent to the browser
248
     *
249
     * @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...
250
     * @param bool $bIncludeCss Also get the CSS files
251
     *
252
     * @return string
253
     * @throws UriException
254
     */
255
    public function getScript(bool $bIncludeJs, bool $bIncludeCss): string
256
    {
257
        $this->generateCodes();
258
        $sScript = '';
259
        if(($bIncludeCss))
260
        {
261
            $sScript .= $this->getCss() . "\n";
262
        }
263
        if(($bIncludeJs))
264
        {
265
            $sScript .= $this->getJs() . "\n";
266
        }
267
268
        if(!($sUrl = $this->xAssetManager->createJsFiles($this)))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
269
        {
270
            return trim($sScript) . "\n\n" . $this->render('wrapper.js',
271
                ['sScript' => trim($this->sJsScript) . "\n\n" . trim($this->sJsInlineScript)]);
272
        }
273
274
        return trim($sScript) . "\n\n" . trim($this->render('include.js', ['sUrl' => $sUrl])) .
275
            "\n\n" . $this->render('wrapper.js', ['sScript' => $this->sJsInlineScript]);
276
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
277
}
278