aternos / io
PHP library for IO operations
Requires
- php: >=8.3.0
Requires (Dev)
- phpunit/phpunit: ^11
This package is auto-updated.
Last update: 2024-11-15 17:35:16 UTC
README
PHP library for IO operations.
The main goal of this library is to abstract IO operations to make different kind of IO elements, e.g. files and streams, interchangeable and allow writing code that doesn't depend on the filesystem, but feature interfaces that can be implemented for any kind of IO backend, e.g. network based storages.
Installation
composer require aternos/io
Basic Usage
use Aternos\IO\System\File\File; use Aternos\IO\System\Directory\Directory; $file = new File("path/to/file.txt"); $file->create(); $file->write("Hello World!"); $file->setPosition(0); echo $file->read($file->getSize()); $file->delete(); $directory = new Directory("path/to/directory"); foreach ($directory->getChildrenRecursive() as $child) { echo $child->getPath() . PHP_EOL; } $directory->delete();
IO Elements
You can get the correct IO element from a path using FilesystemElement::getIOElementFromPath()
.
use Aternos\IO\System\FilesystemElement; $element = FilesystemElement::getIOElementFromPath("path/to/element");
Interfaces
You can and should use the provided interfaces when writing your code to make it more flexible and interchangeable.
The basic interface for all IO elements is Aternos\IO\Interfaces\IOElementInterface
.
Feature Interfaces
The feature interfaces define specific features that an IO element can have, e.g. reading or writing. You should limit the required type in your code to the specific features that you need.
All feature interfaces are listed here: src/Interfaces/Features
.
Example
use Aternos\IO\Interfaces\Features\ReadInterface; use Aternos\IO\Interfaces\Features\GetSizeInterface; function readEntireFile(ReadInterface&GetSizeInterface $file): string { return $file->read($file->getSize()); }
Type Interfaces
For convenience, this library also provides type interfaces that combine multiple feature interfaces for common
IO elements. All type interfaces are listed here: src/Interfaces/Types
.