1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <contact@terryl.in>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * php version 7.1.0
11
 *
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <contact@terryl.in>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20

21
declare(strict_types=1);
22

23
namespace Shieldon\Firewall;
24

25
use Psr\Http\Message\ResponseInterface;
26
use function header;
27
use function headers_sent;
28
use function sprintf;
29
use function stripos;
30

31
/**
32
 * Display the final result.
33
 */
34
class HttpResolver
35
{
36
    /**
37
     * Invoker.
38
     *
39
     * @param ResponseInterface $response The PSR-7 response.
40
     * @param bool              $finally  Terminate current PHP proccess if 
41
     *                                    this value is true.
42
     *
43
     * @return void
44
     */
45 3
    public function __invoke(ResponseInterface $response, $finally = true): void
46
    {
47 3
        if (!headers_sent()) {
48

49 3
            foreach ($response->getHeaders() as $key => $values) {
50 3
                $replace = stripos($key, 'Set-Cookie') === 0 ? false : true;
51 3
                foreach ($values as $value) {
52 3
                    header(sprintf('%s: %s', $key, $value), $replace);
53 3
                    $replace = false;
54
                }
55
            }
56

57 3
            header(
58 3
                sprintf(
59 3
                    'HTTP/%s %s %s',
60 3
                    $response->getProtocolVersion(),
61 3
                    $response->getStatusCode(),
62 3
                    $response->getReasonPhrase()
63
                ), 
64 3
                true,
65 3
                $response->getStatusCode()
66
            );
67
        }
68

69 3
        echo $response->getBody()->getContents();
70

71 3
        if ($finally && !defined('PHP_UNIT_TEST')) {
72

73
            // @codeCoverageIgnoreStart
74
            exit;
75
            // @codeCoverageIgnoreEnd
76
        }
77
    }
78
}

Read our documentation on viewing source code .

Loading