Skip to content
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

Adjust to atk4/ui PSR-7 response #32

Merged
merged 5 commits into from
Apr 8, 2023
Merged

Adjust to atk4/ui PSR-7 response #32

merged 5 commits into from
Apr 8, 2023

Conversation

mvorisek
Copy link
Member

@mvorisek mvorisek commented Mar 2, 2023

related with atk4/ui#1706

@DarkSide666 DarkSide666 mentioned this pull request Mar 31, 2023
@abbadon1334
Copy link
Collaborator

abbadon1334 commented Apr 4, 2023

Pull Request Draft: Stream Handling Improvement

This pull request aims to remove the problematic exit statement in a cleaner way by implementing better stream handling. The proposed changes include adding a new condition to App::terminate and modifying the Helper::output method.

Changes to App::terminate

Add the following if condition at the beginning of the method:

if ($output instanceof StreamInterface) { // if is a stream, emit directly
    $this->response = $this->response->withBody($output);
    $this->emitResponse();

} elseif ($type === 'application/json') {

Changes to Helper::output

Modify the method with the following code:

$resource = $model->flysystem->readStream($path);
$factory = new \Nyholm\Psr7\Factory\Psr17Factory();
$app->terminate($factory->createStreamFromResource($resource));

Rationale

Basing the output guessing on headers is not suitable for streams. For example, if a file with application/json content needs to be streamed, it will pass the current first condition and proceed to App::outputResponseJson rather than being treated as a stream. Furthermore, during the processing of App::terminate, the JSON will be added with a new property portals (which creates a useless property for those using ATK to create API services).

Additionally, it is incorrect to pass the stream to the App::outputResponse method, where a stream is created and must be a string.

Implementing the proposed changes will address these issues and provide a cleaner solution.

Please let me know your thoughts on this proposal, @mvorisek and @DarkSide666. so @DarkSide666 or I can complete #33 and open a new PR on Atk/Ui

@DarkSide666
Copy link
Member

I like that.

Instead of
$stream = (new \Nyholm\Psr7\Factory\Psr17Factory())->createStreamFromResource($resource)
I was using
$stream = \Nyholm\Psr7\Stream::create($resource)
but I guess that's the same thing at the end.

@mvorisek
Copy link
Member Author

mvorisek commented Apr 4, 2023

Changes to App::terminate

The proposed if ($output instanceof StreamInterface) { placed as the first if looks reasonable.

https://github.com/atk4/ui/blob/4e5fc69160677/src/App.php#L403 Content-Type will still be asserted to be set.

The if should look more like:

if ($output instanceof StreamInterface) {
    $this->response = $this->response->withBody($output);
    $this->outputResponse('');

} elseif ($type === 'application/json') {

to trigger other assertions like unexpected output and/or headers already set in https://github.com/atk4/ui/blob/4e5fc69160677/src/App.php#L1109-L1121.

@DarkSide666
Copy link
Member

OK, but I'm not sure about this part https://github.com/atk4/ui/blob/4e5fc69160677/src/App.php#L1131. I think it will overwrite Stream with empty data then isn't it?
Maybe it should be like this then?

if ($data !== '') {
    $this->response->getBody()->write($data);
}

@mvorisek
Copy link
Member Author

mvorisek commented Apr 4, 2023

I expect the (empty string) data appended.

@DarkSide666
Copy link
Member

Proably this PR should be closed in favor of #33

@mvorisek
Copy link
Member Author

mvorisek commented Apr 5, 2023

Proably this PR should be closed in favor of #33

I will finish this PR, please rebase your #33 on top of this PR.

@mvorisek mvorisek marked this pull request as ready for review April 8, 2023 11:29
@mvorisek mvorisek merged commit 0f384eb into develop Apr 8, 2023
@mvorisek mvorisek deleted the adjust_latest_ui branch April 8, 2023 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants