3 * TCallbackResponseAdapter and TCallbackResponseWriter class file.
5 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Web.UI.ActiveControls
13 * TCallbackResponseAdapter alters the THttpResponse's outputs.
15 * A TCallbackResponseWriter is used instead of the TTextWrite when
16 * createHtmlWriter is called. Each call to createHtmlWriter will create
17 * a new TCallbackResponseWriter. When flushContent() is called each
18 * instance of TCallbackResponseWriter's content is flushed.
20 * The callback response data can be set using the {@link setResponseData ResponseData}
23 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
24 * @package System.Web.UI.ActiveControls
27 class TCallbackResponseAdapter extends THttpResponseAdapter
30 * @var TCallbackResponseWriter[] list of writers.
32 private $_writers=array();
34 * @var mixed callback response data.
38 private $_redirectUrl=null;
41 * Returns a new instance of THtmlWriter.
42 * An instance of TCallbackResponseWriter is created to hold the content.
43 * @param string writer class name.
44 * @param THttpResponse http response handler.
46 public function createNewHtmlWriter($type,$response)
48 $writer = new TCallbackResponseWriter();
49 $this->_writers[] = $writer;
50 return parent::createNewHtmlWriter($type,$writer);
54 * Flushes the contents in the writers.
56 public function flushContent()
58 foreach($this->_writers as $writer)
59 echo $writer->flush();
60 parent::flushContent();
64 * @param mixed callback response data.
66 public function setResponseData($data)
72 * @return mixed callback response data.
74 public function getResponseData()
80 * Delay the redirect until we process the rest of the page.
81 * @param string new url to redirect to.
83 public function httpRedirect($url)
86 $url=$this->getRequest()->getBaseUrl().$url;
87 $this->_redirectUrl=str_replace('&','&',$url);
91 * @return string new url for callback response to redirect to.
93 public function getRedirectedUrl()
95 return $this->_redirectUrl;
100 * TCallbackResponseWriter class.
102 * TCallbackResponseWriter class enclosed a chunck of content within a
103 * html comment boundary. This allows multiple chuncks of content to return
104 * in the callback response and update multiple HTML elements.
106 * The {@link setBoundary Boundary} property sets boundary identifier in the
107 * HTML comment that forms the boundary. By default, the boundary identifier
108 * is generated using microtime.
110 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
111 * @package System.Web.UI.ActiveControls
114 class TCallbackResponseWriter extends TTextWriter
117 * @var string boundary ID
122 * Constructor. Generates unique boundary ID using microtime.
124 public function __construct()
126 parent::__construct();
127 $this->_boundary = sprintf('%x',crc32(uniqid(null, true)));
131 * @return string boundary identifier.
133 public function getBoundary()
135 return $this->_boundary;
139 * @param string boundary identifier.
141 public function setBoundary($value)
143 $this->_boundary = $value;
147 * Returns the text content wrapped within a HTML comment with boundary
148 * identifier as its comment content.
149 * @return string text content chunck.
151 public function flush()
153 $content = parent::flush();
156 return '<!--'.$this->getBoundary().'-->'.$content.'<!--//'.$this->getBoundary().'-->';