-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for caching the underlying stream #124
Conversation
While php://input is seekable, you can't re-read it a second time. This is problematic when you want to parse the body for `getParsedBody()`, but also allow subsequent uses of `getBody()` to work. To address this, we now allow `Stream` to optionally cache the data it reads (via `read()` or `getContents()`. Then, once the eof is detected, we return the cached data on subsequent `getContents()` calls so that the stream is not read a second time. Also, update `ServerRequestFactory` so that when it creates a stream for php://input, it enables the caching mechanism.
@akrabat Hi, by now you know I like my streams! 😊 Me thinking out loud: If we wanted to, this could even be expanded on to let the user of the SteamInterface object set there own cache stream and in that way get fine-grained control of the cache behavior. An example would be a memory-constrained system that maybe would use fopen("php://temp/maxmemory:1024", 'r+'). |
@akrabat why you decide to add additional flag to add cache ability? The |
I do :) I think it's great that you're thinking about this stuff, thanks.
Very possibly. I'll change it and let's see what it looks like. |
In general in Slim, we haven't tended to create a new class and or interface for every little thing that can be decoupled as that seems to be overwhelming for beginners who are using Slim as their first framework and maybe don't have the strong foundation in design patterns and OO as more experience developers do. If a class does become overwhelming, then you do tend to see it being split up appropriately as we then have more knowledge. However, as always, I'll take direction from @l0gicgate and the maintainers as I think the fundamentals of this feature are important and want to see it in the codebase! |
This is a hard pill to swallow for advanced developers like most of us maintaining the project and commenting on these PRs but it's the cold hard truth. We need to keep simplicity in mind. That being said though, I'm just curious as to why we're giving the option to cache the stream and not make it just default internal behavior without exposing the |
For a seekable stream (that isn't php://input), it doubles memory usage for no benefit. Imagine a 200MB PPT file for instance… |
@akrabat okay that makes sense then. So just so we're clear though, by default we are creating an automatically caching stream when using $body = (new StreamFactory())->createStreamFromFile('php://input', 'r', true); |
Yeah. It's a toss-up. How many apps send a binary directly into a web app without using |
I think we should just leave it as a |
Seems like there's only some additional tests to be written before I merge this. I'd like to tag a release soon. This looks good to me. |
When caching a non-seekable stream, allow the user to provide the StreamInterface object that the wish to use. When creating a Stream from globals, cache the php://input based Stream with a php://temp steam.
@anfly0 I've re-worked to use an instance of |
@l0gicgate This is ready for review from my point of view. I've added the tests that I could think of and for the |
Looking at the coveralls results, it doesn't seem to detect |
That's better! It seems at PHP 7.1 doesn't like something about |
While php://input is seekable, you can't re-read it a second time. This is problematic when you want to parse the body for
getParsedBody()
, but also allow subsequent uses ofgetBody()
to work.To address this, we now allow
Stream
to optionally cache the data it reads (viaread()
orgetContents()
. Then, once the eof is detected, we return the cached data on subsequentgetContents()
calls so that the stream is not read a second time.Also, update
ServerRequestFactory
so that when it creates a stream for php://input, it enables the caching mechanism.