- What is zenstruck/stream used for?
- Zenstruck Stream is a lightweight object-oriented wrapper around PHP stream resources. It lets you read, write, seek, and inspect streams through an object API instead of working directly with raw resource functions. It is useful for files, in-memory data, and streamed content in PHP applications, including Laravel services and jobs.
- How do I install zenstruck/stream in a Laravel app?
- Install it with Composer by running composer require zenstruck/stream in your Laravel project. The package is a plain PHP utility, so it typically does not require a Laravel service provider or config file. After installation, import the package classes and wrap the stream resources you need.
- Does zenstruck/stream work with Laravel?
- Yes. Zenstruck Stream is framework-agnostic, so it can be used inside Laravel controllers, services, commands, and queued jobs. You normally do not need to register anything in Laravel's service container. Just ensure your application's PHP version satisfies the package's Composer requirements.
- How do I read from a stream with Zenstruck Stream?
- Wrap the PHP stream resource you want to read, such as a file handle or in-memory stream, using the package's stream object. Then use its read methods to pull content instead of calling fread directly. Check the README for the exact method names and available read options for your version.
- How do I write to a stream with Zenstruck Stream?
- Open or create a writable PHP stream resource and wrap it with Zenstruck Stream. Use the wrapper's write methods to send data to the underlying stream. Close or flush the resource when finished so buffered data is persisted correctly.
- How can I create an in-memory stream with Zenstruck Stream?
- Create a PHP in-memory stream with php://memory or php://temp, then wrap that resource using Zenstruck Stream. This is handy for tests, temporary data, and avoiding real files on disk. php://temp can spill to disk after a threshold, while php://memory stays in memory.
- Can I seek or rewind a stream with Zenstruck Stream?
- Yes, if the underlying PHP stream is seekable. The wrapper is designed to expose stream operations such as seeking and rewinding through its object API. You should still check that the resource supports seeking before relying on it.
- How do I inspect stream metadata with Zenstruck Stream?
- Zenstruck Stream is intended to make stream inspection simpler by giving you an object wrapper around the resource. Use its inspection methods to access details exposed by the underlying PHP stream, such as metadata available from stream_get_meta_data. For exact capabilities, refer to the package README for your installed version.
- Is zenstruck/stream suitable for production use?
- It is a lightweight utility, so production suitability mostly depends on how you handle the underlying streams and errors. Close resources reliably, handle failed reads or writes, and avoid leaving open file handles. In Laravel, wrapping stream logic in services or jobs helps keep resource cleanup predictable.
- What are alternatives to zenstruck/stream?
- Alternatives include using native PHP stream functions directly, Symfony's stream utilities, or filesystem abstractions such as League Flysystem when you need broader storage support. Zenstruck Stream is a good fit when you want a small object wrapper around PHP resources without a large abstraction layer. Evaluate based on whether you need simple stream wrapping or full filesystem management.