Skip to content
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

Symfony 5 support #97

Merged
merged 11 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
98 changes: 29 additions & 69 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,88 +1,48 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4

sudo: false

matrix:
fast_finish: true
include:
- php: 5.4
- php: 7.2
env: COMPOSER_FLAGS="--prefer-lowest"
- php: 5.5
env: COMPOSER_FLAGS="--prefer-lowest"
- php: 5.6
env: SYMFONY_VERSION=2.3.*
- php: 5.6
env: SYMFONY_VERSION=2.6.*
- php: 5.6
env: SYMFONY_VERSION=2.7.*
- php: 5.6
env: SYMFONY_VERSION=2.8.*
- php: 5.6
env: SYMFONY_VERSION=3.0.*
- php: 5.6
env: SYMFONY_VERSION=3.1.*
- php: 5.6
env: SYMFONY_VERSION=3.2.*
- php: 5.6
env: SYMFONY_VERSION=3.3.*
- php: 5.6
- php: 7.2
env: SYMFONY_VERSION=3.4.*
- php: 7.0
- php: 7.2
env: SYMFONY_VERSION=4.3.*
- php: 7.2
env: SYMFONY_VERSION=4.4.*
- php: 7.2
env: SYMFONY_VERSION=5.0.*
- php: 7.3
env: COMPOSER_FLAGS="--prefer-lowest"
- php: 7.0
env: SYMFONY_VERSION=2.3.*
- php: 7.0
env: SYMFONY_VERSION=2.6.*
- php: 7.0
env: SYMFONY_VERSION=2.7.*
- php: 7.0
env: SYMFONY_VERSION=2.8.*
- php: 7.0
env: SYMFONY_VERSION=3.0.*
- php: 7.0
env: SYMFONY_VERSION=3.1.*
- php: 7.0
env: SYMFONY_VERSION=3.2.*
- php: 7.0
env: SYMFONY_VERSION=3.3.*
- php: 7.0
- php: 7.3
env: SYMFONY_VERSION=3.4.*
- php: 7.1
- php: 7.3
env: SYMFONY_VERSION=4.3.*
- php: 7.3
env: SYMFONY_VERSION=4.4.*
- php: 7.3
env: SYMFONY_VERSION=5.0.*
- php: 7.4
env: COMPOSER_FLAGS="--prefer-lowest"
- php: 7.1
env: SYMFONY_VERSION=2.3.*
- php: 7.1
env: SYMFONY_VERSION=2.6.*
- php: 7.1
env: SYMFONY_VERSION=2.7.*
- php: 7.1
env: SYMFONY_VERSION=2.8.*
- php: 7.1
env: SYMFONY_VERSION=3.0.*
- php: 7.1
env: SYMFONY_VERSION=3.1.*
- php: 7.1
env: SYMFONY_VERSION=3.2.*
- php: 7.1
env: SYMFONY_VERSION=3.3.*
- php: 7.1
- php: 7.4
env: SYMFONY_VERSION=3.4.*
- php: 7.1
env: SYMFONY_VERSION=4.0.*
- php: 7.1
env: SYMFONY_VERSION=4.1.*
- php: 7.1
env: SYMFONY_VERSION=4.2.*
- php: 7.4
env: SYMFONY_VERSION=4.3.*
- php: 7.4
env: SYMFONY_VERSION=4.4.*
- php: 7.4
env: SYMFONY_VERSION=5.0.*
allow_failures:
- php: 5.6

- env: COMPOSER_FLAGS="--prefer-lowest"
- env: SYMFONY_VERSION=5.0.*
before_install:
- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
before_script:
Expand Down
8 changes: 1 addition & 7 deletions DependencyInjection/NoxlogicRateLimitExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,7 @@ private function loadServices(ContainerBuilder $container, array $config)
}

