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

Replace CallbackStream with Stream #36

Open
basz opened this issue Oct 11, 2020 · 2 comments
Open

Replace CallbackStream with Stream #36

basz opened this issue Oct 11, 2020 · 2 comments

Comments

@basz
Copy link
Collaborator

basz commented Oct 11, 2020

using mezzio-swoole in front of the this package is problematic because this package uses a CallbackStream. mezzio-swoole isn't capable of using that currently (there is a PR for that mezzio/mezzio-swoole#31). However, even when that PR get's merged, the CallbackStream Psr7Assets echo's the contents which doesn't work with mezzio-swoole. The contents ends up in the log not in the browser :-).

https://github.com/harikt/psr7-asset/blob/master/src/AssetResponder.php#L110

A simple but somewhat naive fix would be change the echo into a return;

        $callable = function () use ($path) {
            $file = new SplFileObject($path);
            $content = '';
            while (! $file->eof()) {
                $content .= $file->fgets();
            }

            return $content;
        };

This would -however- defeat the purpose of being capable of streaming large files. So in my project I have overwritten the AssetResponder and am using a Diactoros Stream as a response. That works great!

<?php

namespace MyApp;

use Laminas\Diactoros\Stream;
use Psr\Http\Message\ResponseInterface;

class AssetResponder extends \Hkt\Psr7Asset\AssetResponder
{
    /**
     * Sets a 200 OK response with the asset contents.
     *
     * @return ResponseInterface
     */
    protected function ok()
    {
        $response = $this->responseFactory->createResponse(200);
        return $response
            ->withBody(new Stream($this->data->asset->path))
            ->withHeader('Content-Length', (string) filesize($this->data->asset->path))
            ->withHeader('Content-Type', $this->data->asset->type);
    }
}

Before I create PR, this would introduce a dependency on laminas-diactoros or... I could write a minimal Stream implementation s was done with the CallbackStream class...

@harikt
Copy link
Owner

harikt commented Aug 8, 2023

Interesting.. If you are willing you can send a PR.

Sorry for the long wait.

@basz
Copy link
Collaborator Author

basz commented Aug 8, 2023

see #37 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants