<commit_before><?php
namespace Wave\Framework\Http;

use Phly\Http\Response;
use Phly\Http\ServerRequest;
use Phly\Http\Stream;
use Psr\Http\Message\RequestInterface;
use Wave\Framework\Http\Response as LinkResponse;
use Wave\Framework\Adapters\Link\Destination;

class Server implements Destination
{
    /**
     * @type ServerRequest
     */
    protected $request;

    /**
     * @type Response
     */
    protected $response;

    /**
     * @type callable
     */
    protected $callback;

    /**
     * @param $request Request
     */

    protected $bufferLevel;

    /**
     * Accepts a request object and switches it's Stream to 'php://input',
     * which allows to retrieve the body of the request (usually in POST requests).
     * And will construct a ResponseInterface object as linkable which is then picked
     * up by the application class and linked.
     *
     * @param $request RequestInterface
     */
    public function __construct($request)
    {
        if (!$request instanceof RequestInterface && !$request instanceof Request) {
            throw new \InvalidArgumentException(
                sprintf('Class "%s" does not implement RequestInterface', get_class($request))
            );
        }
        $this->request = $request->withBody(new Stream('php://input'), 'r+b');
        $this->response = new LinkResponse(new Response());
    }

    /**
     * Invokes the callback provided in Wave::run
     *
     * @param callable $callback
     * @return $this
     */
    public function listen(callable $callback = null)
    {
        ob_start();
        $this->bufferLevel = ob_get_level();
        $result = call_user_func($callback, $this->request, $this->response);

        if (is_array($result) || is_object($result) || is_callable($result)) {
            throw new \RuntimeException(
                sprintf(
                    'Unexpected result type "%s" from callback,' .
                    ' valid return types are all except: callable, array, object',
                    gettype($result)
                )
            );
        }

        $this->response->getBody()->write($result);

        return $this;
    }

    /**<commit_msg>Start output buffering with `ob_gzhandler` to allow compression if supported<commit_after><?php
namespace Wave\Framework\Http;

use Phly\Http\Response;
use Phly\Http\ServerRequest;
use Phly\Http\Stream;
use Psr\Http\Message\RequestInterface;
use Wave\Framework\Http\Response as LinkResponse;
use Wave\Framework\Adapters\Link\Destination;

class Server implements Destination
{
    /**
     * @type ServerRequest
     */
    protected $request;

    /**
     * @type Response
     */
    protected $response;

    /**
     * @type callable
     */
    protected $callback;

    /**
     * @param $request Request
     */

    protected $bufferLevel;

    /**
     * Accepts a request object and switches it's Stream to 'php://input',
     * which allows to retrieve the body of the request (usually in POST requests).
     * And will construct a ResponseInterface object as linkable which is then picked
     * up by the application class and linked.
     *
     * @param $request RequestInterface
     */
    public function __construct($request)
    {
        if (!$request instanceof RequestInterface && !$request instanceof Request) {
            throw new \InvalidArgumentException(
                sprintf('Class "%s" does not implement RequestInterface', get_class($request))
            );
        }
        $this->request = $request->withBody(new Stream('php://input'), 'r+b');
        $this->response = new LinkResponse(new Response());
    }

    /**
     * Starts output buffering with `ob_gzhandler` and
     * invokes the callback provided in Wave::run
     *
     * @param callable $callback
     * @return $this
     */
    public function listen(callable $callback = null)
    {
        ob_start('ob_gzhandler');
        $this->bufferLevel = ob_get_level();
        $result = call_user_func($callback, $this->request, $this->response);

        if (is_array($result) || is_object($result) || is_callable($result)) {
            throw new \RuntimeException(
                sprintf(
                    'Unexpected result type "%s" from callback,' .
                    ' valid return types are all except: callable, array, object',
                    gettype($result)
                )
            );
        }

        $this->response->getBody()->write($result);

        return $this;
    }

    /**
{'fraction_non_alphanumeric': 0.11434280639431617, 'fraction_numerical': 0.0, 'mean_word_length': 2.2503607503607506, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '108984', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1172, 'n_words': 337}