Skip to content

Commit

Permalink
Merge pull request #3 from MASNathan/composer-support-right-way
Browse files Browse the repository at this point in the history
Composer support right way
  • Loading branch information
Maikuolan authored Oct 12, 2017
2 parents 40f3fa4 + e0b9b01 commit c845552
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
composer.lock
10 changes: 9 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
language: php

php:
- '5.4'
- '5.5'
- '5.6'
- '7.0'
- '7.1'

before_script:
- phpenv config-rm xdebug.ini
script: phpunit .travis.php
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-source
- composer dump-autoload -o

script:
- php vendor/bin/phpunit -c phpunit.xml

notifications:
email: false
4 changes: 4 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Versioning guidelines for SemVer can be found at: http://www.semver.org/

- [2017.10.08; Sub-minor code change; Maikuolan]: Refactored code.

- [2017.10.12; Minor code change; MASNathan]: Added the CIDRAM\Aggregator
namespace. Added a helper function (helpers.php). Fixed PSR-4 autoloader
issues, fixed tests, and fixed PHPUnit implementation.

=== Version/Release 1.0.0 ===
FIRST TAGGED RELEASE.

Expand Down
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,38 @@ A stand-alone class implementation of the IPv4+IPv6 IP+CIDR aggregator from CIDR

---


### How to install:

As a stand-alone PHP class, installing it is exceptionally easy. You can download the file containing the class, `aggregator.php`, directly from this repository, and copy it to any projects that need it, or, if you'd prefer, you can install it using Composer:
As a stand-alone PHP class, installing it is exceptionally easy. You can download the file containing the class, [aggregator.php](src/Aggregator.php), directly from this repository, and copy it to any projects that need it, or, if you'd prefer, you can install it using Composer:

`composer require cidram/aggregator`

*Note: The code in this class is based upon code in the CIDRAM package, but the two are NOT dependent on each other.*

After you've downloaded the file, to allow your projects to use the class, [PSR-4](http://www.php-fig.org/psr/psr-4/) autoloading is preferred (particularly if you're using a large number of different, unrelated classes). If you're installing the class via Composer, then this will already be taken care of for you, and you won't need to worry about it. Alternatively, if you're installing it manually (or without Composer), and don't want to use a PSR-4 autoloader, you can simply require or include the class into your projects (which may be much easier in many cases) by including the respective statement to point to the class file in the relevant PHP files:

```PHP
<?php
require __DIR__ . '/aggregator.php';
```
After you've downloaded the file, to allow your projects to use the class, [PSR-4](http://www.php-fig.org/psr/psr-4/) autoloading is preferred (particularly if you're using a large number of different, unrelated classes). If you're installing the class via Composer, all you have to do is `require_once 'vendor/autoload.php';` and everything will be taken care of. Alternatively, if you're installing it manually (or without Composer), and don't want to use a PSR-4 autoloader, you can simply require or include the class into your projects (which may be much easier in many cases) by including the respective statement to point to the class file in the relevant PHP files.

---


### How to use:

The simplest way to use Aggregator is to create a new instance of the class and enter some data to be aggregated as a parameter to the aggregate method. The aggregate method will return an aggregate of the entered data.

Example:
```PHP
<?php
use \CIDRAM\Aggregator\Aggregator;

$Aggregator = new Aggregator();
$Output = $Aggregator->aggregate($Input);
```

Or, if the file helpers.php is loaded, this function will be available:
```PHP
$Output = aggregate($Input);
```

*Note: The function `aggregate` will be available if you installed the package via `composer`, but otherwise, you'll need to include the file `src/helpers.php`.*

In the case of the above example, if this is entered as `$Input`:
```
127.0.0.1 Some arbitrary single IPs from here
Expand Down Expand Up @@ -93,6 +95,8 @@ It is possible to obtain more information about each aggregation operation if de

```PHP
<?php
use \CIDRAM\Aggregator\Aggregator;

