ulovdomov / php-stream
Php Stream
v1.0.3
2025-05-26 12:55 UTC
Requires
- php: >=8.0 <8.5
- psr/http-message: ^1.0|^2.0
Requires (Dev)
- phpstan/phpstan: ^1.11
- phpstan/phpstan-strict-rules: ^1.6
- phpunit/phpunit: ^8.5|^10.5
- ulovdomov/php-code-style: ^1.0.0
Suggests
- ext-fileinfo: To get MimeType in FileStream
README
This package provides a PSR-7-compatible StreamInterface
implementation,
along with an extended UlovDomov\Stream\FileStream
class that adds more.
Installation
Run:
composer require ulovdomov/php-stream
Usage
Create UlovDomov\Stream\FileStream
You can create stream from:
resource
string
int
float
bool
\Psr\Http\Message\StreamInterface
callable
\Iterator
null
$stream = \UlovDomov\Stream\FileStream::create('my stream content');
For file path
:
// stream for reading $stream = \UlovDomov\Stream\FileStream::createForPath(__DIR__ . '/file.pdf'); // stream for writing $stream = \UlovDomov\Stream\FileStream::createForPath(__DIR__ . '/file.pdf', 'w');
Additional features
/** @var UlovDomov\Stream\FileStream $stream */ // get mime type $stream->getMimeType(); // string: text/plain // get file extension (can return null for application/octet-stream or unknown mimetype) $stream->getExtension(); // string: txt // save content as file $stream->saveAs(__DIR__ . '/my-new-file.jpg');
Utils
Method tryFopen
Replacement for fopen
, it returns a resource; throws a StreamException
on error.
/** @var resource $file */ $file = \UlovDomov\Stream\Utils::tryFopen(__DIR__ . '/my-file.pdf');
Method mimetypeToExtension
Get file extension. Can return null for application/octet-stream
or not implemented mimetype.
\UlovDomov\Stream\Utils::mimetypeToExtension('application/pdf'); // "pdf"
Method isOctetStream
Get true
if mimetype is application/octet-stream
.
\UlovDomov\Stream\Utils::isOctetStream('application/octet-stream'); // true \UlovDomov\Stream\Utils::isOctetStream('text/json'); // false
Development
First setup
- Run for initialization
make init
- Run composer install
make composer
Use tasks in Makefile:
- To log into container
make docker
- To run code sniffer fix
make cs-fix
- To run PhpStan
make phpstan
- To run tests
make phpunit