How to get Filesystem path from public static package resources?

Hello.
I wanted to get the absolute path of a file from the filesystem perspective but found no way to get this with the current API of the resourceManager.

I know you can get the Webserver URL to a resource with $this->resourceManager->getPublicPackageResourceUriByPath()

But there is no method to somehow get the filepath.

You can get the file content with file_get_contents(), so i tried different php functions like realpath() which only returns false since i guess it does not use Stream Wrappers like file_get_contents does.

I currently have written this method to get the path

/**
 * @param string $resourceUri
 * @return string
 * @throws \Neos\Flow\ResourceManagement\Exception
 */
protected function getPackageResourcePath(string $resourceUri): string
{
    [$packageKey, $relativePathAndFilename] = $this->resourceManager->getPackageAndPathByPublicPath($resourceUri);
    return FLOW_PATH_PACKAGES . 'Application/' . $packageKey . '/Resources/Public/' . $relativePathAndFilename;
}

but it feels a bit wrong since especially the documentation tells you, you should not build the path with the constants.

Is there any way i missed something here?

Not sure what you are trying to do. But you can get a certain package from the PackageManager and it has PackagePath and ResourcesPath.

Usecase was giving ImageMagick the path to a font-file. since you can only give it by a path, i was looking for any easy solution.

Didn’t thought about the PackageManager and migt be useful.
But i thought it might be possible to get the filesystem path by a resource uri like resource://My.Package/Foo/Bar.file

since we can already use it with file_get_contents() , give it to viewHelpers etc. it could have been useful.

Problem with resources is that they may be not on the system at all when cloudstorage is used (at least the public path can be anywhere). I would suggest to use file_get_contents to create a tempfile that is passed to imagemagick.

Other than that the package manager is able to tell you the full resources-path of the package via $package->getResourcesPath().

You can also do resource->createTemporaryLocalCopy() which returns a filepath. That should work with any storage.

1 Like