VirtualSplFileInfo
allows to use non existing (virtual) paths and still be able to perform
all file operations like getSize()
, isFile()
, getOnwer()
, etc, and get predefined results.
It has setters support, and helper methods like isVirtual()
, toArray()
, fromArray()
, fromSplFileInfo()
.
composer require phuxtil/splfileinfo
Note: Use v1.x for compatibility with PHP v7.0.x. Note: Use v2.x for compatibility with PHP v7.2+
$path = '/tmp/not-yet/existing-path';
$virtualInfo = new VirtualSplFileInfo($path);
Only PathInfo data is set at this point.
$virtualInfo->getPathname(); # /tmp/not-yet/existing-path
$virtualInfo->getPath(); # /tmp/not-yet
...
The rest of the data can be updated with setters.
$virtualInfo->setSize(120);
$virtualInfo->setATime(time());
$virtualInfo->setPerms(0775);
...
Note: All properties besides PathInfo are set to -1 by default.
$virtualInfo->getType(); # virtual
$virtualInfo->isVirtual(); # true
@mkdir($path);
$virtualInfo->fromSplFileInfo(new SplFileInfo($path));
$virtualInfo->isVirtual(); # false
VirtualFileInfo
vs \SplFileInfo
.
$splInfo = SplFileInfo {
path: "/tmp/not-yet"
filename: "existing-path"
basename: "existing-path"
pathname: "/tmp/not-yet/existing-path"
extension: ""
realPath: "/tmp/not-yet/existing-path"
aTime: 2019-06-15 22:07:47
mTime: 2019-06-15 22:07:47
cTime: 2019-06-15 22:07:47
inode: 10248205
size: 64
perms: 040755
owner: 0
group: 0
type: "dir"
writable: true
readable: true
executable: true
file: false
dir: true
link: false
}
$virtualInfo = Phuxtil\SplFileInfo\VirtualSplFileInfo {
path: "/tmp/not-yet"
filename: "existing-path"
basename: "existing-path"
pathname: "/tmp/not-yet/existing-path"
extension: ""
realPath: "/tmp/not-yet/existing-path"
aTime: 2019-06-15 22:07:47
mTime: 2019-06-15 22:07:47
cTime: 2019-06-15 22:07:47
inode: 10248205
size: 64
perms: 040755
owner: 0
group: 0
type: "dir"
writable: true
readable: true
executable: true
file: false
dir: true
link: false
}
Returns true if the and does not really exist.
Note: isReadable(), isFile(),... etc, can return true, even if the resource does not exist.
$path = '/tmp/not-yet/existing-file.txt';
$virtualInfo = new VirtualSplFileInfo($path);
// ... do stuff
// create resource later
file_put_contents($path, 'Lorem Ipsum');
// update virtual file info
$virtualInfo->fromSplFileInfo(new SplFileInfo($path));
$info = new VirtualSplFileInfo('/tmp/not-yet/existing-path');
$data = $info->toArray();
$info = new VirtualSplFileInfo('/tmp/not-yet/existing-path');
$info->fromArray(
[
'aTime' => 123,
'mTime' => 456,
'cTime' => 789,
'inode' => 222,
'size' => 333,
'perms' => 0755,
'owner' => 1,
'group' => 2,
'type' => 'dir',
'writable' => true,
'readable' => true,
'executable' => true,
'file' => false,
'dir' => true,
'link' => false,
]
);
You can use setters for all properties besides PathInfo
, which is resolved by default in \SplFileInfo
.
The resource does not have to yet exist for those methods to work.
Properties with setters:
realPath
aTime
mTime
cTime
inode
size
perms
owner
group
type
writable
readable
executable
file
dir
link
linkTarget
All values besides PathInfo
are set to -1
by default.
See tests for more examples.