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
15.07
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
4
1<?php
2/**
3 * Pop PHP Framework (https://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@noladev.com>
7 * @copyright  Copyright (c) 2009-2025 NOLA Interactive, LLC.
8 * @license    https://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@noladev.com>
22 * @copyright  Copyright (c) 2009-2025 NOLA Interactive, LLC.
23 * @license    https://www.popphp.org/license     New BSD License
24 * @version    5.3.2
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     * @param  mixed  $filters
49     * @param  mixed  $streamToFile
50     * @return Server
51     */
52    public static function createWithBasePath(string $basePath, mixed $filters = null, mixed $streamToFile = null): Server
53    {
54        return new self(Server\Request::createWithBasePath($basePath, $filters, $streamToFile));
55    }
56
57    /**
58     * Get request (shorthand)
59     *
60     * @return Server\Request
61     */
62    public function request(): Server\Request
63    {
64        return $this->request;
65    }
66
67    /**
68     * Get response (shorthand)
69     *
70     * @return Server\Response
71     */
72    public function response(): Server\Response
73    {
74        return $this->response;
75    }
76
77    /**
78     * Get the response headers as a string
79     *
80     * @param  mixed  $status
81     * @param  string $eol
82     * @return string
83     */
84    public function getHeadersAsString(mixed $status = null, string $eol = "\r\n"): string
85    {
86        return $this->response->getHeadersAsString($status, $eol);
87    }
88
89    /**
90     * Send response headers
91     *
92     * @throws Exception
93     * @return void
94     */
95    public function sendHeaders(): void
96    {
97        $this->response->sendHeaders();
98    }
99
100    /**
101     * Send the server response
102     *
103     * @return void
104     */
105    public function send(?int $code = null, ?array $headers = null, bool $length = false): void
106    {
107        $this->response->send($code, $headers, $length);
108    }
109
110
111    /**
112     * Send the server response and exit
113     *
114     * @return void
115     */
116    public function sendAndExit(?int $code = null, ?array $headers = null, bool $length = false): void
117    {
118        $this->response->sendAndExit($code, $headers, $length);
119    }
120
121    /**
122     * Render response as a raw string
123     *
124     * @return string
125     */
126    public function render(): string
127    {
128        return ($this->response !== null) ? $this->response->render() : '';
129    }
130
131    /**
132     * Render response as a raw string
133     *
134     * @return string
135     */
136    public function __toString(): string
137    {
138        return $this->render();
139    }
140
141    /**
142     * Magic method to get the request or response object
143     *
144     * @param  string $name
145     * @return mixed
146     */
147    public function __get(string $name): mixed
148    {
149        return match ($name) {
150            'request'  => $this->request,
151            'response' => $this->response,
152            default    => null,
153        };
154    }
155
156}