Completed
Push — master ( 3dffac...5d90b9 )
by Thierry
02:25
created

Manager::getOutput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Manager.php - Jaxon Response Manager
5
 *
6
 * This class stores and tracks the response that will be returned after processing a request.
7
 * The Response Manager represents a single point of contact for working with <Response> objects.
8
 *
9
 * @package jaxon-core
10
 * @author Jared White
11
 * @author J. Max Wilson
12
 * @author Joseph Woolley
13
 * @author Steffen Konerow
14
 * @author Thierry Feuzeu <[email protected]>
15
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
16
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
17
 * @copyright 2016 Thierry Feuzeu <[email protected]>
18
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
19
 * @link https://github.com/jaxon-php/jaxon-core
20
 */
21
22
namespace Jaxon\Response;
23
24
class Manager
25
{
26
    use \Jaxon\Utils\Traits\Translator;
27
28
    /**
29
     * The current response object that will be sent back to the browser
30
     * once the request processing phase is complete
31
     *
32
     * @var \Jaxon\Response\Response
33
     */
34
    private $xResponse;
35
36
    /**
37
     * The debug messages
38
     *
39
     * @var array
40
     */
41
    private $aDebugMessages;
42
43
    public function __construct()
44
    {
45
        $this->xResponse = null;
46
        $this->aDebugMessages = array();
47
    }
48
49
    /**
50
     * Clear the current response
51
     *
52
     * A new response will need to be appended before the request processing is complete.
53
     *
54
     * @return void
55
     */
56
    public function clear()
57
    {
58
        $this->xResponse = null;
59
    }
60
61
    /**
62
     * Append one response object onto the end of another
63
     *
64
     * You cannot append a given response onto the end of a response of different type.
65
     * If no prior response has been appended, this response becomes the main response
66
     * object to which other response objects will be appended.
67
     *
68
     * @param Response        $xResponse            The response object to be appended
69
     *
70
     * @return void
71
     */
72
    public function append(Response $xResponse)
73
    {
74
        if(!$this->xResponse)
75
        {
76
            $this->xResponse = $xResponse;
77
        }
78
        else if(get_class($this->xResponse) == get_class($xResponse))
79
        {
80
            if($this->xResponse != $xResponse)
81
            {
82
                $this->xResponse->appendResponse($xResponse);
83
            }
84
        }
85
        else
86
        {
87
            $this->debug($this->trans('errors.mismatch.types', array('class' => get_class($xResponse))));
88
        }
89
    }
90
91
    /**
92
     * Appends a debug message on the end of the debug message queue
93
     *
94
     * Debug messages will be sent to the client with the normal response
95
     * (if the response object supports the sending of debug messages, see: <Response>)
96
     *
97
     * @param string        $sMessage            The debug message
98
     *
99
     * @return void
100
     */
101
    public function debug($sMessage)
102
    {
103
        $this->aDebugMessages[] = $sMessage;
104
    }
105
106
    /**
107
     * Prints the debug messages into the current response object
108
     *
109
     * @return void
110
     */
111
    public function printDebug()
112
    {
113
        if(($this->xResponse))
114
        {
115
            foreach($this->aDebugMessages as $sMessage)
116
            {
117
                $this->xResponse->debug($sMessage);
118
            }
119
            $this->aDebugMessages = array();
120
        }
121
    }
122
123
    /**
124
     * Sends the HTTP headers back to the browser
125
     *
126
     * @return void
127
     */
128
    public function sendHeaders()
129
    {
130
        if(($this->xResponse))
131
        {
132
            $this->xResponse->sendHeaders();
133
        }
134
    }
135
136
    /**
137
     * Get the JSON output of the response
138
     *
139
     * @return string
140
     */
141
    public function getOutput()
142
    {
143
        if(($this->xResponse))
144
        {
145
            return $this->xResponse->getOutput();
146
        }
147
        return '';
148
    }
149
150
    /**
151
     * Prints the response object to the output stream, thus sending the response to the browser
152
     *
153
     * @return void
154
     */
155
    public function sendOutput()
156
    {
157
        if(($this->xResponse))
158
        {
159
            $this->xResponse->sendHeaders();
160
            $this->xResponse->printOutput();
161
        }
162
    }
163
}
164