I'm working on some Magento 2 extension that requires reading files from the file system.
When running the php sniffer using the ECGM2 standards, it complains about the fact that I'm using functions like basename
or dirname
.
The use of function dirname() is forbidden
or
The use of function basename() is forbidden
What wrapper should I use instead of those to get the the same effect?
[EDIT]
Here is some code, but it's not that relevant to the question.
I have a collection class that extends the \Magento\Framework\Data\Collection\Filesystem
class and I want to list this collection in a grid (ui-components) and one of the actions in the grid is a download action.
For this, I need to get the actual name of the file so I can send it to the download action.
// here $file is dynamic and it can be
// folder/filename.xml or folder/subfolder/file.tar.gz
//so there is no strict number of folders and subfolders.
$file = $downloader->getRelativePath($packageName);
$relativeFile = UmcFilesystem::VAR_DIR_NAME . '/' .$file;
$absoluteFile = $rootDir->getAbsolutePath($relativeFile);
if ($rootDir->isFile($relativeFile) && $rootDir->isReadable($relativeFile)){
//I don't want to use `explode` just for the sake of avoiding basename
$fileName = basename($absoluteFile);
$this->fileFactory->create(
$fileName,
null,
DirectoryList::VAR_DIR,
'application/octet-stream',
$rootDir->stat($relativeFile)['size']
);
$resultRaw = $this->resultRawFactory->create();
$resultRaw->setContents($rootDir->readFile($relativeFile));
return $resultRaw;
} else {
...
}
Best Answer
I also needed something like that recently. Only solution I found to get
basename
anddirname
was using:Before that I tried using
Magento\Framework\Filesystem\Directory\Write
andgetDriver()
with no success. With them you can get pretty much everything but not thebasename
.