Add the Router and RouteMap components for http2.
[hoplite.git] / http2 / response.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2016 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\http2;
18
19 require_once HOPLITE_ROOT . '/http/response_code.php';
20
21 class Response
22 {
23 /*! @var integer The HTTP response code to return. */
24 public $code;
25
26 /*! @var array A map of headers to values to be sent with the response. */
27 public $headers = [];
28
29 public function __construct($code=\hoplite\http\ResponseCode::OK) {
30 $this->code = $code;
31 }
32
33 /*! @var string Raw HTTP response body. */
34 public function generate() {
35 http_response_code($this->code);
36 foreach ($this->headers as $header => $value) {
37 header("$header: $value");
38 }
39 }
40 }
41
42 class NotFoundResponse extends Response {
43 public function __construct() {
44 parent::__construct(\hoplite\http\ResponseCode::NOT_FOUND);
45 }
46
47 public function generate() {
48 parent::generate();
49 print '<h1>404 - Not Found</h1>';
50 }
51 }
52
53 class TextResponse extends Response {
54 private $text;
55
56 public function __construct($text) {
57 parent::__construct();
58 $this->text = $text;
59 $this->headers['Content-Type'] = 'text/plain';
60 }
61
62 public function generate() {
63 parent::generate();
64 print $this->text;
65 }
66 }