Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Why init stream property in the constructor? #54

Closed
k00ni opened this issue Jun 12, 2015 · 4 comments
Closed

Why init stream property in the constructor? #54

k00ni opened this issue Jun 12, 2015 · 4 comments
Labels

Comments

@k00ni
Copy link

k00ni commented Jun 12, 2015

In the Response class constructor, the stream property gets initialized:

$this->stream = ($body instanceof StreamInterface) ? $body : new Stream($body, 'wb+');

It has no explicit property definition, which means it is public, isn't it? But its usage is not specified in the ResponseInterface of the PSR-7.

My problem is, that the Response class creates files in my project folder. Their name is the same string which i used as $body before. I am wondering how to deal with it, because if i implement some specific code to deal with the stream afterwards it will most likely not be compatible with another PSR-7 implementation.

@Maks3w
Copy link
Member

Maks3w commented Jun 12, 2015

  1. Attribute is public, is the way opted by this library for to preserve the inmutability without to create more methods or instances.
  2. MessageInterface::getBody() force the object must return a StreamInterface. This could be lazy load or at constructor time.

@weierophinney
Copy link
Member

$stream is in MessageTrait and defined as private: https://github.com/zendframework/zend-diactoros/blob/master/src/MessageTrait.php#L41-L44

My problem is, that the Response class creates files in my project folder.

The Response constructor indicates that you should be providing a stream, which can be one of the following:

  • a PHP resource
  • a StreamInterface implementation (such as Zend\Diactoros\Stream)

If you provide a string value, Response interprets this as a stream identifier to use to create a stream; in other words, it assumes it is a filesystem path. This is also documented. (See https://github.com/zendframework/zend-diactoros/blob/master/src/Response.php#L108)

If you are trying to pass a string value, your options are:

// Write to the stream after creating the response:
$response = new Response();
$response->getBody()->write($string);

// Create a stream first:
$stream = new Stream('php://temp');
$stream->write($string);
$response = new Response($stream);

Or wait until 1.1.0, when #52 is added, and you can do this:

$response = StringResponse::html($html); // HTML contents
$response = StringResponse::json($data); //JSON contents

@weierophinney
Copy link
Member

@Maks3w You're incorrect; the property is defined in MessageTrait.

@k00ni
Copy link
Author

k00ni commented Jun 12, 2015

Thanks for the fast answers. I will look into it.

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

No branches or pull requests

3 participants