$Aggregator = new Aggregator();
$Aggregator->Results = true;
$Output = $Aggregator->aggregate($Input);
Expand All @@ -116,6 +120,8 @@ Regardless of whether "Results" is `true` or `false`, after each aggregation ope
Example:
```PHP
<?php
use \CIDRAM\Aggregator\Aggregator;

$Aggregator = new Aggregator();
$Output = $Aggregator->aggregate($Input);
echo $Aggregator->ProcessingTime . "\n";
Expand All @@ -125,7 +131,6 @@ Additionally, "ExpandIPv4" and "ExpandIPv6" public methods are provided with the

---


### Other information:

#### Licensing:
Expand All @@ -136,5 +141,4 @@ Please use the issues page of this repository.

---


*Last modified: 22 September 2017 (2017.09.22).*
*Last modified: 12 October 2017 (2017.10.12).*
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,24 @@
},
"suggest": {
"php": ">=7.0.0 (this package performs significantly better with PHP >= 7.0.0 than it does with previous versions)."
},
"autoload": {
"psr-4": {
"CIDRAM\\Aggregator\\": "src/"
},
"files": ["src/helpers.php"]
},
"autoload-dev": {
"psr-4": {
"CIDRAM\\Aggregator\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^4.8"
},
"scripts": {
"test": [
"phpunit"
]
}
}
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Unit Tests">
<directory>tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
46 changes: 28 additions & 18 deletions aggregator.php → src/Aggregator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace CIDRAM\Aggregator;

/**
* Aggregator v1.0.1-DEV (last modified: 2017.10.08).
* Aggregator v1.1.0-DEV (last modified: 2017.10.12).
*
* Description: A stand-alone class implementation of the IPv4+IPv6 IP+CIDR
* aggregator from CIDRAM.
Expand All @@ -12,16 +14,16 @@
* AGGREGATOR COPYRIGHT 2017 and beyond by Caleb Mazalevskis (Maikuolan).
*
* License: GNU/GPLv2
*
* @see LICENSE.txt
*/

class Aggregator
{

/** Input. */
public $Input = '';

/** Outout. */
/** Output. */
public $Output = '';

/** Results switch. */
Expand Down Expand Up @@ -53,9 +55,9 @@ class Aggregator
*
* Adapted from CIDRAM/CIDRAM->vault/functions.php->$CIDRAM['ExpandIPv4']().
*
* @param string $Addr Refer to the description above.
* @param bool $ValidateOnly If true, just checks if the IP is valid only.
* @param int $FactorLimit Maximum number of CIDRs to return (default: 32).
* @param string $Addr Refer to the description above.
* @param bool $ValidateOnly If true, just checks if the IP is valid only.
* @param int $FactorLimit Maximum number of CIDRs to return (default: 32).
* @return bool|array Refer to the description above.
*/
public function ExpandIPv4($Addr, $ValidateOnly = false, $FactorLimit = 32)
Expand All @@ -64,14 +66,15 @@ public function ExpandIPv4($Addr, $ValidateOnly = false, $FactorLimit = 32)
'/^([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.([01]?[0-9]{1,2}|2[0-4][0-' .
'9]|25[0-5])\.([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.([01]?[0-9]{1,2' .
'}|2[0-4][0-9]|25[0-5])$/i',
$Addr, $Octets)) {
$Addr, $Octets)
) {
return false;
}
if ($ValidateOnly) {
return true;
}
$CIDRs = array();
$Base = array(0, 0, 0, 0);
$CIDRs = [];
$Base = [0, 0, 0, 0];
for ($Cycle = 0; $Cycle < 4; $Cycle++) {
for ($Size = 128, $Step = 0; $Step < 8; $Step++, $Size /= 2) {
$CIDR = $Step + ($Cycle * 8);
Expand All @@ -82,6 +85,7 @@ public function ExpandIPv4($Addr, $ValidateOnly = false, $FactorLimit = 32)
}
}
}

return $CIDRs;
}

Expand All @@ -93,9 +97,9 @@ public function ExpandIPv4($Addr, $ValidateOnly = false, $FactorLimit = 32)
*
* Adapted from CIDRAM/CIDRAM->vault/functions.php->$CIDRAM['ExpandIPv6']().
*
* @param string $Addr Refer to the description above.
* @param bool $ValidateOnly If true, just checks if the IP is valid only.
* @param int $FactorLimit Maximum number of CIDRs to return (default: 128).
* @param string $Addr Refer to the description above.
* @param bool $ValidateOnly If true, just checks if the IP is valid only.
* @param int $FactorLimit Maximum number of CIDRs to return (default: 128).
* @return bool|array Refer to the description above.
*/
public function ExpandIPv6($Addr, $ValidateOnly = false, $FactorLimit = 128)
Expand All @@ -119,7 +123,8 @@ public function ExpandIPv6($Addr, $ValidateOnly = false, $FactorLimit = 128)
'-4]\d)|(\d{1,2}))\b))|([0-9a-f]{1,4}\:\:([0-9a-f]{1,4}\:){0,5}[0-9a' .
'-f]{1,4})|(\:\:([0-9a-f]{1,4}\:){0,6}[0-9a-f]{1,4})|(([0-9a-f]{1,4}' .
'\:){1,7}\:)$/i',
$Addr)) {
$Addr)
) {
return false;
}
if ($ValidateOnly) {
Expand All @@ -134,7 +139,7 @@ public function ExpandIPv6($Addr, $ValidateOnly = false, $FactorLimit = 128)
}
if (substr_count($NAddr, '::')) {
$c = 7 - substr_count($Addr, ':');
$Arr = array(':0:', ':0:0:', ':0:0:0:', ':0:0:0:0:', ':0:0:0:0:0:', ':0:0:0:0:0:0:');
$Arr = [':0:', ':0:0:', ':0:0:0:', ':0:0:0:0:', ':0:0:0:0:0:', ':0:0:0:0:0:0:'];
if (!isset($Arr[$c])) {
return false;
}
Expand All @@ -153,8 +158,8 @@ public function ExpandIPv6($Addr, $ValidateOnly = false, $FactorLimit = 128)
$NAddr[5] = hexdec($NAddr[5]);
$NAddr[6] = hexdec($NAddr[6]);
$NAddr[7] = hexdec($NAddr[7]);
$CIDRs = array();
$Base = array(0, 0, 0, 0, 0, 0, 0, 0);
$CIDRs = [];
$Base = [0, 0, 0, 0, 0, 0, 0, 0];
for ($Cycle = 0; $Cycle < 8; $Cycle++) {
for ($Size = 32768, $Step = 0; $Step < 16; $Step++, $Size /= 2) {
$CIDR = $Step + ($Cycle * 16);
Expand Down Expand Up @@ -184,6 +189,7 @@ public function ExpandIPv6($Addr, $ValidateOnly = false, $FactorLimit = 128)
continue;
}
}

return $CIDRs;
}

Expand All @@ -197,6 +203,7 @@ public function aggregate($In)
$this->stripInvalidRangesAndSubs($this->Output);
$this->mergeRanges($this->Output);
$this->ProcessingTime = microtime(true) - $Begin;

return $this->Output;
}

Expand All @@ -208,7 +215,8 @@ private function stripInvalidCharactersAndSort(&$In)
$this->NumberEntered = count($In);
}
$In = array_filter(array_unique(array_map(function ($Line) {
$Line = preg_replace(array('~^[^0-9a-f:./]*~i', '~[ \t].*$~', '~[^0-9a-f:./]*$~i'), '', $Line);
$Line = preg_replace(['~^[^0-9a-f:./]*~i', '~[ \t].*$~', '~[^0-9a-f:./]*$~i'], '', $Line);

return (!$Line || !preg_match('~[0-9a-f:./]+~i', $Line) || preg_match('~[^0-9a-f:./]+~i', $Line)) ? '' : $Line;
}, $In)));
usort($In, function ($A, $B) {
Expand All @@ -234,6 +242,7 @@ private function stripInvalidCharactersAndSort(&$In)
if ($B === false) {
return 0;
}

return 1;
}
if ($B === false) {
Expand All @@ -244,8 +253,10 @@ private function stripInvalidCharactersAndSort(&$In)
if ($ASize === $BSize) {
return 0;
}

return ($ASize > $BSize) ? 1 : -1;
}

return ($Compare < 0) ? -1 : 1;
});
$In = implode("\n", $In);
Expand Down Expand Up @@ -355,5 +366,4 @@ public function resetNumbers()
$this->NumberReturned = 0;
$this->ProcessingTime = 0;
}

}
13 changes: 13 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
if (!function_exists('aggregate')) {
/**
* Aggregate it!
*
* @param string $input
* @return string
*/
function aggregate($input)
{
return (new \CIDRAM\Aggregator\Aggregator())->aggregate($input);
}
}
23 changes: 13 additions & 10 deletions .travis.php → tests/AggregatorTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php
require __DIR__ . '/aggregator.php';
namespace CIDRAM\Aggregator;

$ClassNames = array('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
if (class_exists($ClassNames[0]) && !class_exists($ClassNames[1])) {
class_alias($ClassNames[0], $ClassNames[1]);
}
unset($ClassNames);
use PHPUnit\Framework\TestCase;

class Experimental extends \PHPUnit_Framework_TestCase
class AggregatorTest extends TestCase
{
public function testInOut() {
public function testInOut()
{
$TestInput = '127.0.0.1 Some arbitrary single IPs from here
127.0.0.2
127.0.0.3
Expand Down Expand Up @@ -57,20 +54,26 @@ public function testInOut() {
$Aggregator = new Aggregator();
$Aggregator->Results = true;
$Aggregated = $Aggregator->aggregate($TestInput);
$ExpectedOutput = str_replace(PHP_EOL, "\n", $ExpectedOutput);

$this->NumberEntered = $Aggregator->NumberEntered;
$this->NumberRejected = $Aggregator->NumberRejected;
$this->NumberAccepted = $Aggregator->NumberAccepted;
$this->NumberMerged = $Aggregator->NumberMerged;
$this->NumberReturned = $Aggregator->NumberReturned;
$this->assertEquals($ExpectedOutput, $Aggregated, 'Actual aggregated output does not match expected aggregated output!');
}
public function testExpandIPv4() {

public function testExpandIPv4()
{
$Aggregator = new Aggregator();
$Out = $Aggregator->ExpandIPv4('127.0.0.1');
$Checksum = md5(serialize($Out));
$this->assertEquals('cd37d1d14133dfd75f9dd13414cdcd76', $Checksum, 'ExpandIPv4 output does not match expected output!');
}
public function testExpandIPv6() {

public function testExpandIPv6()
{
$Aggregator = new Aggregator();
$Out = $Aggregator->ExpandIPv6('2002::1');
$Checksum = md5(serialize($Out));
Expand Down

0 comments on commit c845552

Please sign in to comment.