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

Allow to prepare response outside of Request #40

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,29 @@ public function setContent($content)

/**
* Sends the response to the client.
* @param bool $return Send to stdout or return object. If set to `true` method will return prepared Response object.
* @return Response
* @throws HeadersAlreadySentException
* @throws InvalidConfigException
*/
public function send()
public function send($return = false)
Copy link
Contributor

Choose a reason for hiding this comment

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

Now we have send() that does not send. :D

Copy link
Member

@samdark samdark Jan 11, 2019

Choose a reason for hiding this comment

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

How about getData(): string?

Copy link
Author

Choose a reason for hiding this comment

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

It's a little confusing. Technically you send it but not in stdout but to the variable.

Copy link
Author

Choose a reason for hiding this comment

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

Like with print_r function.

Copy link
Author

Choose a reason for hiding this comment

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

Also logically it suppose to call events that you already have in send() when i requesting filled response...

Copy link
Member

Choose a reason for hiding this comment

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

Aha. Then maybe process() that returns data? Then send() will look like echo process().

Copy link
Author

@Alex-Bond Alex-Bond Jan 12, 2019

Choose a reason for hiding this comment

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

@samdark that can work. What about events EVENT_BEFORE_SEND and EVENT_AFTER_SEND? Technically we sending content somewhere. Or these events apply only to stdout print?
I found $response->on(Response::EVENT_AFTER_SEND, [$this, 'cacheResponse']); in PageCache. If we dont call it when we working with RR cache will be newer generated.

Copy link
Author

@Alex-Bond Alex-Bond Jan 12, 2019

Choose a reason for hiding this comment

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

Actually PageCache will never work with PSR-7 server. It requesting data from ob_get_clean() which always will be empty. Or we rewrite it so it will collect data from Response, not ob_get_clean().

Copy link
Member

Choose a reason for hiding this comment

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

Sending events should be called for sending only. We can add two additional events for processing. Roadrunner-like server and traditional server are two similar but different models so there may be need to do things a bit differently.

{
if ($this->isSent) {
return;
return $this;
}

$this->trigger(self::EVENT_BEFORE_SEND);
$this->prepare();
$this->trigger(self::EVENT_AFTER_PREPARE);
$this->sendHeaders();
$this->sendContent();

if(!$return) {
$this->sendHeaders();
$this->sendContent();
}

$this->trigger(self::EVENT_AFTER_SEND);
$this->isSent = true;
return $this;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Exception;
use RuntimeException;
use yii\helpers\StringHelper;
use yii\http\MemoryStream;
use yii\web\HttpException;

/**
Expand Down Expand Up @@ -95,6 +96,20 @@ protected function generateTestFileContent()
return '12ёжик3456798áèabcdefghijklmnopqrstuvwxyz!"§$%&/(ёжик)=?';
}

public function testSendToReturn()
{
$testBodyContent = "Test response";
$this->response->data = $testBodyContent;

ob_start();
$psrResponse = $this->response->send(true);
$content = ob_get_clean();

static::assertEquals('', $content);
static::assertEquals(200, $this->response->statusCode);
static::assertEquals($psrResponse->getBody()->getContents(), $testBodyContent);
}

/**
* @see https://github.com/yiisoft/yii2/issues/7529
*/
Expand Down