Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Allow to use f:resource path and improve error message on invalid path #1533

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions Classes/ViewHelpers/Asset/AbstractAssetViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

use FluidTYPO3\Vhs\Service\AssetService;
use FluidTYPO3\Vhs\Traits\ArrayConsumingViewHelperTrait;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder;
use TYPO3\CMS\Frontend\Exception;

/**
* Base class for ViewHelpers capable of registering assets
Expand Down Expand Up @@ -270,7 +272,7 @@ public function build()
if (true === isset($this->arguments['external']) && true === (boolean) $this->arguments['external']) {
$path = $this->arguments['path'];
} else {
$path = GeneralUtility::getFileAbsFileName($this->arguments['path']);
$path = $this->buildAbsolutePath($this->arguments['path']);
}
$content = file_get_contents($path);
return $content;
Expand Down Expand Up @@ -474,7 +476,7 @@ public function getAssetSettings()
$assetSettings = $this->mergeArrays($assetSettings, $settings['asset'][$name]);
}
if (!empty($assetSettings['path']) && !$assetSettings['external']) {
$assetSettings['path'] = GeneralUtility::getFileAbsFileName($assetSettings['path']);
$assetSettings['path'] = $this->buildAbsolutePath($assetSettings['path']);
}
$assetSettings['name'] = $name;
$this->assetSettingsCache = $assetSettings;
Expand Down Expand Up @@ -571,4 +573,33 @@ public function assertHasBeenRemoved()
}
return false;
}

/**
* @param $path
* @return string
* @throws Exception
*
* A path like /typo3conf/ext/myext/Resources/Public/myscript.js
* ( which is what you get from {f:uri.resource(path:'myscript.js')} )
* is not correct processed by GeneralUtility::getFileAbsFileName
* it returns '' - we need to remove the '/' at the beginning to work
*/
private function buildAbsolutePath($path)
{
$incomingPath = $path;
if( preg_match('|^/|',$path)
&& !file_exists($path)
&& file_exists(Environment::getPublicPath() . $path)
) {
$path = preg_replace('|^/|','',$path);
}
$path = GeneralUtility::getFileAbsFileName($path);
if (empty($path)) {
throw new Exception (
sprintf('Could not create absolute path for path "%s" (asset-name: "%s")',
$incomingPath, $this->arguments["name"])
);
}
return $path;
}
}