if ($config['fos_oauth_key_listener']) {
// Set the SecurityContext for Symfony < 2.6
// Replace with xml when < 2.6 is dropped.
if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
$tokenStorageReference = new Reference('security.token_storage');
} else {
$tokenStorageReference = new Reference('security.context');
}
$tokenStorageReference = new Reference('security.token_storage');
$container->getDefinition('noxlogic_rate_limit.oauth_key_generate_listener')->replaceArgument(0, $tokenStorageReference);
} else {
$container->removeDefinition('noxlogic_rate_limit.oauth_key_generate_listener');
Expand Down
6 changes: 3 additions & 3 deletions EventListener/HeaderModificationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Noxlogic\RateLimitBundle\EventListener;

use Noxlogic\RateLimitBundle\Events\ProxyFilterResponseEvent;
use Noxlogic\RateLimitBundle\Service\RateLimitInfo;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;

class HeaderModificationListener extends BaseListener
{
Expand All @@ -17,9 +17,9 @@ public function __construct($defaultParameters = array())
}

/**
* @param FilterResponseEvent $event
* @param ProxyFilterResponseEvent $event
*/
public function onKernelResponse(FilterResponseEvent $event)
public function onKernelResponse(ProxyFilterResponseEvent $event)
{
$request = $event->getRequest();

Expand Down
3 changes: 1 addition & 2 deletions EventListener/OauthKeyGenerateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace Noxlogic\RateLimitBundle\EventListener;

use Noxlogic\RateLimitBundle\Events\GenerateKeyEvent;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class OauthKeyGenerateListener
{
/**
* @var SecurityContextInterface|TokenStorageInterface
* @var TokenStorageInterface
*/
protected $tokenStorage;

Expand Down
36 changes: 24 additions & 12 deletions EventListener/RateLimitAnnotationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
namespace Noxlogic\RateLimitBundle\EventListener;

use Noxlogic\RateLimitBundle\Annotation\RateLimit;
use Noxlogic\RateLimitBundle\Events\ProxyFilterControllerEvent;
use Noxlogic\RateLimitBundle\Events\CheckedRateLimitEvent;
use Noxlogic\RateLimitBundle\Events\GenerateKeyEvent;
use Noxlogic\RateLimitBundle\Events\RateLimitEvents;
use Noxlogic\RateLimitBundle\Exception\RateLimitExceptionInterface;
use Noxlogic\RateLimitBundle\Service\RateLimitService;
use Noxlogic\RateLimitBundle\Util\PathLimitProcessor;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface as LegacyEventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\Route;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class RateLimitAnnotationListener extends BaseListener
{

/**
* @var eventDispatcherInterface
* @var EventDispatcherInterface | LegacyEventDispatcherInterface
*/
protected $eventDispatcher;

Expand All @@ -38,7 +38,7 @@ class RateLimitAnnotationListener extends BaseListener
* @param RateLimitService $rateLimitService
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
$eventDispatcher,
RateLimitService $rateLimitService,
PathLimitProcessor $pathLimitProcessor
) {
Expand All @@ -48,9 +48,9 @@ public function __construct(
}

/**
* @param FilterControllerEvent $event
* @param ProxyFilterControllerEvent $event
*/
public function onKernelController(FilterControllerEvent $event)
public function onKernelController(ProxyFilterControllerEvent $event)
{
// Skip if the bundle isn't enabled (for instance in test environment)
if( ! $this->getParameter('enabled', true)) {
Expand All @@ -68,7 +68,7 @@ public function onKernelController(FilterControllerEvent $event)

// Another treatment before applying RateLimit ?
$checkedRateLimitEvent = new CheckedRateLimitEvent($event->getRequest(), $rateLimit);
$this->eventDispatcher->dispatch(RateLimitEvents::CHECKED_RATE_LIMIT, $checkedRateLimitEvent);
$this->dispatch(RateLimitEvents::CHECKED_RATE_LIMIT, $checkedRateLimitEvent);
$rateLimit = $checkedRateLimitEvent->getRateLimit();

// No matching annotation found
Expand Down Expand Up @@ -163,25 +163,25 @@ protected function findBestMethodMatch(Request $request, array $annotations)
return $best_match;
}

private function getKey(FilterControllerEvent $event, RateLimit $rateLimit, array $annotations)
private function getKey(ProxyFilterControllerEvent $event, RateLimit $rateLimit, array $annotations)
{
// Let listeners manipulate the key
$keyEvent = new GenerateKeyEvent($event->getRequest(), '', $rateLimit->getPayload());

$rateLimitMethods = join('.', $rateLimit->getMethods());
$rateLimitMethods = implode('.', $rateLimit->getMethods());
$keyEvent->addToKey($rateLimitMethods);

$rateLimitAlias = count($annotations) === 0
? str_replace('/', '.', $this->pathLimitProcessor->getMatchedPath($event->getRequest()))
: $this->getAliasForRequest($event);
$keyEvent->addToKey($rateLimitAlias);

$this->eventDispatcher->dispatch(RateLimitEvents::GENERATE_KEY, $keyEvent);
$this->dispatch(RateLimitEvents::GENERATE_KEY, $keyEvent);

return $keyEvent->getKey();
}

private function getAliasForRequest(FilterControllerEvent $event)
private function getAliasForRequest(ProxyFilterControllerEvent $event)
{
if (($route = $event->getRequest()->attributes->get('_route'))) {
return $route;
Expand All @@ -207,4 +207,16 @@ private function getAliasForRequest(FilterControllerEvent $event)

return 'other';
}

private function dispatch($eventName, $event)
{
if ($this->eventDispatcher instanceof EventDispatcherInterface) {
// Symfony >= 4.3
$this->eventDispatcher->dispatch($event, $eventName);
} else {
// Symfony 3.4
$this->eventDispatcher->dispatch($eventName, $event);
}
}

}
22 changes: 22 additions & 0 deletions Events/AbstractEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Noxlogic\RateLimitBundle\Events;

use Symfony\Component\EventDispatcher\Event as LegacyEvent;
use Symfony\Contracts\EventDispatcher\Event;

if (!class_exists('Symfony\\Contracts\\EventDispatcher\\Event')) {
/**
* Symfony 3.4
*/
abstract class AbstractEvent extends LegacyEvent
{
}
} else {
/**
* Symfony >= 4.3
*/
abstract class AbstractEvent extends Event
{
}
}
3 changes: 1 addition & 2 deletions Events/CheckedRateLimitEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace Noxlogic\RateLimitBundle\Events;

use Noxlogic\RateLimitBundle\Annotation\RateLimit;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;

class CheckedRateLimitEvent extends Event
class CheckedRateLimitEvent extends AbstractEvent
{

/**
Expand Down
3 changes: 1 addition & 2 deletions Events/GenerateKeyEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Noxlogic\RateLimitBundle\Events;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;

class GenerateKeyEvent extends Event
class GenerateKeyEvent extends AbstractEvent
{

/** @var Request */
Expand Down
22 changes: 22 additions & 0 deletions Events/ProxyFilterControllerEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace Noxlogic\RateLimitBundle\Events;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent as LegacyEvent;

if (!class_exists('Symfony\\Component\HttpKernel\\Event\\ControllerEvent')) {
/**
* Symfony 3.4
*/
class ProxyFilterControllerEvent extends LegacyEvent
{
}
} else {
/**
* Symfony >= 4.3
*/
class ProxyFilterControllerEvent extends ControllerEvent
{
}
}
22 changes: 22 additions & 0 deletions Events/ProxyFilterResponseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace Noxlogic\RateLimitBundle\Events;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent as LegacyEvent;

if (!class_exists('Symfony\\Component\HttpKernel\\Event\\ResponseEvent')) {
/**
* Symfony 3.4
*/
class ProxyFilterResponseEvent extends LegacyEvent
{
}
} else {
/**
* Symfony >= 4.3
*/
class ProxyFilterResponseEvent extends ResponseEvent
{
}
}
Loading