Skip to content
This repository was archived by the owner on Mar 12, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .dunitconfig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// the list of docker images to run against
images="
vectorface/php5.4
vectorface/php5.5
vectorface/php5.6
vectorface/php-nightly
vectorface/hhvm";


Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ return array(

'shortenTimeout' => 15 // If after this amount of seconds no url shortener has come up with a short URL the normal URL will be used. (Not in effect when there are no shorteners listening.)

// or

'filter' => null // Any valid filter implementing Phergie\Irc\Plugin\React\EventFilter\FilterInterface to filter which messages should be handled

)),

)
Expand Down Expand Up @@ -87,6 +91,17 @@ Selection of response headers from: [en.wikipedia.org/wiki/List_of_HTTP_header_f
* `%header-server%`
* `%header-x-powered-by%`

## UrlSectionFilter

This plugin comes with the `UrlSectionFilter` that lets you filter on the different key value pairs coming out of [`parse_url`](http://php.net/parse_url). The following example filter allows `www.phergie.org`, `www2.phergie.org`, and `phergie.org`:

```php
new OrFilter([
new UrlSectionFilter('host', '*.phergie.org'),
new UrlSectionFilter('host', 'phergie.org'),
])
``

## Tests

To run the unit test suite:
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"php": "^5.5|^7.0",
"phergie/phergie-irc-bot-react": "~2",
"nojimage/twitter-text-php": "1.1.1",
"phergie/phergie-irc-plugin-http": "^4",
"react/promise": "~1.0|~2.0"
"phergie/phergie-irc-plugin-http": "^4.0",
"react/promise": "~1.0|~2.0",
"phergie/phergie-irc-plugin-react-eventfilter": "^1.0||^2.0"
},
"require-dev": {
"phake/phake": "dev-VerifierResultConstraint-issue",
Expand Down
78 changes: 61 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions src/Filter/UrlEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Phergie\Irc\Plugin\React\Url\Filter;

use Phergie\Irc\ConnectionInterface;
use Phergie\Irc\Event\EventInterface;

class UrlEvent implements EventInterface
{
/**
* @var string
*/
protected $url;

/**
* @var array|mixed|false
*/
protected $parsedUrl;

/**
* @var EventInterface
*/
protected $event;

/**
* UrlEvent constructor.
* @param string $url
* @param EventInterface $event
*/
public function __construct($url, EventInterface $event)
{
$this->url = $url;
$this->parsedUrl = parse_url($url);
$this->event = $event;
}

public function setMessage($message)
{
return $this->event->setMessage($message);
}

public function getMessage()
{
return $this->event->getMessage();
}

public function setConnection(ConnectionInterface $connection)
{
return $this->event->setConnection($connection);
}

public function getConnection()
{
return $this->event->getConnection();
}

public function setParams(array $params)
{
return $this->event->setParams($params);
}

public function getParams()
{
return $this->event->getParams();
}

public function setCommand($command)
{
return $this->event->setCommand($command);
}

public function getCommand()
{
return $this->event->getCommand();
}

public function getUrlSection($section)
{
if (isset($this->parsedUrl[$section])) {
return $this->parsedUrl[$section];
}

return false;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would returning null here would make more sense semantically (i.e. to imply the lack of a value)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasoning for using false is the failure of finding a value. If your preference is using null that can be arranged 😄

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think null makes more sense here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
}
53 changes: 53 additions & 0 deletions src/Filter/UrlSectionFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Phergie\Irc\Plugin\React\Url\Filter;

use Phergie\Irc\Event\EventInterface;
use Phergie\Irc\Plugin\React\EventFilter\FilterInterface;

class UrlSectionFilter implements FilterInterface
{
/**
* @var string
*/
protected $value;

/**
* @var string
*/
protected $section;

/**
*
* @param string $section
* @param string $value
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a @param tag for $section.

*/
public function __construct($section, $value)
{
$this->section = $section;
$this->value = $value;
}

/**
* @param EventInterface $event
* @return bool|null
*/
public function filter(EventInterface $event)
{
if (!($event instanceof UrlEvent)) {
return null;
}

$section = $event->getUrlSection($this->section);
if ($section === false) {
return null;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implies that the event should be allowed through if it doesn't contain the part of the URL that's being filtered on. Is that the desired behavior?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes if the part of the URL is missing it doesn't fall within scope of the filter. How ever in some cases it does make sense to return false if it is missing so I've added a strict bool in 2aed4c3.

}

$pattern = '/^' . str_replace('*', '.*', $this->value) . '$/';
if (preg_match($pattern, $section)) {
return true;
}

return false;
}
}
Loading