3 * TCallbackResponseAdapter and TCallbackResponseWriter class file.
5 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2013 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @version $Id: TCallbackResponseAdapter.php 3245 2013-01-07 20:23:32Z ctrlaltca $
10 * @package System.Web.UI.ActiveControls
14 * TCallbackResponseAdapter alters the THttpResponse's outputs.
16 * A TCallbackResponseWriter is used instead of the TTextWrite when
17 * createHtmlWriter is called. Each call to createHtmlWriter will create
18 * a new TCallbackResponseWriter. When flushContent() is called each
19 * instance of TCallbackResponseWriter's content is flushed.
21 * The callback response data can be set using the {@link setResponseData ResponseData}
24 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
25 * @version $Id: TCallbackResponseAdapter.php 3245 2013-01-07 20:23:32Z ctrlaltca $
26 * @package System.Web.UI.ActiveControls
29 class TCallbackResponseAdapter extends THttpResponseAdapter
32 * @var TCallbackResponseWriter[] list of writers.
34 private $_writers=array();
36 * @var mixed callback response data.
40 private $_redirectUrl=null;
43 * Returns a new instance of THtmlWriter.
44 * An instance of TCallbackResponseWriter is created to hold the content.
45 * @param string writer class name.
46 * @param THttpResponse http response handler.
48 public function createNewHtmlWriter($type,$response)
50 $writer = new TCallbackResponseWriter();
51 $this->_writers[] = $writer;
52 return parent::createNewHtmlWriter($type,$writer);
56 * Flushes the contents in the writers.
58 public function flushContent()
60 foreach($this->_writers as $writer)
61 echo $writer->flush();
62 parent::flushContent();
66 * @param mixed callback response data.
68 public function setResponseData($data)
74 * @return mixed callback response data.
76 public function getResponseData()
82 * Delay the redirect until we process the rest of the page.
83 * @param string new url to redirect to.
85 public function httpRedirect($url)
88 $url=$this->getRequest()->getBaseUrl().$url;
89 $this->_redirectUrl=str_replace('&','&',$url);
93 * @return string new url for callback response to redirect to.
95 public function getRedirectedUrl()
97 return $this->_redirectUrl;
102 * TCallbackResponseWriter class.
104 * TCallbackResponseWriter class enclosed a chunck of content within a
105 * html comment boundary. This allows multiple chuncks of content to return
106 * in the callback response and update multiple HTML elements.
108 * The {@link setBoundary Boundary} property sets boundary identifier in the
109 * HTML comment that forms the boundary. By default, the boundary identifier
110 * is generated using microtime.
112 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
113 * @version $Id: TCallbackResponseAdapter.php 3245 2013-01-07 20:23:32Z ctrlaltca $
114 * @package System.Web.UI.ActiveControls
117 class TCallbackResponseWriter extends TTextWriter
120 * @var string boundary ID
125 * Constructor. Generates unique boundary ID using microtime.
127 public function __construct()
129 $this->_boundary = sprintf('%x',crc32(microtime()));
133 * @return string boundary identifier.
135 public function getBoundary()
137 return $this->_boundary;
141 * @param string boundary identifier.
143 public function setBoundary($value)
145 $this->_boundary = $value;
149 * Returns the text content wrapped within a HTML comment with boundary
150 * identifier as its comment content.
151 * @return string text content chunck.
153 public function flush()
155 $content = parent::flush();
158 return '<!--'.$this->getBoundary().'-->'.$content.'<!--//'.$this->getBoundary().'-->';