Giter Site home page Giter Site logo

symfony-writer's Introduction

vcn/symfony-writer

vcn/symfony-writer is a library that provides an alternative to the StreamedResponse-class of Symfony. In contrast to StreamedResponse, WriterResponse allows you to attach listeners and capture what is being responded.

Installation

Install using composer in your symfony-project:

composer require vcn/symfony-writer

Usage

Usage is similar to usage of the StreamedResponse, with a few small changes:

  • the callback now takes an instance of \Vcn\Symfony\HttpFoundation\Writer as first and only argument
  • instead of echo-ing, you should call Writer::write with the data to output as string parameter
  • before the response is sent, you can attach a listener to the response using Writer::attachListener

Example:

<?php

namespace App;

use Symfony\Component\HttpFoundation\Response;
use Vcn\Symfony\HttpFoundation\Writer\Writer;
use Vcn\Symfony\HttpFoundation\Writer\WriterResponse;

class Controller
{
    public function count(): Response
    {
        $response = new WriterResponse(
            function (Writer $writer) {
                for ($i = 0; $i < 50; $i++) {
                    $writer->write("{$i} ");
                }
            }
        );

        $tmpFile       = tempnam(sys_get_temp_dir(), 'writer-response-');
        $tmpFileHandle = fopen($tmpFile, 'w');
        register_shutdown_function(
            function () use ($tmpFileHandle) {
                @fclose($tmpFileHandle);
            }
        );

        error_log("Copy of response is sent to {$tmpFile}");

        $response->attachListener(
            function (string $data) use ($tmpFileHandle) {
                fwrite($tmpFileHandle, $data);
            }
        );

        return $response;
    }
}

symfony-writer's People

Contributors

droeftoeter avatar gerritdrost avatar

Watchers

James Cloos avatar  avatar Onno Schutze avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.