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

CssViewHelper::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
 * CssViewHelper
23
 *
24
 * Examples
25
 * ========
26
 *
27
 * ::
28
 *
29
 *    <f:asset.css identifier="identifier123" href="EXT:my_ext/Resources/Public/Css/foo.css" />
30
 *    <f:asset.css identifier="identifier123">
31
 *       .foo { color: black; }
32
 *    </f:asset.css>
33
 */
34
class CssViewHelper 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('as', 'string', '', false);
58
        $this->registerTagAttribute('crossorigin', 'string', '', false);
59
        $this->registerTagAttribute('disabled', 'string', '', false);
60
        $this->registerTagAttribute('href', 'string', '', false);
61
        $this->registerTagAttribute('hreflang', 'string', '', false);
62
        $this->registerTagAttribute('importance', 'string', '', false);
63
        $this->registerTagAttribute('integrity', 'string', '', false);
64
        $this->registerTagAttribute('media', 'string', '', false);
65
        $this->registerTagAttribute('referrerpolicy', 'string', '', false);
66
        $this->registerTagAttribute('rel', 'string', '', false);
67
        $this->registerTagAttribute('sizes', 'string', '', false);
68
        $this->registerTagAttribute('type', 'string', '', false);
69
        $this->registerTagAttribute('nonce', 'string', '', false);
70
        $this->registerArgument(
71
            'identifier',
72
            'string',
73
            'Use this identifier within templates to only inject your CSS once, even though it is added multiple times',
74
            true
75
        );
76
        $this->registerArgument(
77
            'priority',
78
            'boolean',
79
            'Define whether the css should be put in the <head> tag above-the-fold or somewhere in the body part.',
80
            false,
81
            false
82
        );
83
    }
84
85
    public function render(): string
86
    {
87
        $identifier = $this->arguments['identifier'];
88
        $attributes = $this->tag->getAttributes();
89
        $file = $this->tag->getAttribute('href');
90
        unset($attributes['href']);
91
        $options = [
92
            'priority' => $this->arguments['priority']
93
        ];
94
        if ($file !== null) {
0 ignored issues
show
introduced by
The condition $file !== null is always true.
Loading history...
95
            $this->assetCollector->addStyleSheet($identifier, $file, $attributes, $options);
96
        } else {
97
            $this->assetCollector->addInlineStyleSheet($identifier, $this->renderChildren(), $attributes, $options);
98
        }
99
        return '';
100
    }
101
}
102