1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\Http\PhpEnvironment;
11
12use Zend\Http\Header\MultipleHeaderInterface;
13use Zend\Http\Response as HttpResponse;
14
15/**
16 * HTTP Response for current PHP environment
17 */
18class Response extends HttpResponse
19{
20    /**
21     * The current used version
22     * (The value will be detected on getVersion)
23     *
24     * @var null|string
25     */
26    protected $version;
27
28    /**
29     * @var bool
30     */
31    protected $contentSent = false;
32
33    /**
34     * Return the HTTP version for this response
35     *
36     * @return string
37     * @see \Zend\Http\AbstractMessage::getVersion()
38     */
39    public function getVersion()
40    {
41        if (!$this->version) {
42            $this->version = $this->detectVersion();
43        }
44        return $this->version;
45    }
46
47    /**
48     * Detect the current used protocol version.
49     * If detection failed it falls back to version 1.0.
50     *
51     * @return string
52     */
53    protected function detectVersion()
54    {
55        if (isset($_SERVER['SERVER_PROTOCOL']) && $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {
56            return self::VERSION_11;
57        }
58
59        return self::VERSION_10;
60    }
61
62    /**
63     * @return bool
64     */
65    public function headersSent()
66    {
67        return headers_sent();
68    }
69
70    /**
71     * @return bool
72     */
73    public function contentSent()
74    {
75        return $this->contentSent;
76    }
77
78    /**
79     * Send HTTP headers
80     *
81     * @return Response
82     */
83    public function sendHeaders()
84    {
85        if ($this->headersSent()) {
86            return $this;
87        }
88
89        $status  = $this->renderStatusLine();
90        header($status);
91
92        /** @var \Zend\Http\Header\HeaderInterface $header */
93        foreach ($this->getHeaders() as $header) {
94            if ($header instanceof MultipleHeaderInterface) {
95                header($header->toString(), false);
96                continue;
97            }
98            header($header->toString());
99        }
100
101        $this->headersSent = true;
102        return $this;
103    }
104
105    /**
106     * Send content
107     *
108     * @return Response
109     */
110    public function sendContent()
111    {
112        if ($this->contentSent()) {
113            return $this;
114        }
115
116        echo $this->getContent();
117        $this->contentSent = true;
118        return $this;
119    }
120
121    /**
122     * Send HTTP response
123     *
124     * @return Response
125     */
126    public function send()
127    {
128        $this->sendHeaders()
129             ->sendContent();
130        return $this;
131    }
132}
133