22
33namespace Proxy ;
44
5- use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
6- use Symfony \Component \EventDispatcher \EventDispatcher ;
7- use Symfony \Component \EventDispatcher \Event ;
8- use Symfony \Component \EventDispatcher \GenericEvent ;
9-
10- use Proxy \Config ;
115use Proxy \Event \ProxyEvent ;
126use Proxy \Http \Request ;
137use Proxy \Http \Response ;
8+ use Proxy \Config ;
149
1510class Proxy {
16-
17- // proxy version!
18- const VERSION = '5.0.1 ' ;
11+
12+ // Proxy script version
13+ const VERSION = '5.1.0 ' ;
1914
2015 private $ dispatcher ;
2116
@@ -28,15 +23,14 @@ class Proxy {
2823 private $ status_found = false ;
2924
3025 public function __construct (){
31- $ this -> dispatcher = new EventDispatcher ();
26+ // do nothing for now
3227 }
3328
3429 public function setOutputBuffering ($ output_buffering ){
3530 $ this ->output_buffering = $ output_buffering ;
3631 }
3732
3833 private function header_callback ($ ch , $ headers ){
39-
4034 $ parts = explode (": " , $ headers , 2 );
4135
4236 // extract status code
@@ -60,7 +54,7 @@ private function header_callback($ch, $headers){
6054 $ event = new ProxyEvent (array ('request ' => $ this ->request , 'response ' => $ this ->response , 'proxy ' => &$ this ));
6155
6256 // this is the end of headers - last line is always empty - notify the dispatcher about this
63- $ this ->dispatcher -> dispatch ('request.sent ' , $ event );
57+ $ this ->dispatch ('request.sent ' , $ event );
6458 }
6559
6660 return strlen ($ headers );
@@ -70,7 +64,7 @@ private function write_callback($ch, $str){
7064
7165 $ len = strlen ($ str );
7266
73- $ this ->dispatcher -> dispatch ('curl.callback.write ' , new ProxyEvent (array (
67+ $ this ->dispatch ('curl.callback.write ' , new ProxyEvent (array (
7468 'request ' => $ this ->request ,
7569 'data ' => $ str
7670 )));
@@ -83,8 +77,34 @@ private function write_callback($ch, $str){
8377 return $ len ;
8478 }
8579
86- public function getEventDispatcher (){
87- return $ this ->dispatcher ;
80+ // TODO: move this all into its own Dispatcher class?
81+ // https://github.com/guzzle/guzzle/blob/5.3/src/Event/Emitter.php
82+ // https://github.com/laravel/framework/blob/5.0/src/Illuminate/Events/Dispatcher.php#L72
83+ private $ listeners = array ();
84+
85+ public function addListener ($ event , $ callback , $ priority = 0 ){
86+ $ this ->listeners [$ event ][$ priority ][] = $ callback ;
87+ }
88+
89+ public function addSubscriber ($ subscriber ){
90+ if (method_exists ($ subscriber , 'subscribe ' )){
91+ $ subscriber ->subscribe ($ this );
92+ }
93+ }
94+
95+ private function dispatch ($ event_name , $ event ){
96+
97+ if (isset ($ this ->listeners [$ event_name ])){
98+ $ temp = (array )$ this ->listeners [$ event_name ];
99+
100+ foreach ($ temp as $ priority => $ listeners ){
101+ foreach ( (array )$ listeners as $ listener ){
102+ if (is_callable ($ listener ) ){
103+ $ listener ($ event );
104+ }
105+ }
106+ }
107+ }
88108 }
89109
90110 public function forward (Request $ request , $ url ){
@@ -122,11 +142,13 @@ public function forward(Request $request, $url){
122142 $ options [CURLOPT_WRITEFUNCTION ] = array ($ this , 'write_callback ' );
123143
124144 // Notify any listeners that the request is ready to be sent, and this is your last chance to make any modifications.
125- $ this ->dispatcher ->dispatch ('request.before_send ' , new ProxyEvent (array ('request ' => $ this ->request , 'response ' => $ this ->response )));
145+ $ this ->dispatch ('request.before_send ' , new ProxyEvent (array (
146+ 'request ' => $ this ->request ,
147+ 'response ' => $ this ->response
148+ )));
126149
127150 // We may not even need to send this request if response is already available somewhere (CachePlugin)
128151 if ($ this ->request ->params ->has ('request.complete ' )){
129-
130152 // do nothing?
131153 } else {
132154
@@ -146,9 +168,7 @@ public function forward(Request $request, $url){
146168
147169 // there must have been an error if at this point
148170 if (!$ result ){
149-
150171 $ error = sprintf ('(%d) %s ' , curl_errno ($ ch ), curl_error ($ ch ));
151-
152172 throw new \Exception ($ error );
153173 }
154174
@@ -159,7 +179,10 @@ public function forward(Request $request, $url){
159179 $ this ->output_buffer = null ;
160180 }
161181
162- $ this ->dispatcher ->dispatch ('request.complete ' , new ProxyEvent (array ('request ' => $ this ->request , 'response ' => $ this ->response )));
182+ $ this ->dispatch ('request.complete ' , new ProxyEvent (array (
183+ 'request ' => $ this ->request ,
184+ 'response ' => $ this ->response
185+ )));
163186
164187 return $ this ->response ;
165188 }
0 commit comments