Skip to content

Commit

Permalink
chore: support symfony 5 (#75)
Browse files Browse the repository at this point in the history
* chore: support symfony 5

DataCollector::collect method signature change since SF 4.4

* fix: travis config + doc

* fix: swap arguments of dispatch()

@see symfony/symfony@75369da

* fix: renamed events

GetResponseForExceptionEvent replaced by ExceptionEvent;
PostResponseEvent replaced by TerminateEvent;

* refactor: use the spaceless filter instead of spaceless tag (since Twig 1.38)

* chore: only needed symfony dependencies

* chore: CI : php compatibility for SF5 (>=7.2.5)

* style: remove spaceless filter

* add new line at the end of the file
  • Loading branch information
SelviA authored Jul 26, 2020
1 parent 94941d9 commit 319f7fc
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 54 deletions.
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ php:
- 7.4

env:
- SYMFONY_VERSION=3.4.0
- SYMFONY_VERSION=4.0.*
- SYMFONY_VERSION=4.4.0
- SYMFONY_VERSION=5.*.*

before_script:
- wget http://getcomposer.org/composer.phar
Expand All @@ -18,3 +18,8 @@ before_script:
script:
- bin/coke
- bin/atoum

jobs:
exclude:
- php: 7.1
env: SYMFONY_VERSION=5.*.*
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ StatsdBundle [![Build Status](https://travis-ci.org/M6Web/StatsdBundle.png?branc

## Bundle easing the [statsd](https://github.com/etsy/statsd/) usage.

This bundle currently supports `php >=7.1.3` and `symfony 3.4/4.x`.
This bundle currently supports `php >=7.1.3` and `symfony 4.4/5.x`.

If you need support for `symfony 3.4/<4.4`, use version [v3.0.0](https://github.com/M6Web/StatsdBundle/tree/v3.0.0).
If you need support for `php >=5.4.0` and `symfony 2.x/3.x`, use version [v2.15.1](https://github.com/M6Web/StatsdBundle/tree/v2.15.1).

* [about](doc/about.md)
Expand Down
16 changes: 11 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@
],
"config": {
"bin-dir": "bin",
"vendor-dir": "vendor"
"vendor-dir": "vendor",
"sort-packages": true
},
"require": {
"php": "^7.1.3",
"symfony/property-access": "^3.4 || ^4.0",
"symfony/property-access": "^4.4 || ^5.0",
"m6web/statsd": "^1.3"
},
"require-dev": {
"atoum/atoum": "^2.8|^3.0",
"symfony/symfony": "^3.4 || ^4.0 || ^5.0",
"m6web/coke" : "~1.2",
"m6web/symfony2-coding-standard" : "~1.2"
"m6web/coke": "~1.2",
"m6web/symfony2-coding-standard": "~1.2",
"symfony/config": "^4.4 || ^5.0",
"symfony/console": "^4.4 || ^5.0",
"symfony/dependency-injection": "^4.4 || ^5.0",
"symfony/event-dispatcher-contracts": "^1.1 || ^2.1",
"symfony/http-foundation": "^4.4 || ^5.0",
"symfony/http-kernel": "^4.4 || ^5.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ On the Symfony event dispatcher, when the ```forum.read``` event is fired, our s

So you can now just fire the event from a controller :
```php
$this->get('event_dispatcher')->dispatch('forum.read', new Symfony\Component\EventDispatcher\Event());
$this->get('event_dispatcher')->dispatch(new Symfony\Contracts\EventDispatcher\Event(), 'forum.read');
```

It's also possible to create tokens in the Symfony configuration, allowing you to pass custom value in the node.
Expand Down
1 change: 0 additions & 1 deletion src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use M6Web\Bundle\StatsdBundle\Statsd\MonitorableEventInterface;
use M6Web\Component\Statsd\Client as BaseClient;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\PropertyAccess;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/DataCollector/StatsdDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public function addStatsdClient($clientAlias, $statsdClient)
*
* @param Request $request The request object
* @param Response $response The response object
* @param \Exception $exception An exception
* @param \Throwable $exception An exception
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Event/ConsoleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace M6Web\Bundle\StatsdBundle\Event;

use Symfony\Component\Console\Event\ConsoleEvent as BaseConsoleEvent;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Base console event
Expand Down
16 changes: 8 additions & 8 deletions src/Listener/ConsoleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use M6Web\Bundle\StatsdBundle\Event\ConsoleEvent;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Console\Event\ConsoleEvent as BaseConsoleEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;

Expand Down Expand Up @@ -43,7 +43,7 @@ public function onCommand(BaseConsoleEvent $e)
{
$this->startTime = microtime(true);

$this->dispatch(ConsoleEvent::COMMAND, $e);
$this->dispatch($e, ConsoleEvent::COMMAND);
}

/**
Expand All @@ -53,29 +53,29 @@ public function onTerminate(ConsoleTerminateEvent $e)
{
// For non-0 exit command, fire an ERROR event
if ($e->getExitCode() != 0) {
$this->dispatch(ConsoleEvent::ERROR, $e);
$this->dispatch($e, ConsoleEvent::ERROR);
}

$this->dispatch(ConsoleEvent::TERMINATE, $e);
$this->dispatch($e, ConsoleEvent::TERMINATE);
}

/**
* @param BaseConsoleEvent $e
*/
public function onException(BaseConsoleEvent $e)
{
$this->dispatch(ConsoleEvent::EXCEPTION, $e);
$this->dispatch($e, ConsoleEvent::EXCEPTION);
}

/**
* Dispatch custom event
*
* @param string $eventName
* @param BaseConsoleEvent $e
* @param string $eventName
*
* @return boolean
*/
protected function dispatch($eventName, BaseConsoleEvent $e)
protected function dispatch(BaseConsoleEvent $e, $eventName)
{
if (!is_null($this->eventDispatcher)) {
$class = str_replace(
Expand All @@ -90,7 +90,7 @@ protected function dispatch($eventName, BaseConsoleEvent $e)
!is_null($this->startTime) ? microtime(true) - $this->startTime : null
);

return $this->eventDispatcher->dispatch($eventName, $finaleEvent);
return $this->eventDispatcher->dispatch($finaleEvent, $eventName);
} else {
return false;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Resources/views/Collector/statsd.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

{% block menu %}
<span class="label">
<span class="icon">{% spaceless %}
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAAsTAAALEwEAmpwYAAAEVUlEQVRYhe2Wz0sVaxjHP+/8OOdM53SmOJwgMTtYSHBBTLDFrUjolmvBIxiCFLhI6F5xIbToD3Aj6d3VQgqiRYu6riJaWd3ERUq20pQDSgv1kkiemTMzZ9670JnrHH9gxPXehQ8MM/N93/f5fp8f78wLh3Zo/7GJ4OGXa9cWTPNY9UGQuq5rj/7xwgDQAtA0j1X/Pjx0EPzc+fW3RPCsVQ52dXWRSqWIxWJomkYul2NhYSEcF0Jw6tSpCAaEmJQyxKqrqykUCnieh+M4WJbFyMhIZF0oIFiYTCZZXV0lk8lgGAZCCJLJJIqi/LNI0zh69GjUkaaRSqXCd9/3EUKgqirFYpHl5WWqqqoiXBEBvu+HjtLpNJlMhnQ6jWmaKIqCECLMgGma29IaYIFzKSWpVIpsNouu69i2ja7rEa4dM6AoCpqmoes6mqaFkW917Pt+xEngtBJTFAVd18Or0teOAnK5HJZlcfz4cRKJBKVSiUQiEXG8X8xxHNLpNLquU1VVRXt7O4qi7F2C+fl5bNtmfX2dRCJBNptleXk54ng3bG1tjVKpFMGWlpYwTZN8Ps/Tp0+5efPm3iWQUuI4DrZt4/s+pVKJYrG4LdpKLBaL0d3dzdraGuPj40xNTVEqlfB9n7a2Nt6/f8/09DTxeHzvEgSklmXhed6+BbS0tDA2Nsbi4iJXr17l8uXLzM3NcfLkST5+/MjDhw+pra2NcO1Ygq1R67q+LwGtra3Yts2zZ89QVZU3b95w9uxZWltbmZmZYWRkBCFEhGPXDORyOeLxOIZhoKoqtm1z4sSJiICt2Llz57hw4QJDQ0OcPn06nOM4Do8ePUIIQW1tLcVikZqamt0zIDdVff78mfn5eVKpFFeuXOHWrVuMjo7y6tWrMOozZ84wNzfHkSNH6OnpYXBwEMuyKBQKEaHBPNd1+fbtG47jRLgAws9buaIETU1N5PN5Xr58iWEYDA8P09XVRTKZDEtw+/Zt3r59y8TEBJ7nUSwWI1cwz7IsbNumXC5HuCIZCAZ93+fSpUt0dnbS399PLBZjdnaWx48fc+PGDe7fv8+nT58oFApomsaDBw9wHGfPXgmaORSweY9moFxGSklTUxPd3d309vYyMzODEALHcfjy5QsDAwN0dHRQKBRoaGhgYGCAr1+/RqKtzMD6+jqWZYVbUkoZERCeB36+eFG+eP6clZUV7t69y8rKCqqqUmlSSlzXxXVdDMOI/KR2s4C0vr6ee/fukW9v589370SkBH7ZZ3V1lb6+Pl6/fo0QAiEEjY2NfPjwIeKwsbGR6enpbVjlvPPnzzM5OYmUEikl8Xh8YzuWd+gBr1wmmUyGNQvMdd2we78XC84BWzMhpcTbrQeAfaX0R21rD2wpQTn85zc3N4cT6urqth0+fgTbKMFOAjb35pMnT74vnO+0jfOEhI3s+xqgqaqatWzrr+vXWzL/KvumlX3f1XX9J8/zZg+C79D+3/Y3WY+lvPQgrdQAAAAASUVORK5CYII=" alt="Statsd" />
{% endspaceless %}</span>
<span class="icon"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAAsTAAALEwEAmpwYAAAEVUlEQVRYhe2Wz0sVaxjHP+/8OOdM53SmOJwgMTtYSHBBTLDFrUjolmvBIxiCFLhI6F5xIbToD3Aj6d3VQgqiRYu6riJaWd3ERUq20pQDSgv1kkiemTMzZ9670JnrHH9gxPXehQ8MM/N93/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XVdDMOI/KR2s4C0vr6ee/fukW9v589370SkBH7ZZ3V1lb6+Pl6/fo0QAiEEjY2NfPjwIeKwsbGR6enpbVjlvPPnzzM5OYmUEikl8Xh8YzuWd+gBr1wmmUyGNQvMdd2we78XC84BWzMhpcTbrQeAfaX0R21rD2wpQTn85zc3N4cT6urqth0+fgTbKMFOAjb35pMnT74vnO+0jfOEhI3s+xqgqaqatWzrr+vXWzL/KvumlX3f1XX9J8/zZg+C79D+3/Y3WY+lvPQgrdQAAAAASUVORK5CYII=" alt="Statsd" /></span>
<strong>Statsd</strong>
<span class="count">
<span>{{ collector.operations }}</span>
Expand Down
38 changes: 19 additions & 19 deletions src/Statsd/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace M6Web\Bundle\StatsdBundle\Statsd;

use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

use M6Web\Component\Statsd\Client;
Expand All @@ -32,30 +32,30 @@ public function __construct(Client $statsdClient, EventDispatcherInterface $even
/**
* onKernelException
*
* @param GetResponseForExceptionEvent $event
* @param ExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getException();
$exception = $event->getThrowable();
if ($exception instanceof HttpExceptionInterface) {
$code = $event->getException()->getStatusCode();
$code = $event->getThrowable()->getStatusCode();
} else {
$code = 'unknown';
}
$this->eventDispatcher->dispatch(
'statsd.exception',
new StatsdEvent($code)
new StatsdEvent($code),
'statsd.exception'
);
}

/**
* method called on the kernel.terminate event
*
* @param PostResponseEvent $event event
* @param TerminateEvent $event event
*
* @return void
*/
public function onKernelTerminate(PostResponseEvent $event)
public function onKernelTerminate(TerminateEvent $event)
{
$this->statsdClient->send();
}
Expand All @@ -76,9 +76,9 @@ public function onConsoleTerminate(ConsoleTerminateEvent $event)
* method called if base_collectors = true in config to dispatch base events
* (you still have to catch them)
*
* @param PostResponseEvent $event
* @param TerminateEvent $event
*/
public function dispatchBaseEvents(PostResponseEvent $event)
public function dispatchBaseEvents(TerminateEvent $event)
{
$this->dispatchMemory();
$this->dispatchRequestTime($event);
Expand All @@ -93,8 +93,8 @@ private function dispatchMemory()
$memory = ($memory > 1024 ? intval($memory / 1024) : 0);

$this->eventDispatcher->dispatch(
'statsd.memory_usage',
new StatsdEvent($memory)
new StatsdEvent($memory),
'statsd.memory_usage'
);
}

Expand All @@ -103,18 +103,18 @@ private function dispatchMemory()
* This time is a "fake" one, because some actions are performed before the initialization of the request
* It is ~100ms smaller than the real kernel time.
*
* @param PostResponseEvent $event
* @param TerminateEvent $event
*/
private function dispatchRequestTime(PostResponseEvent $event)
private function dispatchRequestTime(TerminateEvent $event)
{
$request = $event->getRequest();
$startTime = $request->server->get('REQUEST_TIME_FLOAT', $request->server->get('REQUEST_TIME'));
$time = microtime(true) - $startTime;
$time = round($time * 1000);

$this->eventDispatcher->dispatch(
'statsd.time',
new StatsdEvent($time)
new StatsdEvent($time),
'statsd.time'
);
}
}
18 changes: 9 additions & 9 deletions src/Tests/Units/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testHandleEventWithValidConfigIncrement()

$client = $this->getMockedClient();

$event = new \Symfony\Component\EventDispatcher\Event();
$event = new \Symfony\Contracts\EventDispatcher\Event();

$client->addEventToListen('test', array(
'increment' => 'stats.<name>'
Expand All @@ -59,7 +59,7 @@ public function testHandleEventWithValidConfigDecrement()
{
$client = $this->getMockedClient();

$event = new \Symfony\Component\EventDispatcher\Event();
$event = new \Symfony\Contracts\EventDispatcher\Event();

$client->addEventToListen('test', array(
'decrement' => 'stats.<name>'
Expand All @@ -83,7 +83,7 @@ public function testHandleEventWithImmediateSend()

$client = $this->getMockedClient();

$event = new \Symfony\Component\EventDispatcher\Event();
$event = new \Symfony\Contracts\EventDispatcher\Event();

$client->addEventToListen('test', array(
'increment' => 'stats.<name>',
Expand All @@ -109,7 +109,7 @@ public function testHandleEventWithToSendLimit()
$client = $this->getMockedClient();
$client->setToSendLimit(3);

$event = new \Symfony\Component\EventDispatcher\Event();
$event = new \Symfony\Contracts\EventDispatcher\Event();

$queue = new \SPLQueue;

Expand Down Expand Up @@ -148,7 +148,7 @@ public function testHandleEventWithInvalidConfigIncrement()
));

$this->exception(function () use ($client) {
$event = new \Symfony\Component\EventDispatcher\Event();
$event = new \Symfony\Contracts\EventDispatcher\Event();

$client->handleEvent($event, 'test');
});
Expand All @@ -166,7 +166,7 @@ public function testHandleEventWithInvalidEventTiming()
));

$this->exception(function () use ($client) {
$event = new \Symfony\Component\EventDispatcher\Event();
$event = new \Symfony\Contracts\EventDispatcher\Event();

$client->handleEvent($event, 'test');
});
Expand All @@ -178,7 +178,7 @@ public function testHandleEventWithInvalidEventTiming()
));

$this->exception(function () use ($client) {
$event = new \Symfony\Component\EventDispatcher\Event();
$event = new \Symfony\Contracts\EventDispatcher\Event();

$client->handleEvent($event, 'test');
});
Expand Down Expand Up @@ -253,7 +253,7 @@ public function testHandleEventCallsConfiguredTiming()
$dispatcher->addListener('test.event.name', [$client, 'handleEvent']);

$this
->if($dispatcher->dispatch('test.event.name', $event))
->if($dispatcher->dispatch($event, 'test.event.name'))
->mock($client)
->call('timing')
->withArguments('my.statsd.node', 101, 1.0)
Expand All @@ -278,7 +278,7 @@ public function testHandleEventDontCallTimingOnUnconfiguredEvent()
$dispatcher->addListener('test.event.name', [$client, 'handleEvent']);

$this
->if($dispatcher->dispatch('test.event.other.name', $event))
->if($dispatcher->dispatch($event, 'test.event.other.name'))
->mock($client)
->call('timing')
->never();
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Units/Client/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace M6Web\Bundle\StatsdBundle\Tests\Units\Client;

class Event extends \Symfony\Component\EventDispatcher\Event
class Event extends \Symfony\Contracts\EventDispatcher\Event
{
private $name = '';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class M6WebStatsdExtension extends atoum\test
protected function initContainer($resource, $debug = false)
{
$this->container = new ContainerBuilder();
$this->container->register('event_dispatcher', new EventDispatcher());
$this->container->register('event_dispatcher', EventDispatcher::class);
$this->container->registerExtension(new BaseM6WebStatsdExtension());
$this->loadConfiguration($this->container, $resource);
$this->container->setParameter('kernel.debug', $debug);
Expand Down

0 comments on commit 319f7fc

Please sign in to comment.