• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / vhs / 28371118611

29 Jun 2026 12:11PM UTC coverage: 69.786%. Remained the same
28371118611

push

github

NamelessCoder
[TASK] Get rid of LF constant usage

9 of 15 new or added lines in 10 files covered. (60.0%)

4795 of 6871 relevant lines covered (69.79%)

4.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.4
/Classes/ViewHelpers/Media/AudioViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Media;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use FluidTYPO3\Vhs\Traits\TagViewHelperTrait;
12
use FluidTYPO3\Vhs\Utility\CoreUtility;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
15

16
/**
17
 * Renders HTML code to embed a HTML5 audio player. NOTICE: This is
18
 * all HTML5 and won't work on browsers like IE8 and below. Include
19
 * some helper library like kolber.github.io/audiojs/ if you need to suport those.
20
 * Source can be a single file, a CSV of files or an array of arrays
21
 * with multiple sources for different audio formats. In the latter
22
 * case provide array keys 'src' and 'type'. Providing an array of
23
 * sources (even for a single source) is preferred as you can set
24
 * the correct mime type of the audio which is otherwise guessed
25
 * from the filename's extension.
26
 */
27
class AudioViewHelper extends AbstractMediaViewHelper
28
{
29
    use TagViewHelperTrait;
30

31
    /**
32
     * @var string
33
     */
34
    protected $tagName = 'audio';
35

36
    protected array $validTypes = ['mp3', 'ogg', 'oga', 'wav'];
37
    protected array $mimeTypesMap = [
38
        'mp3' => 'audio/mpeg',
39
        'ogg' => 'audio/ogg',
40
        'oga' => 'audio/ogg',
41
        'wav' => 'audio/wav'
42
    ];
43
    protected array $validPreloadModes = ['auto', 'metadata', 'none'];
44

45
    public function initializeArguments(): void
46
    {
47
        parent::initializeArguments();
2✔
48
        $this->registerUniversalTagAttributes();
2✔
49
        $this->registerArgument(
2✔
50
            'width',
2✔
51
            'integer',
2✔
52
            'Sets the width of the audio player in pixels.',
2✔
53
            true
2✔
54
        );
2✔
55
        $this->registerArgument('height', 'integer', 'Sets the height of the audio player in pixels.', true);
2✔
56
        $this->registerArgument(
2✔
57
            'autoplay',
2✔
58
            'boolean',
2✔
59
            'Specifies that the audio will start playing as soon as it is ready.',
2✔
60
            false,
2✔
61
            false
2✔
62
        );
2✔
63
        $this->registerArgument(
2✔
64
            'controls',
2✔
65
            'boolean',
2✔
66
            'Specifies that audio controls should be displayed (such as a play/pause button etc).',
2✔
67
            false,
2✔
68
            false
2✔
69
        );
2✔
70
        $this->registerArgument(
2✔
71
            'loop',
2✔
72
            'boolean',
2✔
73
            'Specifies that the audio will start over again, every time it is finished.',
2✔
74
            false,
2✔
75
            false
2✔
76
        );
2✔
77
        $this->registerArgument(
2✔
78
            'muted',
2✔
79
            'boolean',
2✔
80
            'Specifies that the audio output of the audio should be muted.',
2✔
81
            false,
2✔
82
            false
2✔
83
        );
2✔
84
        $this->registerArgument(
2✔
85
            'poster',
2✔
86
            'string',
2✔
87
            'Specifies an image to be shown while the audio is downloading, or until the user hits the play button.'
2✔
88
        );
2✔
89
        $this->registerArgument(
2✔
90
            'preload',
2✔
91
            'string',
2✔
92
            'Specifies if and how the author thinks the audio should be loaded when the page loads. Can be "auto", ' .
2✔
93
            '"metadata" or "none".',
2✔
94
            false,
2✔
95
            'auto'
2✔
96
        );
2✔
97
        $this->registerArgument(
2✔
98
            'unsupported',
2✔
99
            'string',
2✔
100
            'Add a message for old browsers like Internet Explorer 9 without audio support.'
2✔
101
        );
2✔
102
    }
103

104
    public function render(): string
105
    {
106
        $sources = static::getSourcesFromArgument($this->arguments);
×
107
        if (0 === count($sources)) {
×
108
            throw new Exception('No audio sources provided.', 1359382189);
×
109
        }
110

111
        foreach ($sources as $source) {
×
112
            if (is_string($source)) {
×
113
                if (false !== strpos($source, '//')) {
×
114
                    $src = $source;
×
115
                    $type = mb_substr($source, mb_strrpos($source, '.') + 1);
×
116
                } else {
117
                    $src = mb_substr(
×
118
                        GeneralUtility::getFileAbsFileName($source),
×
119
                        mb_strlen(CoreUtility::getSitePath())
×
120
                    );
×
121
                    $type = pathinfo($src, PATHINFO_EXTENSION);
×
122
                }
123
            } elseif (is_array($source)) {
×
124
                if (!isset($source['src'])) {
×
125
                    throw new Exception('Missing value for "src" in sources array.', 1359381250);
×
126
                }
127
                $src = $source['src'];
×
128
                if (!isset($source['type'])) {
×
129
                    throw new Exception('Missing value for "type" in sources array.', 1359381255);
×
130
                }
131
                $type = $source['type'];
×
132
            } else {
133
                // skip invalid source
134
                continue;
×
135
            }
136
            $type = mb_strtolower($type);
×
137
            if (!in_array($type, $this->validTypes)) {
×
138
                    throw new Exception('Invalid audio type "' . $type . '".', 1359381260);
×
139
            }
140
            $type = $this->mimeTypesMap[$type];
×
141
            $src = static::preprocessSourceUri($src, $this->arguments);
×
142
            $this->renderChildTag('source', ['src' => $src, 'type' => $type], false, 'append');
×
143
        }
144
        $tagAttributes = [
×
145
            'width'   => $this->arguments['width'],
×
146
            'height'  => $this->arguments['height'],
×
147
            'preload' => 'auto',
×
148
        ];
×
149
        if ($this->arguments['autoplay']) {
×
150
            $tagAttributes['autoplay'] = 'autoplay';
×
151
        }
152
        if ($this->arguments['controls']) {
×
153
            $tagAttributes['controls'] = 'controls';
×
154
        }
155
        if ($this->arguments['loop']) {
×
156
            $tagAttributes['loop'] = 'loop';
×
157
        }
158
        if ($this->arguments['muted']) {
×
159
            $tagAttributes['muted'] = 'muted';
×
160
        }
161
        if (in_array($this->arguments['preload'], $this->validPreloadModes)) {
×
162
            $tagAttributes['preload'] = $this->arguments['preload'];
×
163
        }
164
        if (null !== $this->arguments['poster']) {
×
165
            $tagAttributes['poster'] = $this->arguments['poster'];
×
166
        }
167
        $this->tag->addAttributes($tagAttributes);
×
168
        if ($this->arguments['unsupported']) {
×
NEW
169
            $this->tag->setContent($this->tag->getContent() . PHP_EOL . $this->arguments['unsupported']);
×
170
        }
171
        return $this->tag->render();
×
172
    }
173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc