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