aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/restapi/renderer/DebugRenderer.php
blob: afd56f62b29564155a97ed48a20f252b8d7374c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace RESTAPI\Renderer;

/**
 * Debug content renderer.
 *
 * @author     Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @author     <mlunzena@uos.de>
 * @license    GPL 2 or later
 * @since      Stud.IP 3.0
 * @deprecated Since Stud.IP 5.0. Will be removed in Stud.IP 6.0.
 */
class DebugRenderer extends DefaultRenderer
{
    /**
     * Returns an associated content type.
     */
    public function contentType()
    {
        return 'text/plain';
    }

    /**
     * Returns an associated extension.
     */
    public function extension()
    {
        return '.debug';
    }

    /**
     * Response transformation function.
     *
     * @param \RESTAPI\Response $response  the response to transform
     */
    public function render($response)
    {
        if (!isset($response['Content-Type'])) {
            $response['Content-Type'] = $this->contentType() . ';charset=utf-8';
        }

        $debug = function ($label, $data) {
            echo str_pad('', 78, '=') . PHP_EOL;
            echo str_pad('- ' . $label, 77, ' ') . '-' . PHP_EOL;
            echo str_pad('', 78, '=') . PHP_EOL;
            var_export($data);
            echo PHP_EOL;
        };

        ob_start();
        $debug('Response Status', $response->status);
        $debug('Response Header', $response->headers);
        $debug('Response Body',   $response->body);
        $debug('Request', $GLOBALS['_' . $_SERVER['REQUEST_METHOD']]);
        $response->body = ob_get_clean();
    }
}