|
1 | 1 | <?php |
2 | | - |
3 | 2 | namespace Proxy\Plugin; |
4 | 3 |
|
5 | 4 | use Proxy\Event\ProxyEvent; |
6 | 5 |
|
7 | | -abstract class AbstractPlugin { |
8 | | - |
9 | | - // apply these methods only to those events whose request URL passes this filter |
10 | | - protected $url_pattern; |
11 | | - |
12 | | - public function onBeforeRequest(ProxyEvent $event){ |
13 | | - // fired right before a request is being sent to a proxy |
14 | | - } |
15 | | - |
16 | | - public function onHeadersReceived(ProxyEvent $event){ |
17 | | - // fired right after response headers have been fully received - last chance to modify before sending it back to the user |
18 | | - } |
19 | | - |
20 | | - public function onCurlWrite(ProxyEvent $event){ |
21 | | - // fired as the data is being written piece by piece |
22 | | - } |
23 | | - |
24 | | - public function onCompleted(ProxyEvent $event){ |
25 | | - // fired after the full response=headers+body has been read - will only be called on "non-streaming" responses |
26 | | - } |
27 | | - |
28 | | - final public function subscribe($dispatcher){ |
29 | | - |
30 | | - $dispatcher->addListener('request.before_send', function($event){ |
31 | | - $this->route('request.before_send', $event); |
32 | | - }); |
33 | | - |
34 | | - $dispatcher->addListener('request.sent', function($event){ |
35 | | - $this->route('request.sent', $event); |
36 | | - }); |
37 | | - |
38 | | - $dispatcher->addListener('curl.callback.write', function($event){ |
39 | | - $this->route('curl.callback.write', $event); |
40 | | - }); |
41 | | - |
42 | | - $dispatcher->addListener('request.complete', function($event){ |
43 | | - $this->route('request.complete', $event); |
44 | | - }); |
45 | | - } |
46 | | - |
47 | | - // dispatch based on filter |
48 | | - final private function route($event_name, ProxyEvent $event){ |
49 | | - $url = $event['request']->getUri(); |
50 | | - |
51 | | - // url filter provided and current request url does not match it |
52 | | - if($this->url_pattern){ |
53 | | - if(starts_with($this->url_pattern, '/') && preg_match($this->url_pattern, $url) !== 1){ |
54 | | - return; |
55 | | - } else if(stripos($url, $this->url_pattern) === false){ |
56 | | - return; |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - switch($event_name){ |
61 | | - |
62 | | - case 'request.before_send': |
63 | | - $this->onBeforeRequest($event); |
64 | | - break; |
65 | | - |
66 | | - case 'request.sent': |
67 | | - $this->onHeadersReceived($event); |
68 | | - break; |
69 | | - |
70 | | - case 'curl.callback.write': |
71 | | - $this->onCurlWrite($event); |
72 | | - break; |
73 | | - |
74 | | - case 'request.complete': |
75 | | - $this->onCompleted($event); |
76 | | - break; |
77 | | - } |
78 | | - } |
79 | | -} |
| 6 | +abstract class AbstractPlugin |
| 7 | +{ |
| 8 | + |
| 9 | + public function onBeforeRequest(ProxyEvent $event) |
| 10 | + { |
| 11 | + // fired right before a request is being sent to a proxy |
| 12 | + } |
| 13 | + |
| 14 | + public function onHeadersReceived(ProxyEvent $event) |
| 15 | + { |
| 16 | + // fired right after response headers have been fully received - last chance to modify before sending it back to the user |
| 17 | + } |
80 | 18 |
|
81 | | -?> |
| 19 | + public function onCurlWrite(ProxyEvent $event) |
| 20 | + { |
| 21 | + // fired as the data is being written piece by piece |
| 22 | + } |
| 23 | + |
| 24 | + public function onCompleted(ProxyEvent $event) |
| 25 | + { |
| 26 | + // fired after the full response=headers+body has been read - will only be called on "non-streaming" responses |
| 27 | + } |
| 28 | + |
| 29 | + final public function subscribe($dispatcher) |
| 30 | + { |
| 31 | + $event_listeners = [ |
| 32 | + 'request.before_send' => 'onBeforeRequest', |
| 33 | + 'request.sent' => 'onHeadersReceived', |
| 34 | + 'curl.callback.write' => 'onCurlWrite', |
| 35 | + 'request.complete' => 'onCompleted', |
| 36 | + ]; |
| 37 | + |
| 38 | + foreach ($event_listeners as $event => $listener) { |
| 39 | + $dispatcher->addListener($event, [$this, $listener]); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
0 commit comments