Completed
Push — master ( 8aed6d...e71704 )
by Brad
02:25
created
src/Protocol/WebSocket/MessageQueue.php 1 patch
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -36,7 +36,8 @@
 block discarded – undo
36 36
 
37 37
     public function shift($size)
38 38
     {
39
-        if ($size > strlen($this->data)) {
39
+        if ($size > strlen($this->data))
40
+        {
40 41
             return false;
41 42
         }
42 43
 
Please login to merge, or discard this patch.
src/Protocol/WebSocket/Frame.php 1 patch
Braces   +39 added lines, -18 removed lines patch added patch discarded remove patch
@@ -33,12 +33,14 @@  discard block
 block discarded – undo
33 33
      */
34 34
     public static function parse($data)
35 35
     {
36
-        if (!($data instanceof MessageQueue)) {
36
+        if (!($data instanceof MessageQueue))
37
+        {
37 38
             $data = new MessageQueue($data);
38 39
         }
39 40
         $frame = new static();
40 41
         $frameSize = $frame->decode($data);
41
-        if (!$frame->isCoalesced()) {
42
+        if (!$frame->isCoalesced())
43
+        {
42 44
             return $frameSize;
43 45
         }
44 46
         $data->shift($frameSize);
@@ -128,7 +130,8 @@  discard block
 block discarded – undo
128 130
     public function generateMaskingKey()
129 131
     {
130 132
         $maskingKey = '';
131
-        for ($i = 0; $i < static::MASK_LENGTH; $i++) {
133
+        for ($i = 0; $i < static::MASK_LENGTH; $i++)
134
+        {
132 135
             $maskingKey .= pack('C', rand(0, 255));
133 136
         }
134 137
 
@@ -138,7 +141,8 @@  discard block
 block discarded – undo
138 141
     protected function applyMask($maskingKey, $payload = null)
139 142
     {
140 143
         $applied = '';
141
-        for ($i = 0, $len = strlen($payload); $i < $len; $i++) {
144
+        for ($i = 0, $len = strlen($payload); $i < $len; $i++)
145
+        {
142 146
             $applied .= $payload[$i] ^ $maskingKey[$i % static::MASK_LENGTH];
143 147
         }
144 148
 
@@ -147,7 +151,8 @@  discard block
 block discarded – undo
147 151
 
148 152
     protected function maskPayload($payload)
149 153
     {
150
-        if (!$this->isMask()) {
154
+        if (!$this->isMask())
155
+        {
151 156
             return $payload;
152 157
         }
153 158
         $maskingKey = $this->generateMaskingKey();
@@ -158,20 +163,27 @@  discard block
 block discarded – undo
158 163
     public function getPayloadLength($encodedData)
159 164
     {
160 165
         $length = $this->secondByte & 0x7f;
161
-        if ($length < 126) {
166
+        if ($length < 126)
167
+        {
162 168
             return [$length, 0];
163 169
         }
164 170
 
165
-        if ($length == 126) { // with 2 bytes extended payload length
166
-            if (($packedPayloadLength = substr($encodedData, 2, 2)) === false) {
171
+        if ($length == 126)
172
+        {
173
+// with 2 bytes extended payload length
174
+            if (($packedPayloadLength = substr($encodedData, 2, 2)) === false)
175
+            {
167 176
                 return [0, 0];
168 177
             }
169 178
 
170 179
             return [unpack("n", $packedPayloadLength)[1] + 2, 2];
171 180
         }
172 181
 
173
-        if ($length == 127) { //with 8 bytes extended payload length
174
-            if (($packedPayloadLength = substr($encodedData, 2, 8)) === false) {
182
+        if ($length == 127)
183
+        {
184
+//with 8 bytes extended payload length
185
+            if (($packedPayloadLength = substr($encodedData, 2, 8)) === false)
186
+            {
175 187
                 return [0, 0];
176 188
             }
177 189
             $payloadLength = unpack("N2", $packedPayloadLength);
@@ -183,28 +195,33 @@  discard block
 block discarded – undo
183 195
     public function decode($encodedData)
184 196
     {
185 197
         $this->isCoalesced = false;
186
-        if (strlen($encodedData) <= 2) {
198
+        if (strlen($encodedData) <= 2)
199
+        {
187 200
             return static::DECODE_STATUS_MORE_DATA;
188 201
         }
189 202
         $bytes = unpack("C2", $encodedData);
190 203
         $this->setFirstByte($bytes[1]);
191 204
         $this->setSecondByte($bytes[2]);
192 205
 
193
-        if (!$this->verifyPayload()) {
206
+        if (!$this->verifyPayload())
207
+        {
194 208
             return static::DECODE_STATUS_ERROR;
195 209
         }
196 210
         list($payloadLength, $extendedPayloadBytes) = $this->getPayloadLength($encodedData);
197 211
         $totalFramLength = 2 + $payloadLength;
198
-        if ($this->isMask()) {
212
+        if ($this->isMask())
213
+        {
199 214
             $totalFramLength += static::MASK_LENGTH;
200 215
         }
201
-        if ($payloadLength == 0 || strlen($encodedData) < $totalFramLength) {
216
+        if ($payloadLength == 0 || strlen($encodedData) < $totalFramLength)
217
+        {
202 218
             return static::DECODE_STATUS_MORE_DATA;
203 219
         }
204 220
         $maskingKey = substr($encodedData, 2 + $extendedPayloadBytes, static::MASK_LENGTH);
205 221
         $data = $this->applyMask($maskingKey, substr($encodedData, 2 + $extendedPayloadBytes + static::MASK_LENGTH));
206 222
         $this->setData($data);
207
-        if (strlen($encodedData) >= $totalFramLength) {
223
+        if (strlen($encodedData) >= $totalFramLength)
224
+        {
208 225
             $this->isCoalesced = true;
209 226
         }
210 227
 
@@ -233,7 +250,8 @@  discard block
 block discarded – undo
233 250
 
234 251
     protected function verifyPayload()
235 252
     {
236
-        if ($this->getRSV1() && $this->getRSV2() && $this->getRSV3()) {
253
+        if ($this->getRSV1() && $this->getRSV2() && $this->getRSV3())
254
+        {
237 255
             return false;
238 256
         }
239 257
 
@@ -256,13 +274,16 @@  discard block
 block discarded – undo
256 274
     {
257 275
         $secondByte &= 0x80;
258 276
         $size = strlen($data);
259
-        if ($size < 126) {
277
+        if ($size < 126)
278
+        {
260 279
             $secondByte |= $size;
261 280
 
262 281
             return;
263 282
         }
264 283
 
265
-        if ($size <= 65535) {  //use 2 bytes extended payload
284
+        if ($size <= 65535)
285
+        {
286
+//use 2 bytes extended payload
266 287
             $secondByte |= 126;
267 288
             $extendedPayload = pack("n", $size);
268 289
 
Please login to merge, or discard this patch.
src/Protocol/WebSocket/WebSocket.php 1 patch
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -37,7 +37,7 @@
 block discarded – undo
37 37
             strtolower($request->headers->get('Upgrade')) != 'websocket' ||
38 38
             !$this->checkProtocolVrsion($request) ||
39 39
             !$this->checkSecKey($key)
40
-          ){
40
+          ) {
41 41
             return false;
42 42
         }
43 43
         $acceptKey = $this->generateAcceptKey($key);
Please login to merge, or discard this patch.
src/Protocol/WebSocketProtocol.php 1 patch
Braces   +8 added lines, -4 removed lines patch added patch discarded remove patch
@@ -25,7 +25,8 @@  discard block
 block discarded – undo
25 25
 
26 26
     protected function handshake(HttpFoundation\Request $request)
27 27
     {
28
-        if (!($handshakeResponse = $this->websocket->handshake($request))) {
28
+        if (!($handshakeResponse = $this->websocket->handshake($request)))
29
+        {
29 30
             $this->conn->write(new Response('bad protocol', 400), true);
30 31
             return;
31 32
         }
@@ -35,7 +36,8 @@  discard block
 block discarded – undo
35 36
     protected function initEvent()
36 37
     {
37 38
         $this->websocket = new WebSocket\WebSocket();
38
-        $this->conn->on('data', function($data){
39
+        $this->conn->on('data', function($data)
40
+        {
39 41
             $this->onData($data);
40 42
         });
41 43
     }
@@ -43,10 +45,12 @@  discard block
 block discarded – undo
43 45
     protected function onData($data)
44 46
     {
45 47
         $frame = $this->websocket->onMessage($data);
46
-        if(!($frame instanceof WebSocket\Frame)) {
48
+        if(!($frame instanceof WebSocket\Frame))
49
+        {
47 50
             return;
48 51
         }
49
-        if(($command = json_decode($frame->getData(), true)) === null){
52
+        if(($command = json_decode($frame->getData(), true)) === null)
53
+        {
50 54
             return;
51 55
         }
52 56
         $this->conn->emit('command', array($command));
Please login to merge, or discard this patch.
src/Protocol/HttpProtocol.php 1 patch
Braces   +18 added lines, -9 removed lines patch added patch discarded remove patch
@@ -20,14 +20,16 @@  discard block
 block discarded – undo
20 20
 
21 21
     protected function initEvent(SocketServer $socket)
22 22
     {
23
-        $socket->on('connection', function(SocketConnection $conn){
23
+        $socket->on('connection', function(SocketConnection $conn)
24
+        {
24 25
             $this->onConnect($conn);
25 26
         });
26 27
     }
27 28
 
28 29
     protected function onConnect(SocketConnection $conn)
29 30
     {
30
-        $conn->on('data', function($data) use($conn){
31
+        $conn->on('data', function($data) use($conn)
32
+        {
31 33
             $this->onData($conn, $data);
32 34
         });
33 35
     }
@@ -40,7 +42,8 @@  discard block
 block discarded – undo
40 42
 
41 43
     protected function handleRequest(SocketConnection $conn, Request $request)
42 44
     {
43
-        switch($request->getPathInfo()){
45
+        switch($request->getPathInfo())
46
+        {
44 47
             case '/livereload':
45 48
                 $this->initWebSocket($conn, $request);
46 49
                 break;
@@ -63,7 +66,8 @@  discard block
 block discarded – undo
63 66
 
64 67
     protected function getRequestChangedFiles(Request $request)
65 68
     {
66
-        if(($files = $request->query->get('files')) != null){
69
+        if(($files = $request->query->get('files')) != null)
70
+        {
67 71
             return explode(',', $files);
68 72
         }
69 73
         $requestJson = json_decode($request->getContent(), true);
@@ -72,7 +76,8 @@  discard block
 block discarded – undo
72 76
 
73 77
     protected function notifyChanged(SocketConnection $conn, Request $request)
74 78
     {
75
-        foreach($this->getRequestChangedFiles($request) as $file){
79
+        foreach($this->getRequestChangedFiles($request) as $file)
80
+        {
76 81
             $this->app->getOutput()->writeln(strftime('%T')." - info - Receive request reload $file", OutputInterface::VERBOSITY_VERBOSE);
77 82
             $this->app->reloadFile($file);
78 83
         }
@@ -82,7 +87,8 @@  discard block
 block discarded – undo
82 87
 
83 88
     protected function serveFile(SocketConnection $conn, $file)
84 89
     {
85
-        if(($path = realpath($file)) === null){
90
+        if(($path = realpath($file)) === null)
91
+        {
86 92
             return ;
87 93
         }
88 94
         $content = file_get_contents($file);
@@ -101,7 +107,8 @@  discard block
 block discarded – undo
101 107
     protected function doHttpHandshake($data)
102 108
     {
103 109
         $pos = strpos($data, "\r\n\r\n");
104
-        if($pos === false){
110
+        if($pos === false)
111
+        {
105 112
             return false;
106 113
         }
107 114
         $body = substr($data, $pos + 4);
@@ -119,8 +126,10 @@  discard block
 block discarded – undo
119 126
     protected function parseHeaders($rawHeaders)
120 127
     {
121 128
         $headers = array();
122
-        foreach($rawHeaders as $headerLine){
123
-            if(($pos = strpos($headerLine, ':')) === false){
129
+        foreach($rawHeaders as $headerLine)
130
+        {
131
+            if(($pos = strpos($headerLine, ':')) === false)
132
+            {
124 133
                 continue;
125 134
             }
126 135
             $headers['HTTP_'.str_replace('-', '_', trim(strtoupper(substr($headerLine, 0, $pos))))] = trim(substr($headerLine, $pos + 1));
Please login to merge, or discard this patch.
src/Protocol/LivereloadProtocol.php 1 patch
Braces   +8 added lines, -4 removed lines patch added patch discarded remove patch
@@ -41,10 +41,12 @@  discard block
 block discarded – undo
41 41
 
42 42
     protected function initEvent()
43 43
     {
44
-        $this->conn->on('command', function($command){
44
+        $this->conn->on('command', function($command)
45
+        {
45 46
             $this->dispatchCommand($command);
46 47
         });
47
-        $this->conn->on('close', function(){
48
+        $this->conn->on('close', function()
49
+        {
48 50
             $this->shutdown();
49 51
         });
50 52
     }
@@ -62,7 +64,8 @@  discard block
 block discarded – undo
62 64
 
63 65
     protected function dispatchCommand($command)
64 66
     {
65
-        switch($command['command']){
67
+        switch($command['command'])
68
+        {
66 69
             case 'hello':
67 70
                 $this->processCommandHello($command);
68 71
                 break;
@@ -73,7 +76,8 @@  discard block
 block discarded – undo
73 76
 
74 77
     protected function processCommandHello($command)
75 78
     {
76
-        if($this->connected){
79
+        if($this->connected)
80
+        {
77 81
            return;
78 82
         }
79 83
         $this->app->getOutput()->writeln(strftime('%T')." - info - Livereload protocol initialized.", OutputInterface::VERBOSITY_VERBOSE);
Please login to merge, or discard this patch.
src/Command/RunCommand.php 1 patch
Braces   +6 added lines, -3 removed lines patch added patch discarded remove patch
@@ -30,7 +30,8 @@  discard block
 block discarded – undo
30 30
         $output->writeln('Quit the server with CONTROL-C.');
31 31
         $output->writeln('');
32 32
         $app = new ServerApplication($host, $port);
33
-        if(!$noWatching){
33
+        if(!$noWatching)
34
+        {
34 35
             $config = $this->loadConfig($input, $output);
35 36
             $app->watching($config['period'], $config);
36 37
         }
@@ -41,10 +42,12 @@  discard block
 block discarded – undo
41 42
     protected function loadConfig(InputInterface $input, OutputInterface $output)
42 43
     {
43 44
         $configFile = $input->getOption('config');
44
-        if($configFile === null){
45
+        if($configFile === null)
46
+        {
45 47
             $configFile = 'livereload.json';
46 48
         }
47
-        if(!file_exists($configFile)){
49
+        if(!file_exists($configFile))
50
+        {
48 51
             throw new \Exception("$configFile not found.");
49 52
         }
50 53
 
Please login to merge, or discard this patch.
src/Command/InitCommand.php 1 patch
Braces   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -22,7 +22,8 @@
 block discarded – undo
22 22
     protected function execute(InputInterface $input, OutputInterface $output)
23 23
     {
24 24
         $forceRewrite = $input->getOption('force');
25
-        if(!$forceRewrite && file_exists('livereload.json')){
25
+        if(!$forceRewrite && file_exists('livereload.json'))
26
+        {
26 27
             $output->writeln("<error>livereload.json file exists.\nplease use --force to overwrite.</error>");
27 28
             return;
28 29
         }
Please login to merge, or discard this patch.
src/Application/ServerApplication.php 1 patch
Braces   +22 added lines, -11 removed lines patch added patch discarded remove patch
@@ -52,23 +52,29 @@  discard block
 block discarded – undo
52 52
     {
53 53
         $this->watchConfig = $config;
54 54
         $this->scanFiles();
55
-        $this->loop->addPeriodicTimer($time, function(){
55
+        $this->loop->addPeriodicTimer($time, function()
56
+        {
56 57
             $this->watchingFileChange();
57 58
         });
58 59
     }
59 60
 
60 61
     protected function scanFiles($scanNewFile = true)
61 62
     {
62
-        foreach($this->watchConfig['watch'] as $path => $file){
63
+        foreach($this->watchConfig['watch'] as $path => $file)
64
+        {
63 65
             $finder = new Finder();
64
-            try{
65
-                foreach($finder->in($path)->name($file)->followLinks() as $file){
66
-                    if($file->getRealPath() && !isset($this->watchingFiles[$file->getRealpath()])){
66
+            try
67
+            {
68
+                foreach($finder->in($path)->name($file)->followLinks() as $file)
69
+                {
70
+                    if($file->getRealPath() && !isset($this->watchingFiles[$file->getRealpath()]))
71
+                    {
67 72
                         $this->watchingFiles[$file->getRealpath()] = $scanNewFile?$file->getMTime():0;
68 73
                     }
69 74
                 }
70 75
             }
71
-            catch(\InvalidArgumentException $e){
76
+            catch(\InvalidArgumentException $e)
77
+            {
72 78
                 continue;
73 79
             }
74 80
         }
@@ -77,9 +83,11 @@  discard block
 block discarded – undo
77 83
     protected function watchingFileChange()
78 84
     {
79 85
         $this->scanFiles(false);
80
-        foreach($this->watchingFiles as $file => $time){
86
+        foreach($this->watchingFiles as $file => $time)
87
+        {
81 88
             $mtime = @filemtime($file);
82
-            if($mtime && $mtime > $time){
89
+            if($mtime && $mtime > $time)
90
+            {
83 91
                 $this->watchingFiles[$file] = $mtime;
84 92
                 $this->reloadFile($file);
85 93
             }
@@ -100,7 +108,8 @@  discard block
 block discarded – undo
100 108
 
101 109
     public function addClient(Protocol\LivereloadProtocol $client)
102 110
     {
103
-        if(!in_array($client, $this->clients)){
111
+        if(!in_array($client, $this->clients))
112
+        {
104 113
             $this->clients[] = $client;
105 114
         }
106 115
     }
@@ -108,7 +117,8 @@  discard block
 block discarded – undo
108 117
     public function removeClient(Protocol\LivereloadProtocol $client)
109 118
     {
110 119
         $index = array_search($client, $this->clients, true);
111
-        if($index === false){
120
+        if($index === false)
121
+        {
112 122
             return;
113 123
         }
114 124
         unset($this->clients[$index]);
@@ -116,7 +126,8 @@  discard block
 block discarded – undo
116 126
 
117 127
     public function reloadFile($file)
118 128
     {
119
-        foreach($this->clients as $client){
129
+        foreach($this->clients as $client)
130
+        {
120 131
             $client->reload($file, $this->config);
121 132
         }
122 133
     }
Please login to merge, or discard this patch.