55use Evenement \EventEmitter ;
66use React \Stream \Util ;
77use React \EventLoop \LoopInterface ;
8+ use function React \Promise \reject ;
89
910/**
1011 * @internal
@@ -22,15 +23,15 @@ class LazyClient extends EventEmitter implements Client
2223 private $ idleTimer ;
2324 private $ pending = 0 ;
2425
25- private $ subscribed = array () ;
26- private $ psubscribed = array () ;
26+ private $ subscribed = [] ;
27+ private $ psubscribed = [] ;
2728
2829 /**
2930 * @param $target
3031 */
3132 public function __construct ($ target , Factory $ factory , LoopInterface $ loop )
3233 {
33- $ args = array () ;
34+ $ args = [] ;
3435 \parse_str ((string ) \parse_url ($ target , \PHP_URL_QUERY ), $ args );
3536 if (isset ($ args ['idle ' ])) {
3637 $ this ->idlePeriod = (float )$ args ['idle ' ];
@@ -47,66 +48,59 @@ private function client()
4748 return $ this ->promise ;
4849 }
4950
50- $ self = $ this ;
51- $ pending =& $ this ->promise ;
52- $ idleTimer =& $ this ->idleTimer ;
53- $ subscribed =& $ this ->subscribed ;
54- $ psubscribed =& $ this ->psubscribed ;
55- $ loop = $ this ->loop ;
56- return $ pending = $ this ->factory ->createClient ($ this ->target )->then (function (Client $ redis ) use ($ self , &$ pending , &$ idleTimer , &$ subscribed , &$ psubscribed , $ loop ) {
51+ return $ this ->promise = $ this ->factory ->createClient ($ this ->target )->then (function (Client $ redis ) {
5752 // connection completed => remember only until closed
58- $ redis ->on ('close ' , function () use (& $ pending , $ self , & $ subscribed , & $ psubscribed , & $ idleTimer , $ loop ) {
59- $ pending = null ;
53+ $ redis ->on ('close ' , function () {
54+ $ this -> promise = null ;
6055
6156 // foward unsubscribe/punsubscribe events when underlying connection closes
62- $ n = count ($ subscribed );
63- foreach ($ subscribed as $ channel => $ _ ) {
64- $ self ->emit ('unsubscribe ' , array ( $ channel , --$ n) );
57+ $ n = count ($ this -> subscribed );
58+ foreach ($ this -> subscribed as $ channel => $ _ ) {
59+ $ this ->emit ('unsubscribe ' , [ $ channel , --$ n] );
6560 }
66- $ n = count ($ psubscribed );
67- foreach ($ psubscribed as $ pattern => $ _ ) {
68- $ self ->emit ('punsubscribe ' , array ( $ pattern , --$ n) );
61+ $ n = count ($ this -> psubscribed );
62+ foreach ($ this -> psubscribed as $ pattern => $ _ ) {
63+ $ this ->emit ('punsubscribe ' , [ $ pattern , --$ n] );
6964 }
70- $ subscribed = array ();
71- $ psubscribed = array ();
65+ $ this ->subscribed = $ this ->psubscribed = [];
7266
73- if ($ idleTimer !== null ) {
74- $ loop ->cancelTimer ($ idleTimer );
75- $ idleTimer = null ;
67+ if ($ this -> idleTimer !== null ) {
68+ $ this -> loop ->cancelTimer ($ this -> idleTimer );
69+ $ this -> idleTimer = null ;
7670 }
7771 });
7872
7973 // keep track of all channels and patterns this connection is subscribed to
80- $ redis ->on ('subscribe ' , function ($ channel ) use (& $ subscribed ) {
81- $ subscribed [$ channel ] = true ;
74+ $ redis ->on ('subscribe ' , function ($ channel ) {
75+ $ this -> subscribed [$ channel ] = true ;
8276 });
83- $ redis ->on ('psubscribe ' , function ($ pattern ) use (& $ psubscribed ) {
84- $ psubscribed [$ pattern ] = true ;
77+ $ redis ->on ('psubscribe ' , function ($ pattern ) {
78+ $ this -> psubscribed [$ pattern ] = true ;
8579 });
86- $ redis ->on ('unsubscribe ' , function ($ channel ) use (& $ subscribed ) {
87- unset($ subscribed [$ channel ]);
80+ $ redis ->on ('unsubscribe ' , function ($ channel ) {
81+ unset($ this -> subscribed [$ channel ]);
8882 });
89- $ redis ->on ('punsubscribe ' , function ($ pattern ) use (& $ psubscribed ) {
90- unset($ psubscribed [$ pattern ]);
83+ $ redis ->on ('punsubscribe ' , function ($ pattern ) {
84+ unset($ this -> psubscribed [$ pattern ]);
9185 });
9286
9387 Util::forwardEvents (
9488 $ redis ,
95- $ self ,
96- array (
89+ $ this ,
90+ [
9791 'message ' ,
9892 'subscribe ' ,
9993 'unsubscribe ' ,
10094 'pmessage ' ,
10195 'psubscribe ' ,
10296 'punsubscribe ' ,
103- )
97+ ]
10498 );
10599
106100 return $ redis ;
107- }, function (\Exception $ e ) use (& $ pending ) {
101+ }, function (\Exception $ e ) {
108102 // connection failed => discard connection attempt
109- $ pending = null ;
103+ $ this -> promise = null ;
110104
111105 throw $ e ;
112106 });
@@ -115,22 +109,21 @@ private function client()
115109 public function __call ($ name , $ args )
116110 {
117111 if ($ this ->closed ) {
118- return \ React \ Promise \ reject (new \RuntimeException (
112+ return reject (new \RuntimeException (
119113 'Connection closed (ENOTCONN) ' ,
120114 defined ('SOCKET_ENOTCONN ' ) ? SOCKET_ENOTCONN : 107
121115 ));
122116 }
123117
124- $ that = $ this ;
125- return $ this ->client ()->then (function (Client $ redis ) use ($ name , $ args , $ that ) {
126- $ that ->awake ();
127- return \call_user_func_array (array ($ redis , $ name ), $ args )->then (
128- function ($ result ) use ($ that ) {
129- $ that ->idle ();
118+ return $ this ->client ()->then (function (Client $ redis ) use ($ name , $ args ) {
119+ $ this ->awake ();
120+ return \call_user_func_array ([$ redis , $ name ], $ args )->then (
121+ function ($ result ) {
122+ $ this ->idle ();
130123 return $ result ;
131124 },
132- function ($ error ) use ( $ that ) {
133- $ that ->idle ();
125+ function ($ error ) {
126+ $ this ->idle ();
134127 throw $ error ;
135128 }
136129 );
@@ -147,10 +140,9 @@ public function end()
147140 return ;
148141 }
149142
150- $ that = $ this ;
151- return $ this ->client ()->then (function (Client $ redis ) use ($ that ) {
152- $ redis ->on ('close ' , function () use ($ that ) {
153- $ that ->close ();
143+ return $ this ->client ()->then (function (Client $ redis ) {
144+ $ redis ->on ('close ' , function () {
145+ $ this ->close ();
154146 });
155147 $ redis ->end ();
156148 });
@@ -205,14 +197,12 @@ public function idle()
205197 --$ this ->pending ;
206198
207199 if ($ this ->pending < 1 && $ this ->idlePeriod >= 0 && !$ this ->subscribed && !$ this ->psubscribed && $ this ->promise !== null ) {
208- $ idleTimer =& $ this ->idleTimer ;
209- $ promise =& $ this ->promise ;
210- $ idleTimer = $ this ->loop ->addTimer ($ this ->idlePeriod , function () use (&$ idleTimer , &$ promise ) {
211- $ promise ->then (function (Client $ redis ) {
200+ $ this ->idleTimer = $ this ->loop ->addTimer ($ this ->idlePeriod , function () {
201+ $ this ->promise ->then (function (Client $ redis ) {
212202 $ redis ->close ();
213203 });
214- $ promise = null ;
215- $ idleTimer = null ;
204+ $ this -> promise = null ;
205+ $ this -> idleTimer = null ;
216206 });
217207 }
218208 }
0 commit comments