Completed
Push — master ( 6db9a9...7e342e )
by
unknown
18:05
created

ScriptViewHelper::injectAssetCollector()   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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
namespace TYPO3\CMS\Fluid\ViewHelpers\Asset;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
use TYPO3\CMS\Core\Page\AssetCollector;
19
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
20
21
/**
22
 * ScriptViewHelper
23
 *
24
 * Examples
25
 * ========
26
 *
27
 * ::
28
 *
29
 *    <f:asset.script identifier="identifier123" src="EXT:my_ext/Resources/Public/JavaScript/foo.js" />
30
 *    <f:asset.script identifier="identifier123">
31
 *       alert('hello world');
32
 *    </f:asset.script>
33
 */
34
class ScriptViewHelper extends AbstractTagBasedViewHelper
35
{
36
37
    /**
38
     * @var AssetCollector
39
     */
40
    protected $assetCollector;
41
42
    /**
43
     * @param AssetCollector $assetCollector
44
     */
45
    public function injectAssetCollector(AssetCollector $assetCollector): void
46
    {
47
        $this->assetCollector = $assetCollector;
48
    }
49
50
    /**
51
     * @api
52
     */
53
    public function initializeArguments(): void
54
    {
55
        parent::initializeArguments();
56
        parent::registerUniversalTagAttributes();
57
        $this->registerTagAttribute('async', 'string', '', false);
58
        $this->registerTagAttribute('crossorigin', 'string', '', false);
59
        $this->registerTagAttribute('defer', 'string', '', false);
60
        $this->registerTagAttribute('integrity', 'string', '', false);
61
        $this->registerTagAttribute('nomodule', 'string', '', false);
62
        $this->registerTagAttribute('nonce', 'string', '', false);
63
        $this->registerTagAttribute('referrerpolicy', 'string', '', false);
64
        $this->registerTagAttribute('src', 'string', '', false);
65
        $this->registerTagAttribute('type', 'string', '', false);
66
        $this->registerArgument(
67
            'identifier',
68
            'string',
69
            'Use this identifier within templates to only inject your JS once, even though it is added multiple times',
70
            true
71
        );
72
        $this->registerArgument(
73
            'priority',
74
            'boolean',
75
            'Define whether the JavaScript should be put in the <head> tag above-the-fold or somewhere in the body part.',
76
            false,
77
            false
78
        );
79
    }
80
81
    public function render(): string
82
    {
83
        $identifier = $this->arguments['identifier'];
84
        $attributes = $this->tag->getAttributes();
85
        $src = $this->tag->getAttribute('src');
86
        unset($attributes['src']);
87
        $options = [
88
            'priority' => $this->arguments['priority']
89
        ];
90
        if ($src !== null) {
0 ignored issues
show
introduced by
The condition $src !== null is always true.
Loading history...
91
            $this->assetCollector->addJavaScript($identifier, $src, $attributes, $options);
92
        } else {
93
            $this->assetCollector->addInlineJavaScript($identifier, $this->renderChildren(), $attributes, $options);
94
        }
95
        return '';
96
    }
97
}
98