FluidTYPO3 / vhs

TYPO3 extension VHS: Fluid ViewHelpers
https://fluidtypo3.org
Other
189 stars 228 forks source link

PHP Warning: Undefined array key 2 in Classes/Service/AssetService.php #1853

Closed sdelcroix closed 1 year ago

sdelcroix commented 1 year ago

Hi,

In the CSS file that I'm including with v:asset.style, there are paths for fonts files as follow :

@font-face{
    font-family:weathericons;
    src:url(../font/weathericons-regular-webfont.eot);
    src:url(../font/weathericons-regular-webfont.eot?#iefix) format('embedded-opentype'),url(../font/weathericons-regular-webfont.woff2) format('woff2'),url(../font/weathericons-regular-webfont.woff) format('woff'),url(../font/weathericons-regular-webfont.ttf) format('truetype'),url(../font/weathericons-regular-webfont.svg#weather_iconsregular) format('svg');
    font-weight:400;
    font-style:normal
}

A PHP Warning is raised in AssertService when there's no suffix in the path. The "Undefined array key 2" error occurs with the "list" function at FluidTYPO3\Vhs\Service\AssetService->copyReferencedFilesAndReplacePaths() :

                if (0 < preg_match('/([^\?#]+)(.+)?/', $match, $items)) {
                    list(, $path, $suffix) = $items;
                } else {
                    $path = $match;
                    $suffix = '';
                }

The test for the regexp shouldn't be inverted ? Like this

if (0 < preg_match('/([^\?#]+)(.+)?/', $match, $items)) {
                    $path = $match;
                    $suffix = '';
                } else {
                    list(, $path, $suffix) = $items;
                }

Or the regexp may be changed like this :

if (0 < preg_match('/([^\?#]+)(#.+)/', $match, $items)) {

Because in the actual state, it captures only paths without prefix.

mogensf commented 1 year ago

Got the same Issue. TYPO3: 11.5.25 VHS: 6.1.3

As workaround i changed to fluid viewhelper asset.css. https://docs.typo3.org/other/typo3/view-helper-reference/main/en-us/typo3/fluid/latest/Asset/Css.html

sdelcroix commented 1 year ago

@mogensf

Yep, I've tried this workaround too ;) But I kinda dislike that we cannot combine/compress files using TYPO3 AssetCollector :/ We deliver with HTTP/2 but I like the VHS behaviour : assets by components + concatenation into one file.

magicsunday commented 1 year ago

Changing the regex to if (0 < preg_match('/([^\?#]+)(#.+)/', $match, $items)) { does not work like expected => https://regex101.com/r/QPcgwO/1

The correct way to fix this, imho, is to remove the usage if the "list" method and instead directly access the values and check if they exist.

$path = $items[1];
$suffix = $items[2] ?? '';
sdelcroix commented 1 year ago

Yep, you're right, I've proposed this regexp too quickly without further tests. 'wanted to dive more into this but lack of time :(

NamelessCoder commented 1 year ago

Another valid workaround is to set rewrite="0" on the asset that contains the problematic code.