1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Mvc\Splash; |
4
|
|
|
|
5
|
|
|
use Mouf\Html\HtmlElement\HtmlElementInterface; |
6
|
|
|
use Zend\Diactoros\Response; |
7
|
|
|
use Zend\Diactoros\Stream; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class is a Symfony 2 response that takes in parameter a HtmlElementInterface element and will render it. |
11
|
|
|
* |
12
|
|
|
* @author David Négrier <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class HtmlResponse extends Response |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var HtmlElementInterface |
18
|
|
|
*/ |
19
|
|
|
protected $htmlElement; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Stream |
23
|
|
|
*/ |
24
|
|
|
protected $stream; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor. |
28
|
|
|
* |
29
|
|
|
* @param HtmlElementInterface $htmlElement An HtmlElement to render. |
30
|
|
|
* @param int $status The response status code |
31
|
|
|
* @param array $headers An array of response headers |
32
|
|
|
*/ |
33
|
|
|
public function __construct(HtmlElementInterface $htmlElement, $status = 200, $headers = array()) |
34
|
|
|
{ |
35
|
|
|
parent::__construct('php://temp', $status, $headers); |
36
|
|
|
|
37
|
|
|
$this->htmlElement = $htmlElement; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public static function create(HtmlElementInterface $htmlElement, $status = 200, $headers = array()) |
44
|
|
|
{ |
45
|
|
|
return new static($htmlElement, $status, $headers); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sets the HtmlElement to be rendered. |
50
|
|
|
* |
51
|
|
|
* @param HtmlElementInterface $htmlElement |
52
|
|
|
*/ |
53
|
|
|
public function setHtmlElement(HtmlElementInterface $htmlElement) |
54
|
|
|
{ |
55
|
|
|
$this->htmlElement = $htmlElement; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the HtmlElement to be rendered. |
60
|
|
|
* |
61
|
|
|
* @return \Mouf\Html\HtmlElement\HtmlElementInterface |
62
|
|
|
*/ |
63
|
|
|
public function getHtmlElement() |
64
|
|
|
{ |
65
|
|
|
return $this->htmlElement; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Gets the body of the message. |
70
|
|
|
* |
71
|
|
|
* @return StreamInterface Returns the body as a stream. |
72
|
|
|
*/ |
73
|
|
|
public function getBody() |
74
|
|
|
{ |
75
|
|
|
if ($this->stream === null) { |
76
|
|
|
ob_start(); |
77
|
|
|
$this->htmlElement->toHtml(); |
78
|
|
|
$content = ob_get_clean(); |
79
|
|
|
$this->stream = new Stream('php://memory', 'wb+'); |
80
|
|
|
$this->stream->write($content); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->stream; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|