Skip to content

Commit 999c125

Browse files
authored
Merge pull request #16 from symfony/finish-pr-15
Finish pr 15
2 parents ee5e30e + 0737081 commit 999c125

File tree

7 files changed

+173
-9
lines changed

7 files changed

+173
-9
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: php
33
jobs:
44
include:
55
- php: '7.1'
6-
- php: 7.2
6+
- php: '7.2'
77
- php: '7.3'
88
env: lint=1
99
- php: '7.3'
@@ -24,7 +24,7 @@ install:
2424
- if [[ $deps = 'low' ]]; then
2525
composer update --prefer-dist --no-progress --no-suggest --prefer-stable --prefer-lowest --ansi;
2626
else
27-
composer update --prefer-dist --no-progress --no-suggest --ansi;
27+
composer update --prefer-dist --no-progress --no-suggest --prefer-stable --ansi;
2828
fi
2929

3030
script:

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
CHANGELOG
22
=========
33

4-
0.2.1
4+
0.3.0
55
-----
66

77
* Compatibility with Symfony 5
8+
* Add `TraceablePublisher` to collect debug information
9+
* Add `PublisherInterface`
10+
* Fix an error when using the `retry` parameter
811

912
0.2.0
1013
-----

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"extra": {
2929
"branch-alias": {
30-
"dev-master": "1.0.x-dev"
30+
"dev-master": "0.3.x-dev"
3131
},
3232
"thanks": {
3333
"name": "dunglas/mercure",
@@ -37,7 +37,12 @@
3737
"config": {
3838
"sort-packages": true
3939
},
40+
"suggest": {
41+
"symfony/stopwatch": "Integration with the profiler performances"
42+
},
4043
"require-dev": {
41-
"symfony/phpunit-bridge": "^4.2.4|^5.0"
42-
}
44+
"symfony/phpunit-bridge": "^4.2.4|^5.0",
45+
"symfony/stopwatch": "^4.3|^5.0"
46+
},
47+
"minimum-stability": "dev"
4348
}

src/Debug/TraceablePublisher.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Mercure Component project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Mercure\Debug;
15+
16+
use Symfony\Component\Mercure\PublisherInterface;
17+
use Symfony\Component\Mercure\Update;
18+
use Symfony\Component\Stopwatch\Stopwatch;
19+
use Symfony\Contracts\Service\ResetInterface;
20+
21+
/**
22+
* Traces updates for profiler.
23+
*
24+
* @author Vincent Chalamon <[email protected]>
25+
*
26+
* @experimental
27+
*/
28+
final class TraceablePublisher implements PublisherInterface, ResetInterface
29+
{
30+
private $publisher;
31+
private $stopwatch;
32+
private $messages = [];
33+
34+
public function __construct(PublisherInterface $publisher, Stopwatch $stopwatch)
35+
{
36+
$this->publisher = $publisher;
37+
$this->stopwatch = $stopwatch;
38+
}
39+
40+
public function __invoke(Update $update): string
41+
{
42+
$this->stopwatch->start(__CLASS__);
43+
44+
$content = ($this->publisher)($update);
45+
46+
$e = $this->stopwatch->stop(__CLASS__);
47+
$this->messages[] = [
48+
'object' => $update,
49+
'duration' => $e->getDuration(),
50+
'memory' => $e->getMemory(),
51+
];
52+
53+
return $content;
54+
}
55+
56+
public function reset()
57+
{
58+
$this->messages = [];
59+
}
60+
61+
public function count(): int
62+
{
63+
return \count($this->messages);
64+
}
65+
66+
public function getMessages(): array
67+
{
68+
return $this->messages;
69+
}
70+
71+
public function getDuration(): float
72+
{
73+
return array_sum(array_map(function ($a) {
74+
return $a['duration'];
75+
}, $this->messages));
76+
}
77+
78+
public function getMemory(): int
79+
{
80+
return (int) array_sum(array_map(function ($a) {
81+
return $a['memory'];
82+
}, $this->messages));
83+
}
84+
}

src/Publisher.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525
*
2626
* @experimental
2727
*/
28-
final class Publisher
28+
final class Publisher implements PublisherInterface
2929
{
3030
private $hubUrl;
3131
private $jwtProvider;
3232
private $httpClient;
3333

3434
/**
35-
* @param callable(): string $jwtProvider
36-
* @param HttpClientInterface|null $httpClient
35+
* @param callable(): string $jwtProvider
3736
*/
3837
public function __construct(string $hubUrl, callable $jwtProvider, HttpClientInterface $httpClient = null)
3938
{

src/PublisherInterface.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Mercure Component project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Mercure;
15+
16+
/**
17+
* @author Vincent Chalamon <[email protected]>
18+
*
19+
* @experimental
20+
*/
21+
interface PublisherInterface
22+
{
23+
public function __invoke(Update $update): string;
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Mercure Component project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Mercure\Tests\Debug;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\Mercure\Debug\TraceablePublisher;
18+
use Symfony\Component\Mercure\Publisher;
19+
use Symfony\Component\Mercure\Update;
20+
use Symfony\Component\Stopwatch\Stopwatch;
21+
22+
/**
23+
* @author Vincent Chalamon <[email protected]>
24+
*/
25+
final class TraceablePublisherTest extends TestCase
26+
{
27+
const URL = 'https://demo.mercure.rocks/hub';
28+
const JWT = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InN1YnNjcmliZSI6WyJmb28iLCJiYXIiXSwicHVibGlzaCI6WyJmb28iXX19.LRLvirgONK13JgacQ_VbcjySbVhkSmHy3IznH3tA9PM';
29+
30+
public function testPublish(): void
31+
{
32+
$publisher = new Publisher(self::URL, function (): string {
33+
return self::JWT;
34+
});
35+
$traceablePublisher = new TraceablePublisher($publisher, new Stopwatch());
36+
$update = new Update(
37+
'https://demo.mercure.rocks/demo/books/1.jsonld',
38+
'Hi from Symfony!',
39+
[],
40+
'id',
41+
null,
42+
3
43+
);
44+
$traceablePublisher($update);
45+
46+
$this->assertEquals(1, $traceablePublisher->count());
47+
$this->assertSame($update, $traceablePublisher->getMessages()[0]['object']);
48+
}
49+
}

0 commit comments

Comments
 (0)