aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-12 09:12:09 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-12 09:20:03 +0200
commit0bde21c4469221a3c8f743fa8b122db915cb86f6 (patch)
treee717dcfacb0d0c704ff582113c6c9207e2ee23e1
parentb87086f6df550163bf9a9ab1c60104600de27eca (diff)
downloadEventsource-0bde21c4469221a3c8f743fa8b122db915cb86f6.zip
Eventsource-0bde21c4469221a3c8f743fa8b122db915cb86f6.tar.gz
Eventsource-0bde21c4469221a3c8f743fa8b122db915cb86f6.tar.bz2
Move to PSR-1 and PSR-2.
-rw-r--r--Documentation/En/Index.xyl37
-rw-r--r--Documentation/Fr/Index.xyl37
-rw-r--r--Exception.php10
-rw-r--r--README.md14
-rw-r--r--Server.php90
5 files changed, 83 insertions, 105 deletions
diff --git a/Documentation/En/Index.xyl b/Documentation/En/Index.xyl
index d284485..6d1beee 100644
--- a/Documentation/En/Index.xyl
+++ b/Documentation/En/Index.xyl
@@ -50,25 +50,20 @@
var output = document.getElementById('output');
try {
-
- var source = new EventSource('Server.php');
- source.onopen = function ( ) {
-
+ var source = new EventSource('Server.php');
+ source.onopen = function () {
output.appendChild(document.createElement('hr'));
return;
};
- source.onmessage = function ( evt ) {
-
+ source.onmessage = function (evt) {
var samp = document.createElement('samp');
samp.innerHTML = evt.data + '\n';
output.appendChild(samp);
return;
};
-}
-catch ( e ) {
-
+} catch (e) {
console.log(e);
}
&amp;lt;/script></code></pre>
@@ -81,8 +76,7 @@ catch ( e ) {
as first argument. This data can contain different newline characters:
<code>\n</code>, <code>\r</code> and even <code>\r\n</code>. Our server will
write an infinity of messages every second:</p>
- <pre><code class="language-php">while(true) {
-
+ <pre><code class="language-php">while (true) {
$server->send(time());
sleep(1);
}</code></pre>
@@ -94,8 +88,7 @@ catch ( e ) {
<strong>classify</strong> these data based on <strong>associated</strong>
event names. For example, to associate all the data to the <code>tick</code>
event, we will write:</p>
- <pre data-line="3"><code class="language-php">while(true) {
-
+ <pre data-line="2"><code class="language-php">while (true) {
$server->tick->send(time());
sleep(1);
}</code></pre>
@@ -108,10 +101,9 @@ catch ( e ) {
<p>If we set an event name for our data, consequently we have to modify the
client by using <code>addEventListener</code> instead of
<code>onmessage</code>:</p>
- <pre data-line-offset="11" data-line="14"><code class="language-javascript"> return;
+ <pre data-line-offset="9" data-line="12"><code class="language-javascript"> return;
};
- source.addEventListener('tick', function ( evt ) {
-
+ source.addEventListener('tick', function (evt) {
var samp = document.createElement('samp');
samp.innerHTML = evt.data + '\n';</code></pre>
<p>Let's restart the server. The message is handled for a
@@ -160,8 +152,7 @@ catch ( e ) {
$server->tick->send('last ID is ' . $id);
++$id;
-for($i = mt_rand(2, 5); $i >= 0; --$i) {
-
+for ($i = mt_rand(2, 5); $i >= 0; --$i) {
$server->tick->send(time(), $id);
sleep(1);
}</code></pre>
@@ -183,13 +174,11 @@ for($i = mt_rand(2, 5); $i >= 0; --$i) {
<code>Hoa\Eventsource\Exception</code> exception from its constructor. It is
possible to catch it in order to print our own error, such as:</p>
<pre><code class="language-php">try {
-
$server = new Hoa\Eventsource\Server();
-}
-catch ( Hoa\Eventsource\Exception $e ) {
-
- echo 'You must send a request with ',
- '“Accept: ', Hoa\Eventsource\Server::MIME_TYPE, '”.', "\n";
+} catch (Hoa\Eventsource\Exception $e) {
+ echo
+ 'You must send a request with ',
+ '“Accept: ', Hoa\Eventsource\Server::MIME_TYPE, '”.', "\n";
exit;
}
diff --git a/Documentation/Fr/Index.xyl b/Documentation/Fr/Index.xyl
index b0cdca4..0064232 100644
--- a/Documentation/Fr/Index.xyl
+++ b/Documentation/Fr/Index.xyl
@@ -50,25 +50,20 @@
var output = document.getElementById('output');
try {
-
- var source = new EventSource('Server.php');
- source.onopen = function ( ) {
-
+ var source = new EventSource('Server.php');
+ source.onopen = function () {
output.appendChild(document.createElement('hr'));
return;
};
- source.onmessage = function ( evt ) {
-
+ source.onmessage = function (evt) {
var samp = document.createElement('samp');
samp.innerHTML = evt.data + '\n';
output.appendChild(samp);
return;
};
-}
-catch ( e ) {
-
+} catch (e) {
console.log(e);
}
&amp;lt;/script></code></pre>
@@ -83,8 +78,7 @@ catch ( e ) {
plusieurs natures : <code>\n</code>, <code>\r</code> et même
<code>\r\n</code>. Dans notre serveur, nous allons écrire une infinité de
message et ce, toutes les secondes :</p>
- <pre><code class="language-php">while(true) {
-
+ <pre><code class="language-php">while (true) {
$server->send(time());
sleep(1);
}</code></pre>
@@ -97,8 +91,7 @@ catch ( e ) {
<strong>trier</strong> ces données en les <strong>associant</strong> à des
noms d'événement. Par exemple, pour associer toutes les données à l'événement
<code>tick</code>, nous écrirons :</p>
- <pre data-line="3"><code class="language-php">while(true) {
-
+ <pre data-line="2"><code class="language-php">while (true) {
$server->tick->send(time());
sleep(1);
}</code></pre>
@@ -112,10 +105,9 @@ catch ( e ) {
<p>Si nous précisons un nom d'événement pour nos données, nous devons modifier
le client en conséquence en utilisant <code>addEventListener</code> au lieu de
<code>onmessage</code> :</p>
- <pre data-line-offset="11" data-line="14"><code class="language-javascript"> return;
+ <pre data-line-offset="9" data-line="12"><code class="language-javascript"> return;
};
- source.addEventListener('tick', function ( evt ) {
-
+ source.addEventListener('tick', function (evt) {
var samp = document.createElement('samp');
samp.innerHTML = evt.data + '\n';</code></pre>
<p>Relançons notre client. Le message est bien capturé pour un événement
@@ -165,8 +157,7 @@ catch ( e ) {
$server->tick->send('last ID is ' . $id);
++$id;
-for($i = mt_rand(2, 5); $i >= 0; --$i) {
-
+for ($i = mt_rand(2, 5); $i >= 0; --$i) {
$server->tick->send(time(), $id);
sleep(1);
}</code></pre>
@@ -190,13 +181,11 @@ for($i = mt_rand(2, 5); $i >= 0; --$i) {
Il est possible de la capturer et d'afficher notre propre erreur, comme
ceci :</p>
<pre><code class="language-php">try {
-
$server = new Hoa\Eventsource\Server();
-}
-catch ( Hoa\Eventsource\Exception $e ) {
-
- echo 'You must send a request with ',
- '“Accept: ', Hoa\Eventsource\Server::MIME_TYPE, '”.', "\n";
+} catch (Hoa\Eventsource\Exception $e) {
+ echo
+ 'You must send a request with ',
+ '“Accept: ', Hoa\Eventsource\Server::MIME_TYPE, '”.', "\n";
exit;
}
diff --git a/Exception.php b/Exception.php
index a0c806b..8b318d8 100644
--- a/Exception.php
+++ b/Exception.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -43,9 +43,9 @@ use Hoa\Core;
*
* Extending the \Hoa\Core\Exception class.
*
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Exception extends Core\Exception { }
+class Exception extends Core\Exception
+{
+}
diff --git a/README.md b/README.md
index 1b5bec7..3a045e0 100644
--- a/README.md
+++ b/README.md
@@ -35,8 +35,7 @@ server to the client. The client will display all received events. Thus, in
```php
$server = new Hoa\Eventsource\Server();
-while(true) {
-
+while (true) {
// “tick” is the event name.
$server->tick->send(time());
sleep(1);
@@ -51,25 +50,20 @@ And in `index.html`, our client:
var output = document.getElementById('output');
try {
-
var source = new EventSource('Server.php');
- source.onopen = function ( ) {
-
+ source.onopen = function () {
output.appendChild(document.createElement('hr'));
return;
};
- source.addEventListener('tick', function ( evt ) {
-
+ source.addEventListener('tick', function (evt) {
var samp = document.createElement('samp');
samp.innerHTML = evt.data + '\n';
output.appendChild(samp);
return;
});
-}
-catch ( e ) {
-
+} catch (e) {
console.log(e);
}
</script>
diff --git a/Server.php b/Server.php
index 2576e92..c8053c5 100644
--- a/Server.php
+++ b/Server.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -43,13 +43,11 @@ use Hoa\Http;
*
* A cross-protocol EventSource server.
*
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Server {
-
+class Server
+{
/**
* Mime type.
*
@@ -60,14 +58,14 @@ class Server {
/**
* Current event.
*
- * @var \Hoa\Eventsource\Server string
+ * @var string
*/
protected $_event = null;
/**
* HTTP response.
*
- * @var \Hoa\Http\Response object
+ * @var \Hoa\Http\Response
*/
protected $_response = null;
@@ -76,33 +74,35 @@ class Server {
/**
* Start an event source.
*
- * @access public
* @param bool $verifyHeaders Verify headers or not.
* @return void
- * @throw \Hoa\Eventsource\Exception
+ * @throws \Hoa\Eventsource\Exception
*/
- public function __construct ( $verifyHeaders = true ) {
-
- if(true === $verifyHeaders && true === headers_sent($file, $line))
+ public function __construct($verifyHeaders = true)
+ {
+ if (true === $verifyHeaders && true === headers_sent($file, $line)) {
throw new Exception(
'Headers already sent in %s at line %d, cannot send data ' .
'to client correctly.',
- 0, [$file, $line]);
+ 0,
+ [$file, $line]
+ );
+ }
$mimes = preg_split('#\s*,\s*#', Http\Runtime::getHeader('accept'));
$gotcha = false;
- foreach($mimes as $mime)
- if(0 !== preg_match('#^(\*/\*|' . self::MIME_TYPE . ';?)#', $mime)) {
-
+ foreach ($mimes as $mime) {
+ if (0 !== preg_match('#^(\*/\*|' . self::MIME_TYPE . ';?)#', $mime)) {
$gotcha = true;
+
break;
}
+ }
$this->_response = new Http\Response(false);
- if(false === $gotcha) {
-
+ if (false === $gotcha) {
$this->_response->sendHeader(
'Status',
Http\Response::STATUS_NOT_ACCEPTABLE
@@ -113,7 +113,10 @@ class Server {
);
throw new Exception(
- 'Client does not accept %s.', 1, self::MIME_TYPE);
+ 'Client does not accept %s.',
+ 1,
+ self::MIME_TYPE
+ );
}
$this->_response->sendHeader('Content-Type', self::MIME_TYPE);
@@ -128,15 +131,13 @@ class Server {
/**
* Send an event.
*
- * @access public
* @param string $data Data.
* @param string $id ID (empty string to reset).
* @return void
*/
- public function send ( $data, $id = null ) {
-
- if(null !== $this->_event) {
-
+ public function send($data, $id = null)
+ {
+ if (null !== $this->_event) {
$this->_response->writeAll('event: ' . $this->_event . "\n");
$this->_event = null;
}
@@ -146,12 +147,12 @@ class Server {
preg_replace("#(" . CRLF . "|\n|\r)#", "\n" . 'data: ', $data)
);
- if(null !== $id) {
-
+ if (null !== $id) {
$this->_response->writeAll("\n" . 'id');
- if(!empty($id))
+ if (!empty($id)) {
$this->_response->writeAll(': ' . $id);
+ }
}
$this->_response->writeAll("\n\n");
@@ -163,12 +164,11 @@ class Server {
/**
* Set the reconnection time for the client.
*
- * @access public
* @param int $ms Time in milliseconds.
* @return void
*/
- public function setReconnectionTime ( $ms ) {
-
+ public function setReconnectionTime($ms)
+ {
$this->_response->writeAll('retry: ' . $ms . "\n\n");
$this->_response->flush(true);
@@ -178,20 +178,27 @@ class Server {
/**
* Select an event where to send data.
*
- * @access public
* @param string $event Event.
* @return \Hoa\Eventsource\Server
- * @throw \Hoa\Eventsource\Exception
+ * @throws \Hoa\Eventsource\Exception
*/
- public function __get ( $event ) {
-
- if(false === (bool) preg_match('##u', $event))
+ public function __get($event)
+ {
+ if (false === (bool) preg_match('##u', $event)) {
throw new Exception(
- 'Event name %s must be in UTF-8.', 2, $event);
+ 'Event name %s must be in UTF-8.',
+ 2,
+ $event
+ );
+ }
- if(0 !== preg_match('#[:' . CRLF . ']#u', $event))
+ if (0 !== preg_match('#[:' . CRLF . ']#u', $event)) {
throw new Exception(
- 'Event name %s contains illegal characters.', 3, $event);
+ 'Event name %s contains illegal characters.',
+ 3,
+ $event
+ );
+ }
$this->_event = $event;
@@ -201,11 +208,10 @@ class Server {
/**
* Get last ID.
*
- * @access public
* @return string
*/
- public function getLastId ( ) {
-
+ public function getLastId()
+ {
return Http\Runtime::getHeader('Last-Event-ID') ?: '';
}
}