2017 © Pedro Peláez
 

library php-ratelimiter

A framework agnostic rate limiter for PHP

image

evoluted/php-ratelimiter

A framework agnostic rate limiter for PHP

  • Tuesday, August 23, 2016
  • by evoluted
  • Repository
  • 6 Watchers
  • 2 Stars
  • 9,148 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 11 Versions
  • 12 % Grown

The README.md

PHP Ratelimiter

A framework independent, flexible and highly extensible rate limiter for PHP., (*1)

SensioLabsInsight Scrutinizer Code Quality Code Coverage Code Climate Build Status Latest Stable Version License, (*2)

Installation

With Composer

It is best installed it through packagist by including sunspikes/php-ratelimiter in your project composer.json require:, (*3)

``` json "require": { "sunspikes/php-ratelimiter": "dev-master" }, (*4)


### Without Composer You can also download it from [Github] (https://github.com/sunspikes/php-ratelimiter), but no autoloader is provided so you'll need to register it with your own PSR-4 compatible autoloader. ## Usage ### Overview ```php // 1. Make a rate limiter with limit 3 attempts in 10 minutes $cacheAdapter = new DesarrollaCacheAdapter((new DesarrollaCacheFactory())->make()); $ratelimiter = new RateLimiter(new ThrottlerFactory(), new HydratorFactory(), $cacheAdapter, 3, 600); // 2. Get a throttler for path /login $loginThrottler = $ratelimiter->get('/login'); // 3. Register a hit $loginThrottler->hit() // 4. Check if it reached the limit if ($loginThrottler->check()) { // access permitted } else { // access denied } // Or combine the steps 3 & 4 if ($loginThrottler->access()) { // access permitted } else { // access denied } // To get the number of hits print $loginThrottler->count(); // or count($throttler)

Configuration

By default PHP Ratelimiter uses the desarolla2 cache adapter, the sample configuration provided in config/config.php, (*5)

You can configure the drivers in config.php, for example to use memcache change the driver to 'memcache', (*6)

return [
    'default_ttl' => 3600,
    'driver'      => 'memcache',
    'memcache' => [
        //....
    ],
];

Extending

The PHP Ratelimiter is highly extensible, you can have custom adapters by implementing Sunspikes\Ratelimit\Cache\Adapter\CacheAdapterInterface, (*7)

For example to use Doctrine cache adapter, (*8)

class DoctrineCacheAdapter implements CacheAdapterInterface
{
    public function __construct($cache)
    {
        $this->cache = $cache;
    }

    // Implement the methods
}

// Build adapter using APC cache driver
$adapter = new DoctrineCacheAdapter(new \Doctrine\Common\Cache\ApcCache());

Also you can have custom hydrators by implementing Sunspikes\Ratelimit\Throttle\Hydrator\DataHydratorInterface, (*9)

For example to use a Symfony Request object instead of custom URL for ratelimiting, (*10)

class RequestHydrator implements DataHydratorInterface
{
    public function hydrate($data, $limit, $ttl)
    {
        // Make the key string
        $key = $data->getClientIp() . $data->getPathInfo();

        return new Data($key, $limit, $ttl);
    }
}

// Hydrate the request to Data object
$hydrator = new RequestHydrator();

Then decorate or extend the HydratorFactory to recognize your data, (*11)

use Hydrator\FactoryInterface;

class MyHydratorFactory implements FactoryInterface
{
    private $defaultFactory;

    public function __construct(FactoryInterface $defaultFactory)
    {
        $this->defaultFactory = $defaultFactory;
    }

    public function make($data)
    {
        if ($data instanceof Request) {
            return new RequestHydrator();
        }

        return $this->defaultFactory->make($data);
    }
}

Author

Krishnaprasad MG [@sunspikes], (*12)

Contributing

Please feel free to send pull requests., (*13)

License

This is an open-sourced software licensed under the MIT license., (*14)

The Versions

23/08 2016

dev-master

9999999-dev

A framework agnostic rate limiter for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

throttle rate limit throttling ratelimit krishnaprasad mg sunspikes php-ratelimiter

23/08 2016

1.3.1

1.3.1.0

A framework agnostic rate limiter for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

throttle rate limit throttling ratelimit krishnaprasad mg sunspikes php-ratelimiter

18/08 2016

dev-MWW-3452-ms

dev-MWW-3452-ms

A framework agnostic rate limiter for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

throttle rate limit throttling ratelimit krishnaprasad mg sunspikes php-ratelimiter

16/08 2016

dev-MWW-3452

dev-MWW-3452

A framework agnostic rate limiter for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

throttle rate limit throttling ratelimit krishnaprasad mg sunspikes php-ratelimiter

12/08 2016

1.3.0

1.3.0.0

A framework agnostic rate limiter for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

throttle rate limit throttling ratelimit krishnaprasad mg sunspikes php-ratelimiter

29/07 2016

dev-update-desarrolla

dev-update-desarrolla

A framework agnostic rate limiter for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

throttle rate limit throttling ratelimit krishnaprasad mg sunspikes php-ratelimiter

20/07 2016

v1.2-rc

1.2.0.0-RC

A framework agnostic rate limiter for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

throttle rate limit throttling ratelimit krishnaprasad mg sunspikes php-ratelimiter

11/07 2016

dev-dependency-injection

dev-dependency-injection

A framework agnostic rate limiter for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

throttle rate limit throttling ratelimit krishnaprasad mg sunspikes php-ratelimiter

20/06 2016

v1.1-rc

1.1.0.0-RC

A framework agnostic rate limiter for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

throttle rate limit throttling ratelimit krishnaprasad mg sunspikes php-ratelimiter

27/10 2015

v1.0-rc

1.0.0.0-RC

A framework agnostic rate limiter for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

throttle rate limit throttling ratelimit krishnaprasad mg sunspikes php-ratelimiter

08/09 2015

v1.0

1.0.0.0

A framework agnostic rate limiter for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

throttle rate limit throttling ratelimit krishnaprasad mg sunspikes php-ratelimiter