Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
90.91% covered (success)
90.91%
10 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Server
93.33% covered (success)
93.33%
14 / 15
90.91% covered (success)
90.91%
10 / 11
12.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createWithBasePath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 request
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 response
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHeadersAsString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sendHeaders
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 send
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sendAndExit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 render
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Http;
15
16/**
17 * HTTP server class
18 *
19 * @category   Pop
20 * @package    Pop\Http
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    5.2.0
25 * @property   $request Server\Request
26 * @property   $response Server\Response
27 */
28class Server extends AbstractHttp
29{
30
31    /**
32     * Instantiate the server object
33     *
34     * @param ?Server\Request  $request
35     * @param ?Server\Response $response
36     */
37    public function __construct(
38        ?Server\Request $request = new Server\Request(new Uri()), ?Server\Response $response = new Server\Response()
39    )
40    {
41        parent::__construct($request, $response);
42    }
43
44    /**
45     * Create server object with a base path reference for the request URI
46     *
47     * @param  string $basePath
48     * @return Server
49     */
50    public static function createWithBasePath(string $basePath): Server
51    {
52        return new self(new Server\Request(new Uri(null, $basePath)));
53    }
54
55    /**
56     * Get request (shorthand)
57     *
58     * @return Server\Request
59     */
60    public function request(): Server\Request
61    {
62        return $this->request;
63    }
64
65    /**
66     * Get response (shorthand)
67     *
68     * @return Server\Response
69     */
70    public function response(): Server\Response
71    {
72        return $this->response;
73    }
74
75    /**
76     * Get the response headers as a string
77     *
78     * @param  mixed  $status
79     * @param  string $eol
80     * @return string
81     */
82    public function getHeadersAsString(mixed $status = null, string $eol = "\r\n"): string
83    {
84        return $this->response->getHeadersAsString($status, $eol);
85    }
86
87    /**
88     * Send response headers
89     *
90     * @throws Exception
91     * @return void
92     */
93    public function sendHeaders(): void
94    {
95        $this->response->sendHeaders();
96    }
97
98    /**
99     * Send the server response
100     *
101     * @return void
102     */
103    public function send(?int $code = null, ?array $headers = null, bool $length = false): void
104    {
105        $this->response->send($code, $headers, $length);
106    }
107
108
109    /**
110     * Send the server response and exit
111     *
112     * @return void
113     */
114    public function sendAndExit(?int $code = null, ?array $headers = null, bool $length = false): void
115    {
116        $this->response->sendAndExit($code, $headers, $length);
117    }
118
119    /**
120     * Render response as a raw string
121     *
122     * @return string
123     */
124    public function render(): string
125    {
126        return ($this->response !== null) ? $this->response->render() : '';
127    }
128
129    /**
130     * Render response as a raw string
131     *
132     * @return string
133     */
134    public function __toString(): string
135    {
136        return $this->render();
137    }
138
139    /**
140     * Magic method to get the request or response object
141     *
142     * @param  string $name
143     * @return mixed
144     */
145    public function __get(string $name): mixed
146    {
147        return match ($name) {
148            'request'  => $this->request,
149            'response' => $this->response,
150            default    => null,
151        };
152    }
153
154}