@@ -21,23 +21,18 @@ class Connection extends ClientConnection
21
21
public $ requests = []; // Pending requests
22
22
public $ lastReqId = 0 ; // ID of the last request
23
23
24
- public function onReady ()
25
- {
26
- if ($ this ->user ===null )
27
- {
24
+ public function onReady () {
25
+ if ($ this ->user === null ) {
28
26
$ this ->connected = true ;
29
27
}
30
- if ($ this ->connected )
31
- {
28
+ if ($ this ->connected ) {
32
29
parent ::onReady ();
33
30
return ;
34
31
}
35
32
$ this ->dbname = $ this ->path ;
36
33
$ this ->pool ->getNonce (['dbname ' => $ this ->dbname ],
37
- function ($ result )
38
- {
39
- if (isset ($ result ['$err ' ]))
40
- {
34
+ function ($ result ) {
35
+ if (isset ($ result ['$err ' ])) {
41
36
Daemon::log ('MongoClient: getNonce() error with ' .$ this ->url .': ' .$ result ['$err ' ]);
42
37
$ this ->finish ();
43
38
}
@@ -47,11 +42,9 @@ function ($result)
47
42
'nonce ' => $ result ['nonce ' ],
48
43
'dbname ' => $ this ->dbname ,
49
44
],
50
- function ($ result )
51
- {
52
- if (!isset ($ result ['ok ' ]) || !$ result ['ok ' ])
53
- {
54
- Daemon::log ('MongoClient: authentication error with ' .$ this ->url .': ' .$ result ['errmsg ' ]);
45
+ function ($ result ) {
46
+ if (!isset ($ result ['ok ' ]) || !$ result ['ok ' ]) {
47
+ Daemon::log ('MongoClient: authentication error with ' . $ this ->url . ': ' . $ result ['errmsg ' ]);
55
48
$ this ->finish ();
56
49
return ;
57
50
}
@@ -66,121 +59,99 @@ function ($result)
66
59
* Called when new data received
67
60
* @return void
68
61
*/
69
- public function onRead ()
70
- {
62
+ public function onRead () {
71
63
start:
72
- if ($ this ->freed )
73
- {
64
+ if ($ this ->freed ) {
74
65
return ;
75
66
}
76
- if ($ this ->state ===self ::STATE_ROOT )
77
- {
78
- if (false ===($ hdr = $ this ->readExact (16 )))
79
- {
67
+ if ($ this ->state === self ::STATE_ROOT ) {
68
+ if (false === ($ hdr = $ this ->readExact (16 ))) {
80
69
return ; // we do not have a header
81
70
}
82
71
$ this ->hdr = unpack ('Vlen/VreqId/VresponseTo/VopCode ' , $ hdr );
83
- $ this ->hdr ['plen ' ] = $ this ->hdr ['len ' ]- 16 ;
72
+ $ this ->hdr ['plen ' ] = $ this ->hdr ['len ' ] - 16 ;
84
73
$ this ->setWatermark ($ this ->hdr ['plen ' ], $ this ->hdr ['plen ' ]);
85
74
$ this ->state = self ::STATE_PACKET ;
86
75
}
87
- if ($ this ->state ===self ::STATE_PACKET )
76
+ if ($ this ->state === self ::STATE_PACKET )
88
77
{
89
- if (false ===($ pct = $ this ->readExact ($ this ->hdr ['plen ' ])))
78
+ if (false === ($ pct = $ this ->readExact ($ this ->hdr ['plen ' ])))
90
79
{
91
80
return ; //we do not have a whole packet
92
81
}
93
82
$ this ->state = self ::STATE_ROOT ;
94
83
$ this ->setWatermark (16 , 0xFFFFFF );
95
- if ($ this ->hdr ['opCode ' ]===Pool::OP_REPLY )
96
- {
84
+ if ($ this ->hdr ['opCode ' ] === Pool::OP_REPLY ) {
97
85
$ r = unpack ('Vflag/VcursorID1/VcursorID2/Voffset/Vlength ' , binarySubstr ($ pct , 0 , 20 ));
98
86
$ r ['cursorId ' ] = binarySubstr ($ pct , 4 , 8 );
99
87
$ id = (int )$ this ->hdr ['responseTo ' ];
100
- if (isset ($ this ->requests [$ id ]))
101
- {
88
+ if (isset ($ this ->requests [$ id ])) {
102
89
$ req = $ this ->requests [$ id ];
103
90
}
104
- else
105
- {
91
+ else {
106
92
$ req = false ;
107
93
}
108
94
$ flagBits = str_pad (strrev (decbin ($ r ['flag ' ])), 8 , '0 ' , STR_PAD_LEFT );
109
- $ curId = ($ r ['cursorId ' ]!=="\x00\x00\x00\x00\x00\x00\x00\x00" ? 'c ' .$ r ['cursorId ' ] : 'r ' .$ this ->hdr ['responseTo ' ]);
95
+ $ curId = ($ r ['cursorId ' ] !== "\x00\x00\x00\x00\x00\x00\x00\x00" ? 'c ' .$ r ['cursorId ' ] : 'r ' .$ this ->hdr ['responseTo ' ]);
110
96
111
- if ($ req && isset ($ req [2 ]) && ($ req [2 ]===false ) && !isset ($ this ->cursors [$ curId ]))
112
- {
97
+ if ($ req && isset ($ req [2 ]) && ($ req [2 ]===false ) && !isset ($ this ->cursors [$ curId ])) {
113
98
$ cur = new Cursor ($ curId , $ req [0 ], $ this );
114
99
$ this ->cursors [$ curId ] = $ cur ;
115
- $ cur ->failure = $ flagBits [1 ]==='1 ' ;
116
- $ cur ->await = $ flagBits [3 ]==='1 ' ;
100
+ $ cur ->failure = $ flagBits [1 ] === '1 ' ;
101
+ $ cur ->await = $ flagBits [3 ] === '1 ' ;
117
102
$ cur ->callback = $ req [1 ];
118
103
$ cur ->parseOplog = isset ($ req [3 ]) && $ req [3 ];
119
104
$ cur ->tailable = isset ($ req [4 ]) && $ req [4 ];
120
105
}
121
- else
122
- {
106
+ else {
123
107
$ cur = isset ($ this ->cursors [$ curId ]) ? $ this ->cursors [$ curId ] : false ;
124
108
}
125
- if ($ cur && (($ r ['length ' ]===0 ) || (binarySubstr ($ curId , 0 , 1 )==='r ' )))
126
- {
127
- if ($ cur ->tailable )
128
- {
129
- if ($ cur ->finished = ($ flagBits [0 ]=='1 ' ))
130
- {
109
+ if ($ cur && (($ r ['length ' ]===0 ) || (binarySubstr ($ curId , 0 , 1 ) === 'r ' ))) {
110
+ if ($ cur ->tailable ) {
111
+ if ($ cur ->finished = ($ flagBits [0 ] == '1 ' )) {
131
112
$ cur ->destroy ();
132
113
}
133
114
}
134
- else
135
- {
115
+ else {
136
116
$ cur ->finished = true ;
137
117
}
138
118
}
139
119
140
120
$ p = 20 ;
141
121
$ items = [];
142
- while ($ p < $ this ->hdr ['plen ' ])
143
- {
122
+ while ($ p < $ this ->hdr ['plen ' ]) {
144
123
$ dl = unpack ('Vlen ' , binarySubstr ($ pct , $ p , 4 ));
145
124
$ doc = bson_decode (binarySubstr ($ pct , $ p , $ dl ['len ' ]));
146
125
147
- if ($ cur )
148
- {
149
- if ($ cur ->parseOplog && isset ($ doc ['ts ' ]))
150
- {
151
- $ tsdata = unpack ('Vsec/Vinc ' , binarySubstr ($ pct , $ p +1 +4 +3 , 8 ));
126
+ if ($ cur ) {
127
+ if ($ cur ->parseOplog && isset ($ doc ['ts ' ])) {
128
+ $ tsdata = unpack ('Vsec/Vinc ' , binarySubstr ($ pct , $ p + 8 , 8 ));
152
129
$ doc ['ts ' ] = $ tsdata ['sec ' ].' ' .$ tsdata ['inc ' ];
153
130
}
154
131
$ cur ->items [] = $ doc ;
155
132
}
156
- else
157
- {
133
+ else {
158
134
$ items [] = $ doc ;
159
135
}
160
136
$ p += $ dl ['len ' ];
161
137
}
162
138
$ this ->setFree (true );
163
- if (isset ($ req [2 ]) && $ req [2 ] && $ req [1 ])
164
- {
139
+ if (isset ($ req [2 ]) && $ req [2 ] && $ req [1 ]) {
165
140
call_user_func (
166
141
$ req [1 ],
167
142
sizeof ($ items ) ? $ items [0 ] : false
168
143
);
169
144
170
- if ($ cur )
171
- {
172
- if ($ cur instanceof Cursor)
173
- {
145
+ if ($ cur ) {
146
+ if ($ cur instanceof Cursor) {
174
147
$ cur ->destroy ();
175
148
}
176
- else
177
- {
149
+ else {
178
150
unset($ this ->cursors [$ curId ]);
179
151
}
180
152
}
181
153
}
182
- elseif ($ cur )
183
- {
154
+ elseif ($ cur ) {
184
155
call_user_func ($ cur ->callback , $ cur );
185
156
}
186
157
unset($ this ->requests [$ id ], $ req );
0 commit comments