Hoa central
Server.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Eventsource;
38 
39 use Hoa\Http;
40 
49 class Server
50 {
56  const MIME_TYPE = 'text/event-stream';
57 
63  protected $_event = null;
64 
70  protected $_response = null;
71 
72 
73 
81  public function __construct($verifyHeaders = true)
82  {
83  if (true === $verifyHeaders && true === headers_sent($file, $line)) {
84  throw new Exception(
85  'Headers already sent in %s at line %d, cannot send data ' .
86  'to client correctly.',
87  0,
88  [$file, $line]
89  );
90  }
91 
92  $mimes = preg_split('#\s*,\s*#', Http\Runtime::getHeader('accept'));
93  $gotcha = false;
94 
95  foreach ($mimes as $mime) {
96  if (0 !== preg_match('#^(\*/\*|' . self::MIME_TYPE . ';?)#', $mime)) {
97  $gotcha = true;
98 
99  break;
100  }
101  }
102 
103  $this->_response = new Http\Response(false);
104 
105  if (false === $gotcha) {
106  $this->_response->sendHeader(
107  'Status',
108  Http\Response::STATUS_NOT_ACCEPTABLE
109  );
110  $this->_response->sendHeader(
111  'Content-Type',
112  'text/plain; charset=utf-8'
113  );
114 
115  throw new Exception(
116  'Client does not accept %s.',
117  1,
118  self::MIME_TYPE
119  );
120  }
121 
122  $this->_response->sendHeader('Content-Type', self::MIME_TYPE);
123  $this->_response->sendHeader('Transfer-Encoding', 'identity');
124  $this->_response->sendHeader('Cache-Control', 'no-cache');
125  $this->_response->sendHeader('X-Accel-Buffering', 'no');
126  $this->_response->newBuffer();
127 
128  return;
129  }
130 
138  public function send($data, $id = null)
139  {
140  if (null !== $this->_event) {
141  $this->_response->writeAll('event: ' . $this->_event . "\n");
142  $this->_event = null;
143  }
144 
145  $this->_response->writeAll(
146  'data: ' .
147  preg_replace("#(" . CRLF . "|\n|\r)#", "\n" . 'data: ', $data)
148  );
149 
150  if (null !== $id) {
151  $this->_response->writeAll("\n" . 'id');
152 
153  if (!empty($id)) {
154  $this->_response->writeAll(': ' . $id);
155  }
156  }
157 
158  $this->_response->writeAll("\n\n");
159  $this->_response->flush(true);
160 
161  return;
162  }
163 
170  public function setReconnectionTime($ms)
171  {
172  $this->_response->writeAll('retry: ' . $ms . "\n\n");
173  $this->_response->flush(true);
174 
175  return;
176  }
177 
185  public function __get($event)
186  {
187  if (false === (bool) preg_match('##u', $event)) {
188  throw new Exception(
189  'Event name %s must be in UTF-8.',
190  2,
191  $event
192  );
193  }
194 
195  if (0 !== preg_match('#[:' . CRLF . ']#u', $event)) {
196  throw new Exception(
197  'Event name %s contains illegal characters.',
198  3,
199  $event
200  );
201  }
202 
203  $this->_event = $event;
204 
205  return $this;
206  }
207 
213  public function getLastId()
214  {
215  return Http\Runtime::getHeader('Last-Event-ID') ?: '';
216  }
217 }
__construct($verifyHeaders=true)
Definition: Server.php:81
static getHeader($header)
Definition: Runtime.php:181
send($data, $id=null)
Definition: Server.php:138