| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
namespace Ratchet\WebSocket;
|
|
|
3 |
use Ratchet\WebSocket\Version\VersionInterface;
|
|
|
4 |
use Guzzle\Http\Message\RequestInterface;
|
|
|
5 |
|
|
|
6 |
/**
|
|
|
7 |
* Manage the various versions of the WebSocket protocol
|
|
|
8 |
* This accepts interfaces of versions to enable/disable
|
|
|
9 |
*/
|
|
|
10 |
class VersionManager {
|
|
|
11 |
/**
|
|
|
12 |
* The header string to let clients know which versions are supported
|
|
|
13 |
* @var string
|
|
|
14 |
*/
|
|
|
15 |
private $versionString = '';
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Storage of each version enabled
|
|
|
19 |
* @var array
|
|
|
20 |
*/
|
|
|
21 |
protected $versions = array();
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Get the protocol negotiator for the request, if supported
|
|
|
25 |
* @param \Guzzle\Http\Message\RequestInterface $request
|
|
|
26 |
* @throws \InvalidArgumentException
|
|
|
27 |
* @return \Ratchet\WebSocket\Version\VersionInterface
|
|
|
28 |
*/
|
|
|
29 |
public function getVersion(RequestInterface $request) {
|
|
|
30 |
foreach ($this->versions as $version) {
|
|
|
31 |
if ($version->isProtocol($request)) {
|
|
|
32 |
return $version;
|
|
|
33 |
}
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
throw new \InvalidArgumentException("Version not found");
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* @param \Guzzle\Http\Message\RequestInterface
|
|
|
41 |
* @return bool
|
|
|
42 |
*/
|
|
|
43 |
public function isVersionEnabled(RequestInterface $request) {
|
|
|
44 |
foreach ($this->versions as $version) {
|
|
|
45 |
if ($version->isProtocol($request)) {
|
|
|
46 |
return true;
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
return false;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Enable support for a specific version of the WebSocket protocol
|
|
|
55 |
* @param \Ratchet\WebSocket\Version\VersionInterface $version
|
|
|
56 |
* @return VersionManager
|
|
|
57 |
*/
|
|
|
58 |
public function enableVersion(VersionInterface $version) {
|
|
|
59 |
$this->versions[$version->getVersionNumber()] = $version;
|
|
|
60 |
|
|
|
61 |
if (empty($this->versionString)) {
|
|
|
62 |
$this->versionString = (string)$version->getVersionNumber();
|
|
|
63 |
} else {
|
|
|
64 |
$this->versionString .= ", {$version->getVersionNumber()}";
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
return $this;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Disable support for a specific WebSocket protocol version
|
|
|
72 |
* @param int $versionId The version ID to un-support
|
|
|
73 |
* @return VersionManager
|
|
|
74 |
*/
|
|
|
75 |
public function disableVersion($versionId) {
|
|
|
76 |
unset($this->versions[$versionId]);
|
|
|
77 |
|
|
|
78 |
$this->versionString = implode(',', array_keys($this->versions));
|
|
|
79 |
|
|
|
80 |
return $this;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Get a string of version numbers supported (comma delimited)
|
|
|
85 |
* @return string
|
|
|
86 |
*/
|
|
|
87 |
public function getSupportedVersionString() {
|
|
|
88 |
return $this->versionString;
|
|
|
89 |
}
|
|
|
90 |
}
|