@@ -80,6 +80,9 @@ |
||
80 | 80 | $conn->write($response); |
81 | 81 | } |
82 | 82 | |
83 | + /** |
|
84 | + * @param string $file |
|
85 | + */ |
|
83 | 86 | protected function serveFile(SocketConnection $conn, $file) |
84 | 87 | { |
85 | 88 | if(($path = realpath($file)) === null){ |
@@ -20,7 +20,7 @@ discard block |
||
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 | $this->onConnect($conn); |
25 | 25 | }); |
26 | 26 | } |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | |
41 | 41 | protected function handleRequest(SocketConnection $conn, Request $request) |
42 | 42 | { |
43 | - switch($request->getPathInfo()){ |
|
43 | + switch ($request->getPathInfo()) { |
|
44 | 44 | case '/livereload': |
45 | 45 | $this->initWebSocket($conn, $request); |
46 | 46 | break; |
@@ -58,21 +58,21 @@ discard block |
||
58 | 58 | protected function initWebSocket(SocketConnection $conn, Request $request) |
59 | 59 | { |
60 | 60 | $conn->removeAllListeners('data'); |
61 | - return new WebSocketProtocol($conn, $this->app, $request); |
|
61 | + return new WebSocketProtocol($conn, $this->app, $request); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | protected function getRequestChangedFiles(Request $request) |
65 | 65 | { |
66 | - if(($files = $request->query->get('files')) != null){ |
|
66 | + if (($files = $request->query->get('files')) != null) { |
|
67 | 67 | return explode(',', $files); |
68 | 68 | } |
69 | 69 | $requestJson = json_decode($request->getContent(), true); |
70 | - return isset($requestJson['files'])?(is_array($requestJson['files'])?$requestJson['files']:[$requestJson['files']]):[]; |
|
70 | + return isset($requestJson['files']) ? (is_array($requestJson['files']) ? $requestJson['files'] : [$requestJson['files']]) : []; |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | protected function notifyChanged(SocketConnection $conn, Request $request) |
74 | 74 | { |
75 | - foreach($this->getRequestChangedFiles($request) as $file){ |
|
75 | + foreach ($this->getRequestChangedFiles($request) as $file) { |
|
76 | 76 | $this->app->getOutput()->writeln(strftime('%T')." - info - Receive request reload $file", OutputInterface::VERBOSITY_VERBOSE); |
77 | 77 | $this->app->reloadFile($file); |
78 | 78 | } |
@@ -82,8 +82,8 @@ discard block |
||
82 | 82 | |
83 | 83 | protected function serveFile(SocketConnection $conn, $file) |
84 | 84 | { |
85 | - if(($path = realpath($file)) === null){ |
|
86 | - return ; |
|
85 | + if (($path = realpath($file)) === null) { |
|
86 | + return; |
|
87 | 87 | } |
88 | 88 | $content = file_get_contents($file); |
89 | 89 | $response = new Response($content); |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | protected function doHttpHandshake($data) |
102 | 102 | { |
103 | 103 | $pos = strpos($data, "\r\n\r\n"); |
104 | - if($pos === false){ |
|
104 | + if ($pos === false) { |
|
105 | 105 | return false; |
106 | 106 | } |
107 | 107 | $body = substr($data, $pos + 4); |
@@ -119,8 +119,8 @@ discard block |
||
119 | 119 | protected function parseHeaders($rawHeaders) |
120 | 120 | { |
121 | 121 | $headers = array(); |
122 | - foreach($rawHeaders as $headerLine){ |
|
123 | - if(($pos = strpos($headerLine, ':')) === false){ |
|
122 | + foreach ($rawHeaders as $headerLine) { |
|
123 | + if (($pos = strpos($headerLine, ':')) === false) { |
|
124 | 124 | continue; |
125 | 125 | } |
126 | 126 | $headers['HTTP_'.str_replace('-', '_', trim(strtoupper(substr($headerLine, 0, $pos))))] = trim(substr($headerLine, $pos + 1)); |
@@ -20,14 +20,16 @@ discard block |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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)); |
@@ -21,7 +21,7 @@ |
||
21 | 21 | } |
22 | 22 | public function add($data) |
23 | 23 | { |
24 | - $this->data.=$data; |
|
24 | + $this->data .= $data; |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | public function __toString() |
@@ -36,7 +36,8 @@ |
||
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 |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | |
111 | 111 | public function appendData($data) |
112 | 112 | { |
113 | - $this->data.=$data; |
|
113 | + $this->data .= $data; |
|
114 | 114 | $this->updatePayloadLength($this->data, $this->secondByte, $this->extendedPayload); |
115 | 115 | } |
116 | 116 | |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | } |
153 | 153 | $maskingKey = $this->generateMaskingKey(); |
154 | 154 | |
155 | - return $maskingKey . $this->applyMask($maskingKey, $payload); |
|
155 | + return $maskingKey.$this->applyMask($maskingKey, $payload); |
|
156 | 156 | } |
157 | 157 | |
158 | 158 | public function getPayloadLength($encodedData) |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | { |
250 | 250 | $this->isCoalesced = true; |
251 | 251 | |
252 | - return pack('CC', $this->firstByte, $this->secondByte) . $this->extendedPayload . $this->maskPayload($this->data); |
|
252 | + return pack('CC', $this->firstByte, $this->secondByte).$this->extendedPayload.$this->maskPayload($this->data); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | protected function updatePayloadLength($data, &$secondByte, &$extendedPayload) |
@@ -33,12 +33,14 @@ discard block |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
@@ -37,7 +37,7 @@ |
||
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); |
@@ -33,11 +33,11 @@ |
||
33 | 33 | public function handshake($request) |
34 | 34 | { |
35 | 35 | $key = $request->headers->get('Sec-WebSocket-Key'); |
36 | - if( |
|
36 | + if ( |
|
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); |
@@ -37,7 +37,7 @@ |
||
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); |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | protected function initEvent() |
36 | 36 | { |
37 | 37 | $this->websocket = new WebSocket\WebSocket(); |
38 | - $this->conn->on('data', function($data){ |
|
38 | + $this->conn->on('data', function($data) { |
|
39 | 39 | $this->onData($data); |
40 | 40 | }); |
41 | 41 | } |
@@ -43,10 +43,10 @@ discard block |
||
43 | 43 | protected function onData($data) |
44 | 44 | { |
45 | 45 | $frame = $this->websocket->onMessage($data); |
46 | - if(!($frame instanceof WebSocket\Frame)) { |
|
46 | + if (!($frame instanceof WebSocket\Frame)) { |
|
47 | 47 | return; |
48 | 48 | } |
49 | - if(($command = json_decode($frame->getData(), true)) === null){ |
|
49 | + if (($command = json_decode($frame->getData(), true)) === null) { |
|
50 | 50 | return; |
51 | 51 | } |
52 | 52 | $this->conn->emit('command', array($command)); |
@@ -25,7 +25,8 @@ discard block |
||
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 |
||
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 |
||
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)); |
@@ -74,7 +74,7 @@ |
||
74 | 74 | protected function processCommandHello($command) |
75 | 75 | { |
76 | 76 | if($this->connected){ |
77 | - return; |
|
77 | + return; |
|
78 | 78 | } |
79 | 79 | $this->app->getOutput()->writeln(strftime('%T')." - info - Livereload protocol initialized.", OutputInterface::VERBOSITY_VERBOSE); |
80 | 80 | $this->connected = true; |
@@ -41,10 +41,10 @@ discard block |
||
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 | $this->dispatchCommand($command); |
46 | 46 | }); |
47 | - $this->conn->on('close', function(){ |
|
47 | + $this->conn->on('close', function() { |
|
48 | 48 | $this->shutdown(); |
49 | 49 | }); |
50 | 50 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | |
63 | 63 | protected function dispatchCommand($command) |
64 | 64 | { |
65 | - switch($command['command']){ |
|
65 | + switch ($command['command']) { |
|
66 | 66 | case 'hello': |
67 | 67 | $this->processCommandHello($command); |
68 | 68 | break; |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | |
74 | 74 | protected function processCommandHello($command) |
75 | 75 | { |
76 | - if($this->connected){ |
|
76 | + if ($this->connected) { |
|
77 | 77 | return; |
78 | 78 | } |
79 | 79 | $this->app->getOutput()->writeln(strftime('%T')." - info - Livereload protocol initialized.", OutputInterface::VERBOSITY_VERBOSE); |
@@ -41,10 +41,12 @@ discard block |
||
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 |
||
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 |
||
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); |
@@ -30,7 +30,7 @@ discard block |
||
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 | $config = $this->loadConfig($input, $output); |
35 | 35 | $app->watching($config['period'], $config); |
36 | 36 | } |
@@ -41,10 +41,10 @@ discard block |
||
41 | 41 | protected function loadConfig(InputInterface $input, OutputInterface $output) |
42 | 42 | { |
43 | 43 | $configFile = $input->getOption('config'); |
44 | - if($configFile === null){ |
|
44 | + if ($configFile === null) { |
|
45 | 45 | $configFile = 'livereload.json'; |
46 | 46 | } |
47 | - if(!file_exists($configFile)){ |
|
47 | + if (!file_exists($configFile)) { |
|
48 | 48 | throw new \Exception("$configFile not found."); |
49 | 49 | } |
50 | 50 |
@@ -30,7 +30,8 @@ discard block |
||
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 |
||
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 |
@@ -22,7 +22,7 @@ |
||
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 | $output->writeln("<error>livereload.json file exists.\nplease use --force to overwrite.</error>"); |
27 | 27 | return; |
28 | 28 | } |
@@ -22,7 +22,8 @@ |
||
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 | } |
@@ -40,7 +40,7 @@ |
||
40 | 40 | "web/img/": "\\\\.png|gif|jpg$" |
41 | 41 | } |
42 | 42 | } |
43 | -EOT |
|
43 | +eot |
|
44 | 44 | ; |
45 | 45 | file_put_contents('livereload.json', $json); |
46 | 46 | $output->writeln("<info>livereload.json is generated.</info>"); |
@@ -52,23 +52,23 @@ discard block |
||
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 | $this->watchingFileChange(); |
57 | 57 | }); |
58 | 58 | } |
59 | 59 | |
60 | 60 | protected function scanFiles($scanNewFile = true) |
61 | 61 | { |
62 | - foreach($this->watchConfig['watch'] as $path => $file){ |
|
62 | + foreach ($this->watchConfig['watch'] as $path => $file) { |
|
63 | 63 | $finder = new Finder(); |
64 | - try{ |
|
65 | - foreach($finder->in($path)->name($file)->followLinks() as $file){ |
|
66 | - if($file->getRealPath() && !isset($this->watchingFiles[$file->getRealpath()])){ |
|
67 | - $this->watchingFiles[$file->getRealpath()] = $scanNewFile?$file->getMTime():0; |
|
64 | + try { |
|
65 | + foreach ($finder->in($path)->name($file)->followLinks() as $file) { |
|
66 | + if ($file->getRealPath() && !isset($this->watchingFiles[$file->getRealpath()])) { |
|
67 | + $this->watchingFiles[$file->getRealpath()] = $scanNewFile ? $file->getMTime() : 0; |
|
68 | 68 | } |
69 | 69 | } |
70 | 70 | } |
71 | - catch(\InvalidArgumentException $e){ |
|
71 | + catch (\InvalidArgumentException $e) { |
|
72 | 72 | continue; |
73 | 73 | } |
74 | 74 | } |
@@ -77,9 +77,9 @@ discard block |
||
77 | 77 | protected function watchingFileChange() |
78 | 78 | { |
79 | 79 | $this->scanFiles(false); |
80 | - foreach($this->watchingFiles as $file => $time){ |
|
80 | + foreach ($this->watchingFiles as $file => $time) { |
|
81 | 81 | $mtime = @filemtime($file); |
82 | - if($mtime && $mtime > $time){ |
|
82 | + if ($mtime && $mtime > $time) { |
|
83 | 83 | $this->watchingFiles[$file] = $mtime; |
84 | 84 | $this->reloadFile($file); |
85 | 85 | } |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | |
101 | 101 | public function addClient(Protocol\LivereloadProtocol $client) |
102 | 102 | { |
103 | - if(!in_array($client, $this->clients)){ |
|
103 | + if (!in_array($client, $this->clients)) { |
|
104 | 104 | $this->clients[] = $client; |
105 | 105 | } |
106 | 106 | } |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | public function removeClient(Protocol\LivereloadProtocol $client) |
109 | 109 | { |
110 | 110 | $index = array_search($client, $this->clients, true); |
111 | - if($index === false){ |
|
111 | + if ($index === false) { |
|
112 | 112 | return; |
113 | 113 | } |
114 | 114 | unset($this->clients[$index]); |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | |
117 | 117 | public function reloadFile($file) |
118 | 118 | { |
119 | - foreach($this->clients as $client){ |
|
119 | + foreach ($this->clients as $client) { |
|
120 | 120 | $client->reload($file, $this->config); |
121 | 121 | } |
122 | 122 | } |
@@ -52,23 +52,29 @@ discard block |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 | } |