@@ -155,46 +155,51 @@ public function deleteNode($path)
155155 return $ this ->zookeeper ->delete ($ path );
156156 }
157157 }
158-
159- /**
158+
159+ /**
160160 * Wath a given path
161161 * @param string $path the path to node
162162 * @param callable $callback callback function
163163 * @return string|null
164164 */
165- public function watch ($ path , $ callback )
166- {
165+ public function watch ($ path , $ callback ){
167166 if (!is_callable ($ callback )) {
168167 return null ;
169168 }
170-
169+
170+ $ ret = null ;
171+
171172 if ($ this ->zookeeper ->exists ($ path )) {
172173 if (!isset ($ this ->callback [$ path ])) {
173174 $ this ->callback [$ path ] = array ();
175+ $ ret = $ this ->zookeeper ->get ($ path , array ($ this , 'watchCallback ' ));
176+ } else {
177+ $ ret = $ this ->get ($ path );
174178 }
175179 if (!in_array ($ callback , $ this ->callback [$ path ])) {
176180 $ this ->callback [$ path ][] = $ callback ;
177- return $ this ->zookeeper ->get ($ path , array ($ this , 'watchCallback ' ));
178181 }
179182 }
183+
184+ return $ ret ;
180185 }
181-
186+
182187 /**
183188 * Wath event callback warper
184189 * @param int $event_type
185190 * @param int $stat
186191 * @param string $path
187192 * @return the return of the callback or null
188193 */
189- public function watchCallback ($ event_type , $ stat , $ path )
190- {
194+ public function watchCallback ($ event_type , $ stat , $ path ) {
191195 if (!isset ($ this ->callback [$ path ])) {
192196 return null ;
193197 }
194-
198+
199+ $ ret = $ this ->zookeeper ->get ($ path , array ($ this , 'watchCallback ' ));
200+
195201 foreach ($ this ->callback [$ path ] as $ callback ) {
196- $ this ->zookeeper ->get ($ path , array ($ this , 'watchCallback ' ));
197- return call_user_func ($ callback );
202+ call_user_func ($ callback , $ ret );
198203 }
199204 }
200205
@@ -239,11 +244,15 @@ public function cancelWatch($path, $callback = null)
239244var_dump ($ zk ->getChildren ('/foo ' ));
240245
241246//watch example
242- function callback () {
243- echo "in watch callback \n" ;
247+ function callback ($ data ) {
248+ echo "in watch callback {$ data }\n" ;
249+ }
250+ function callback2 ($ data ) {
251+ echo "in watch callback2 {$ data }\n" ;
244252}
245253$ zk ->set ('/bar ' , 1 );
246254$ ret = $ zk ->watch ('/bar ' , 'callback ' );
255+ $ ret2 = $ zk ->watch ('/bar ' , 'callback2 ' );
247256$ zk ->set ('/bar ' , 2 );
248257while (true ) {
249258 sleep (1 );
0 commit comments