phuxtil / splfileinfo
SplFileInfo compatible implementation that allows to work with virtual (non existing) files
Installs: 14 609
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: ^8
Requires (Dev)
- phpunit/phpunit: ^9
- symfony/var-dumper: ^4|^5
This package is auto-updated.
Last update: 2024-11-18 23:57:38 UTC
README
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()
.
Installation
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+
Usage
Create virtual file info.
$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.
Check if resource is virtual.
$virtualInfo->getType(); # virtual $virtualInfo->isVirtual(); # true
Update virtual file info with real resource data
@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
}
Extra methods
isVirtual(): bool
Returns true if the and does not really exist.
Note: isReadable(), isFile(),... etc, can return true, even if the resource does not exist.
fromSplFileInfo(\SplFileInfo $info)
$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));
toArray(): array
$info = new VirtualSplFileInfo('/tmp/not-yet/existing-path'); $data = $info->toArray();
fromArray(array $data)
$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, ] );
Setter support
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
Default values
All values besides PathInfo
are set to -1
by default.
TDD
See tests for more examples.