-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Small cleanup #110
base: master
Are you sure you want to change the base?
Small cleanup #110
Conversation
src/Plugin/AbstractPlugin.php
Outdated
@@ -26,56 +22,16 @@ public function onCompleted(ProxyEvent $event){ | |||
} | |||
|
|||
final public function subscribe($dispatcher){ | |||
$event_listeners = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
@@ -26,56 +22,16 @@ public function onCompleted(ProxyEvent $event){ | |||
} | |||
|
|||
final public function subscribe($dispatcher){ | |||
$event_listeners = [ | |||
'request.before_send' => 'onBeforeRequest', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
@@ -26,56 +22,16 @@ public function onCompleted(ProxyEvent $event){ | |||
} | |||
|
|||
final public function subscribe($dispatcher){ | |||
$event_listeners = [ | |||
'request.before_send' => 'onBeforeRequest', | |||
'request.sent' => 'onHeadersReceived', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
$event_listeners = [ | ||
'request.before_send' => 'onBeforeRequest', | ||
'request.sent' => 'onHeadersReceived', | ||
'curl.callback.write' => 'onCurlWrite', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
'request.before_send' => 'onBeforeRequest', | ||
'request.sent' => 'onHeadersReceived', | ||
'curl.callback.write' => 'onCurlWrite', | ||
'request.complete' => 'onCompleted', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
'request.sent' => 'onHeadersReceived', | ||
'curl.callback.write' => 'onCurlWrite', | ||
'request.complete' => 'onCompleted', | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
case 'request.complete': | ||
$this->onCompleted($event); | ||
break; | ||
foreach($event_listeners as $event => $listener) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Spaces must be used to indent lines; tabs are not allowed
- Expected 1 space after FOREACH keyword; 0 found
src/Plugin/AbstractPlugin.php
Outdated
$this->onCompleted($event); | ||
break; | ||
foreach($event_listeners as $event => $listener) { | ||
$dispatcher->addListener($event, [$this, $listener]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
|
||
return str_replace($matches[1], proxify_url($matches[1], $this->base_url), $matches[0]); | ||
} | ||
private const CONTENT_TYPE_BLACKLIST = ['image', 'font', 'application/javascript', 'application/x-javascript', 'text/javascript', 'text/plain']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
return str_replace($matches[1], proxify_url($matches[1], $this->base_url), $matches[0]); | ||
} | ||
private const CONTENT_TYPE_BLACKLIST = ['image', 'font', 'application/javascript', 'application/x-javascript', 'text/javascript', 'text/plain']; | ||
private const LINK_TYPE_BLACKLIST = ['data:', 'magnet:', 'about:', 'javascript:', 'mailto:', 'tel:', 'ios-app:', 'android-app:']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
} | ||
private const CONTENT_TYPE_BLACKLIST = ['image', 'font', 'application/javascript', 'application/x-javascript', 'text/javascript', 'text/plain']; | ||
private const LINK_TYPE_BLACKLIST = ['data:', 'magnet:', 'about:', 'javascript:', 'mailto:', 'tel:', 'ios-app:', 'android-app:']; | ||
private const CONTENT_PARSERS = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
private const CONTENT_TYPE_BLACKLIST = ['image', 'font', 'application/javascript', 'application/x-javascript', 'text/javascript', 'text/plain']; | ||
private const LINK_TYPE_BLACKLIST = ['data:', 'magnet:', 'about:', 'javascript:', 'mailto:', 'tel:', 'ios-app:', 'android-app:']; | ||
private const CONTENT_PARSERS = [ | ||
'@\bcontent=(?<quote>\'|")\d+\s*;\s*url=(?<url>.*?)\k<quote>@is' => 'self::proxify_url_callback', // content="X;url=<url>" (meta-refresh) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
private const LINK_TYPE_BLACKLIST = ['data:', 'magnet:', 'about:', 'javascript:', 'mailto:', 'tel:', 'ios-app:', 'android-app:']; | ||
private const CONTENT_PARSERS = [ | ||
'@\bcontent=(?<quote>\'|")\d+\s*;\s*url=(?<url>.*?)\k<quote>@is' => 'self::proxify_url_callback', // content="X;url=<url>" (meta-refresh) | ||
'@\b(?:src|href)\s*=\s*(?<quote>\'|")(?<url>.*?)\k<quote>@is' => 'self::proxify_url_callback', // src="<url>" & href="<url>" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
private const CONTENT_PARSERS = [ | ||
'@\bcontent=(?<quote>\'|")\d+\s*;\s*url=(?<url>.*?)\k<quote>@is' => 'self::proxify_url_callback', // content="X;url=<url>" (meta-refresh) | ||
'@\b(?:src|href)\s*=\s*(?<quote>\'|")(?<url>.*?)\k<quote>@is' => 'self::proxify_url_callback', // src="<url>" & href="<url>" | ||
'@\burl\s*\((?<quote>\'|")(?<url>.*?)\k<quote>\)@im' => 'self::proxify_url_callback', // url(<url>) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
'@\bcontent=(?<quote>\'|")\d+\s*;\s*url=(?<url>.*?)\k<quote>@is' => 'self::proxify_url_callback', // content="X;url=<url>" (meta-refresh) | ||
'@\b(?:src|href)\s*=\s*(?<quote>\'|")(?<url>.*?)\k<quote>@is' => 'self::proxify_url_callback', // src="<url>" & href="<url>" | ||
'@\burl\s*\((?<quote>\'|")(?<url>.*?)\k<quote>\)@im' => 'self::proxify_url_callback', // url(<url>) | ||
'@\@import\s+(?<quote>\'|")(?<url>.*?)\k<quote>@im' => 'self::proxify_url_callback', // @import '<url>' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
'@\b(?:src|href)\s*=\s*(?<quote>\'|")(?<url>.*?)\k<quote>@is' => 'self::proxify_url_callback', // src="<url>" & href="<url>" | ||
'@\burl\s*\((?<quote>\'|")(?<url>.*?)\k<quote>\)@im' => 'self::proxify_url_callback', // url(<url>) | ||
'@\@import\s+(?<quote>\'|")(?<url>.*?)\k<quote>@im' => 'self::proxify_url_callback', // @import '<url>' | ||
'@\b(?:srcset)\s*=\s*(?<quote>\'|")(?<value>.*?)\k<quote>@im' => 'self::proxify_srcset_attribute_callback', // srcset="<url> xxx, …" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
'@\burl\s*\((?<quote>\'|")(?<url>.*?)\k<quote>\)@im' => 'self::proxify_url_callback', // url(<url>) | ||
'@\@import\s+(?<quote>\'|")(?<url>.*?)\k<quote>@im' => 'self::proxify_url_callback', // @import '<url>' | ||
'@\b(?:srcset)\s*=\s*(?<quote>\'|")(?<value>.*?)\k<quote>@im' => 'self::proxify_srcset_attribute_callback', // srcset="<url> xxx, …" | ||
'@<\s*form[^>]*action=(?<quote>\'|")(?<url>.*?)\k<quote>[^>]*>@im' => 'self::proxify_form_callback', // <form action="<url>" …> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
'@\@import\s+(?<quote>\'|")(?<url>.*?)\k<quote>@im' => 'self::proxify_url_callback', // @import '<url>' | ||
'@\b(?:srcset)\s*=\s*(?<quote>\'|")(?<value>.*?)\k<quote>@im' => 'self::proxify_srcset_attribute_callback', // srcset="<url> xxx, …" | ||
'@<\s*form[^>]*action=(?<quote>\'|")(?<url>.*?)\k<quote>[^>]*>@im' => 'self::proxify_form_callback', // <form action="<url>" …> | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
|
||
return str_replace($url, proxify_url($url, $this->base_url), $matches[0]); | ||
} | ||
private $base_url = ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
return str_replace($url, proxify_url($url, $this->base_url), $matches[0]); | ||
} | ||
private $base_url = ''; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
} | ||
private $base_url = ''; | ||
|
||
public function onCompleted(ProxyEvent $event) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Spaces must be used to indent lines; tabs are not allowed
- Opening brace should be on a new line
src/Plugin/ProxifyPlugin.php
Outdated
private $base_url = ''; | ||
|
||
public function onCompleted(ProxyEvent $event) { | ||
$response = $event['response']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
|
||
public function onCompleted(ProxyEvent $event) { | ||
$response = $event['response']; | ||
$content_type = $response->headers->get('content-type'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
public function onCompleted(ProxyEvent $event) { | ||
$response = $event['response']; | ||
$content_type = $response->headers->get('content-type'); | ||
if(starts_with($content_type, self::CONTENT_TYPE_BLACKLIST)) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Spaces must be used to indent lines; tabs are not allowed
- Expected 1 space after IF keyword; 0 found
- Inline control structures are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
} | ||
|
||
return $result; | ||
// to be used when proxifying all the relative links |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
|
||
return $result; | ||
// to be used when proxifying all the relative links | ||
$this->base_url = $event['request']->getUri(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
return $result; | ||
// to be used when proxifying all the relative links | ||
$this->base_url = $event['request']->getUri(); | ||
$proxified_content = preg_replace_callback_array(self::CONTENT_PARSERS, $response->getContent()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
// to be used when proxifying all the relative links | ||
$this->base_url = $event['request']->getUri(); | ||
$proxified_content = preg_replace_callback_array(self::CONTENT_PARSERS, $response->getContent()); | ||
$response->setContent($proxified_content); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/ProxifyPlugin.php
Outdated
} | ||
|
||
public function onBeforeRequest(ProxyEvent $event){ | ||
|
||
public function onBeforeRequest(ProxyEvent $event) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Spaces must be used to indent lines; tabs are not allowed
- Opening brace should be on a new line
src/Plugin/ProxifyPlugin.php
Outdated
|
||
$request->prepare(); | ||
} | ||
$this->convertPostToGet($request); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
Script removal is not related to proxification. Class Html removed, no longer referenced.
Title replacement is not related to proxification.
Use regex named capture groups to allow reuse of callbacks. Added 'image' & 'font' to content-type blacklist.
src/Plugin/AbstractPlugin.php
Outdated
abstract class AbstractPlugin | ||
{ | ||
|
||
public function onBeforeRequest(ProxyEvent $event) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
{ | ||
|
||
public function onBeforeRequest(ProxyEvent $event) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
public function onHeadersReceived(ProxyEvent $event){ | ||
|
||
public function onHeadersReceived(ProxyEvent $event) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
|
||
public function onCurlWrite(ProxyEvent $event){ | ||
|
||
public function onCurlWrite(ProxyEvent $event) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
public function onCurlWrite(ProxyEvent $event){ | ||
|
||
public function onCurlWrite(ProxyEvent $event) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
|
||
public function onCompleted(ProxyEvent $event){ | ||
|
||
public function onCompleted(ProxyEvent $event) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
public function onCompleted(ProxyEvent $event){ | ||
|
||
public function onCompleted(ProxyEvent $event) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
$this->onCompleted($event); | ||
break; | ||
|
||
final public function subscribe($dispatcher) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
break; | ||
|
||
final public function subscribe($dispatcher) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
|
||
final public function subscribe($dispatcher) | ||
{ | ||
$event_listeners = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
final public function subscribe($dispatcher) | ||
{ | ||
$event_listeners = [ | ||
'request.before_send' => 'onBeforeRequest', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
{ | ||
$event_listeners = [ | ||
'request.before_send' => 'onBeforeRequest', | ||
'request.sent' => 'onHeadersReceived', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
$event_listeners = [ | ||
'request.before_send' => 'onBeforeRequest', | ||
'request.sent' => 'onHeadersReceived', | ||
'curl.callback.write' => 'onCurlWrite', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
'request.complete' => 'onCompleted', | ||
]; | ||
|
||
foreach ($event_listeners as $event => $listener) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
src/Plugin/AbstractPlugin.php
Outdated
]; | ||
|
||
foreach ($event_listeners as $event => $listener) { | ||
$dispatcher->addListener($event, [$this, $listener]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaces must be used to indent lines; tabs are not allowed
Reduced function call overhead.
I removed the Html class as it is no longer a dependency following the extraction of js_remove and inlining the RegEx. Since some of the plugins in php-proxy-plugin-bundle are dependent on it, it might make sense to merge it with the utils there before removing it here. |
It looks like there is also a dependency on the url_pattern functionality from php-proxy-plugin-bundle so that goes back in. |
Interesting changes @reef-actor @Athlon1600 what do you think about these changes? I like the refactor of ProxifyPlugin and that remove_js and replace_title have becomed a plugin. |
It has occurred to me that const visibility modifiers were only introduced in php 7.1 so the use of |
Some of those could probably go through. I will have more time to review this next week though... I'm looking right now to see what happened to Html class. |
'remove_js' & 'replace_title' didn't seem to belong in the ProxifyPlugin, so have been given their own plugins.
ProxifyPlugin has been reorganised, primary difference is the use of named capture groups in the RegEx's to allow reuse of the callback and therefore reduce duplicated code. Image and Font content has been added to the blacklist to reduce load.
AbstractPlugin has been simplified to directly subscribe to events, removing 2 extra function calls per event per plugin.