Skip to content

Instantly share code, notes, and snippets.

@Sebobo
Created March 30, 2017 15:31
Show Gist options
  • Select an option

  • Save Sebobo/82e8ded9a3aec2b0d9a433e65d1f00ae to your computer and use it in GitHub Desktop.

Select an option

Save Sebobo/82e8ded9a3aec2b0d9a433e65d1f00ae to your computer and use it in GitHub Desktop.
Cache busting for Neos resources
<?php
namespace Foo\Bar\Aspects;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Aop\JoinPointInterface;
use TYPO3\Flow\Resource\ResourceManager;
use TYPO3\TypoScript\TypoScriptObjects\ResourceUriImplementation;
/**
* @Flow\Aspect
*/
class ResourceModificationTimeAspect
{
/**
* @Flow\Around("method(TYPO3\Fluid\ViewHelpers\Uri\ResourceViewHelper->render())")
*
* @param JoinPointInterface $joinPoint The current joinpoint
*
* @return string The result of the target method if it has not been intercepted
*/
public function addModificationTime(JoinPointInterface $joinPoint)
{
$result = $joinPoint->getAdviceChain()->proceed($joinPoint);
if ($joinPoint->getMethodArgument('resource') !== null) {
return $result;
}
return $this->getModifiedResourceUri($result, $joinPoint->getMethodArgument('path'), $joinPoint->getMethodArgument('package'));
}
/**
* @Flow\Around("method(TYPO3\TypoScript\TypoScriptObjects\ResourceUriImplementation->evaluate())")
*
* @param JoinPointInterface $joinPoint The current joinpoint
*
* @return string The result of the target method if it has not been intercepted
*/
public function addModificationTimeToResourceUri(JoinPointInterface $joinPoint)
{
/** @var ResourceUriImplementation $proxy */
$proxy = $joinPoint->getProxy();
$result = $joinPoint->getAdviceChain()->proceed($joinPoint);
if ($proxy->getResource() !== null) {
return $result;
}
return $this->getModifiedResourceUri($result, $proxy->getPath(), $proxy->getPackage());
}
/**
* @param string $uri
* @param string $path
* @param string $package
* @return string
*/
protected function getModifiedResourceUri($uri, $path, $package)
{
if (strpos($path, 'resource://') === 0) {
$resourcePath = $path;
} elseif ($package !== null) {
$resourcePath = 'resource://' . $package . '/Public/' . $path;
} else {
return $uri;
}
if (!is_dir($resourcePath) && strpos($uri, 'bust') === false) {
try {
$hash = 'bust=' . substr(sha1_file($resourcePath), 0, 8);
if (strpos($uri, '?') === false) {
return $uri . '?' . $hash;
} else {
return $uri . '&' . $hash;
}
} catch (\Exception $e) {
}
}
return $uri;
}
}
@visay
Copy link
Copy Markdown

visay commented Dec 8, 2017

With neos/fluid-adaptor 4.2, this doesn't work anymore and I guess because of the refactor to static function https://github.com/neos/fluidadaptor/blob/c025cc4c13f9e35a3363cfca02af832e6350a3bb/Classes/ViewHelpers/Uri/ResourceViewHelper.php#L108

Do you know the solution?

@Sebobo
Copy link
Copy Markdown
Author

Sebobo commented Dec 10, 2017

You should use https://github.com/Flowpack/Flowpack.CacheBuster instead.
It also doesn't rely on fluid anymore.

@visay
Copy link
Copy Markdown

visay commented Dec 11, 2017

Works like a charm 👍

Thanks a lot :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment