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

Splitting StringResponse into many smaller classes #61

Merged
merged 7 commits into from
Jun 24, 2015

Conversation

weierophinney
Copy link
Member

I would like to suggest splitting the new StringResponse into 2 smaller classes: JsonResponse and HtmlResponse.

Here is the rationale:

StringResponse is actually a StringResponseFactory. It is final, so it cannot be extended, and it provides only 2 static methods: html and json. However, there are a bunch of other perfectly viable responses that are also "strings". For instance, why is there no xml method? As time goes by, it is likely that this class will start growing and growing up to the point where it is not maintainable anymore.

Also, yesterday, I tried to implement my own response. I ended up copy-pasting the createResponse method in my own class. I think this method is very valuable and should be exposed.

Would you be ok if I submit a pull request splitting StringResponse like this:

$htmlResponse = HtmlResponse::create($html, $status, $headers);

$jsonResponse = JsonResponse::create($html, $status, $headers);

Both JsonResponse and HtmlResponse may extend an abstract StringResponse that provides a protected createResponse method.

Does it make sense? Shall I work on a pull-request about this?

@weierophinney
Copy link
Member

I was thinking the same when we created the EmptyResponse and the RedirectResponse; these are simpler to read, and easier to find. If you don't get to it today, I will. :)

weierophinney added a commit to weierophinney/zend-diactoros that referenced this pull request Jun 24, 2015
Per discussion started on zendframework#61, this pull request removes the
`StringResponse` factory class in favor of two dedicated response types,
one for each of HTML and JSON responses.

In order to achieve that goal, it introduces the
`InjectContentTypeTrait`, which will search for a Content-Type header in
the provided headers, and, if none is found, inject the provided
Content-Type.

This also allowed extracting the logic for encoding data to JSON to
another method.
weierophinney added a commit to weierophinney/zend-diactoros that referenced this pull request Jun 24, 2015
@weierophinney weierophinney added this to the 1.1.0 milestone Jun 24, 2015
@weierophinney weierophinney self-assigned this Jun 24, 2015
@weierophinney
Copy link
Member

@moufmouf I've already completed it, if you want to review.

@weierophinney
Copy link
Member

Ping @franzliedke - this is a proposed rewrite of the factories, for the reasons outlined in the issues description.

@moufmouf
Copy link
Contributor Author

Ho, damn, I was working on the same thing (in #62). I definitely like the InjectContentTypeTrait!

I'll add comments directly in the commits.

@franzliedke
Copy link
Contributor

Awesome. 👍

@weierophinney
Copy link
Member

@moufmouf I in turn like the error handling of #62. Want to send a PR against my branch to add that? or shall I borrow the concepts?

$data = (array) $data;
}

return json_encode($data, JSON_UNESCAPED_SLASHES);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symfony is using by default the JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT options.
It seems it makes RFC4627-compliant JSON, which may also be embedded into HTML safely.

Also, in my implementation, I added a fourth parameter (https://github.com/moufmouf/zend-diactoros/blob/develop/src/Response/JsonResponse.php#L36) to allow passing any JSON encoding options. It might be helpful to some people I think.

@moufmouf
Copy link
Contributor Author

@weierophinney PR is coming on your branch :)

@Maks3w
Copy link
Member

Maks3w commented Jun 24, 2015

This is a BC break because StringResponse class is removed

@Maks3w Maks3w removed this from the 1.1.0 milestone Jun 24, 2015
@franzliedke
Copy link
Contributor

@Maks3w But that class was never part of any public release, was it?

@weierophinney
Copy link
Member

@Maks3w not a BC break, as this has only been on the develop branch, and not in a release so far.

Adding error handling via exceptions and JSON options parameter
- Per @moufmouf on zendframework#62, this patch adds some basic error handling to the
  `HtmlResponse` to ensure we have a either a string, or a
  StreamInterface instance, raising an InvalidArgumentException if not.
@weierophinney
Copy link
Member

@moufmouf Merged your pull request, and added error handling to the HtmlResponse.

- PHP 5.4 is a bit more noisy about the inability to encode. Detect it
  early when possible.
- As there's no way to make that condition fail on that platform.
@weierophinney
Copy link
Member

Okay, finally tests and coverage are looking good. :)

@weierophinney weierophinney added this to the 1.1.0 milestone Jun 24, 2015
@weierophinney weierophinney merged commit ae653c8 into zendframework:develop Jun 24, 2015
weierophinney added a commit that referenced this pull request Jun 24, 2015
Combined entries from #52, #58, #59, and #61 into one single narrative entry.
weierophinney added a commit that referenced this pull request Jun 24, 2015
@weierophinney weierophinney deleted the feature/61 branch June 24, 2015 20:40
Fooriva added a commit to Fooriva/whoops-middleware that referenced this pull request Apr 27, 2016
Handle method return HtmlResponse instead of StringResponse which does not exist anymore in Zend/Diactoros : zendframework/zend-diactoros#61
@robincafolla robincafolla mentioned this pull request Oct 29, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants