Stream example

return [
            ['/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'],
            ['/home/Foo/bar.txt', '/var/www/=/files/,/home/Foo/=/baz/', '/baz/bar.txt'],
            ['/home/Foo/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', '/baz/bar.txt'],
            ['/tmp/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', null],
        ];
    }

    public function testStream()
    {
        $request = Request::create('/');
        $response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, ['Content-Type' => 'text/plain']);
        $response->prepare($request);

        $this->assertNull($response->headers->get('Content-Length'));
    }

    public function testPrepareNotAddingContentTypeHeaderIfNoContentResponse()
    {
        $request = Request::create('/');
        $request->headers->set('If-Modified-Since', date('D, d M Y H:i:s').' GMT');

        $response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
        
/** * Message. * * @author Kévin Dunglas <dunglas@gmail.com> */
class Message implements MessageInterface
{
    public function __construct(
        private readonly string $version = '1.1',
        private array $headers = [],
        private readonly StreamInterface $body = new Stream(),
    ) {
    }

    public function getProtocolVersion(): string
    {
        return $this->version;
    }

    public function withProtocolVersion($version): never
    {
        throw new \BadMethodCallException('Not implemented.');
    }
public function __construct(
        private readonly string $filePath,
        private readonly ?int $size = null,
        private readonly int $error = \UPLOAD_ERR_OK,
        private readonly ?string $clientFileName = null,
        private readonly ?string $clientMediaType = null,
    ) {
    }

    public function getStream(): StreamInterface
    {
        return new Stream(file_get_contents($this->filePath));
    }

    public function moveTo($targetPath): void
    {
        rename($this->filePath, $targetPath);
    }

    public function getSize(): ?int
    {
        return $this->size;
    }

    
/** @var StreamInterface */
    private $stream;

    public function __construct(StreamInterface $stream)
    {
        $resource = StreamWrapper::getResource($stream);
        // Specify window=15+32, so zlib will use header detection to both gzip (with header) and zlib data         // See http://www.zlib.net/manual.html#Advanced definition of inflateInit2         // "Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection"         // Default window size is 15.         stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ, ['window' => 15 + 32]);
        $this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource));
    }
}

    public static function streamFor($resource = '', array $options = []): StreamInterface
    {
        if (is_scalar($resource)) {
            $stream = self::tryFopen('php://temp', 'r+');
            if ($resource !== '') {
                fwrite($stream(string) $resource);
                fseek($stream, 0);
            }

            return new Stream($stream$options);
        }

        switch (gettype($resource)) {
            case 'resource':
                /* * The 'php://input' is a special stream with quirks and inconsistencies. * We avoid using that stream by reading it into php://temp */

                /** @var resource $resource */
                if ((\stream_get_meta_data($resource)['uri'] ?? '') === 'php://input') {
                    
/** * We will treat the buffer object as the body of the stream * * @param StreamInterface $stream Stream to cache. The cursor is assumed to be at the beginning of the stream. * @param StreamInterface $target Optionally specify where data is cached */
    public function __construct(
        StreamInterface $stream,
        StreamInterface $target = null
    ) {
        $this->remoteStream = $stream;
        $this->stream = $target ?: new Stream(Utils::tryFopen('php://temp', 'r+'));
    }

    public function getSize(): ?int
    {
        $remoteSize = $this->remoteStream->getSize();

        if (null === $remoteSize) {
            return null;
        }

        return max($this->stream->getSize()$remoteSize);
    }

    private function setStreamOrFile($streamOrFile): void
    {
        if (is_string($streamOrFile)) {
            $this->file = $streamOrFile;
        } elseif (is_resource($streamOrFile)) {
            $this->stream = new Stream($streamOrFile);
        } elseif ($streamOrFile instanceof StreamInterface) {
            $this->stream = $streamOrFile;
        } else {
            throw new InvalidArgumentException(
                'Invalid stream or file provided for UploadedFile'
            );
        }
    }

    /** * @throws InvalidArgumentException */


    public function testCreateRequest()
    {
        $stdClass = new \stdClass();
        $serverRequest = new ServerRequest(
            '1.1',
            [
                'X-Dunglas-API-Platform' => '1.0',
                'X-data' => ['a', 'b'],
            ],
            new Stream('The body'),
            '/about/kevin',
            'GET',
            'http://les-tilleuls.coop/about/kevin',
            ['country' => 'France'],
            ['city' => 'Lille'],
            ['url' => 'http://les-tilleuls.coop'],
            [
                'doc1' => $this->createUploadedFile('Doc 1', \UPLOAD_ERR_OK, 'doc1.txt', 'text/plain'),
                'nested' => [
                    'docs' => [
                        $this->createUploadedFile('Doc 2', \UPLOAD_ERR_OK, 'doc2.txt', 'text/plain'),
                        
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

/** * @author Kévin Dunglas <dunglas@gmail.com> */
class Response extends Message implements ResponseInterface
{
    public function __construct(
        string $version = '1.1',
        array $headers = [],
        StreamInterface $body = new Stream(),
        private readonly int $statusCode = 200,
    ) {
        parent::__construct($version$headers$body);
    }

    public function getStatusCode(): int
    {
        return $this->statusCode;
    }

    public function withStatus($code$reasonPhrase = ''): never
    {
Home | Imprint | This part of the site doesn't use cookies.