Skip to content

Commit de14349

Browse files
committed
minor #40671 [Notifier] Mercure bridge: bump mercure dependency to 0.5 (azjezz, mtarld)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Notifier] Mercure bridge: bump mercure dependency to 0.5 | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | symfony/symfony-docs#15179 Bump Mercure bridge's `symfony/mercure` dependency to 0.5 to deal with hubs instead of publishers. --- To be able to use `HandlerRegistry::all` method, this PR needs symfony/mercure#50 to be merged and released. Commits ------- 498f96f Drop support of mercure:^0.4 d3306fd add support for symfony/mercure:^0.5
2 parents 3ca3de5 + 498f96f commit de14349

File tree

8 files changed

+110
-154
lines changed

8 files changed

+110
-154
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
"psr/http-client": "^1.0",
143143
"psr/simple-cache": "^1.0",
144144
"egulias/email-validator": "^2.1.10|^3.1",
145-
"symfony/mercure-bundle": "^0.2",
145+
"symfony/mercure-bundle": "^0.3",
146146
"symfony/phpunit-bridge": "^5.2",
147147
"symfony/security-acl": "~2.8|~3.0",
148148
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0",

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
2727
use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface;
2828
use Symfony\Bundle\FullStack;
29+
use Symfony\Bundle\MercureBundle\MercureBundle;
2930
use Symfony\Component\Asset\PackageInterface;
3031
use Symfony\Component\BrowserKit\AbstractBrowser;
3132
use Symfony\Component\Cache\Adapter\AdapterInterface;
@@ -44,8 +45,6 @@
4445
use Symfony\Component\Console\Command\Command;
4546
use Symfony\Component\DependencyInjection\Alias;
4647
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
47-
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
48-
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
4948
use Symfony\Component\DependencyInjection\ChildDefinition;
5049
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
5150
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -95,6 +94,7 @@
9594
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
9695
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
9796
use Symfony\Component\Mailer\Mailer;
97+
use Symfony\Component\Mercure\HubRegistry;
9898
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory;
9999
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpTransportFactory;
100100
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdTransportFactory;
@@ -2408,7 +2408,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24082408

24092409
if (ContainerBuilder::willBeAvailable('symfony/mercure-notifier', MercureTransportFactory::class, $parentPackages) && ContainerBuilder::willBeAvailable('symfony/mercure-bundle', MercureBundle::class, $parentPackages)) {
24102410
$container->getDefinition($classToServices[MercureTransportFactory::class])
2411-
->replaceArgument('$publisherLocator', new ServiceLocatorArgument(new TaggedIteratorArgument('mercure.publisher', null, null, true)));
2411+
->replaceArgument('$registry', new Reference(HubRegistry::class));
24122412
} elseif (ContainerBuilder::willBeAvailable('symfony/mercure-notifier', MercureTransportFactory::class, $parentPackages)) {
24132413
$container->removeDefinition($classToServices[MercureTransportFactory::class]);
24142414
}

src/Symfony/Component/Notifier/Bridge/Mercure/MercureTransport.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,48 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Mercure;
1313

14-
use Symfony\Component\Mercure\PublisherInterface;
14+
use Symfony\Component\Mercure\Exception\InvalidArgumentException;
15+
use Symfony\Component\Mercure\Exception\RuntimeException as MercureRuntimeException;
16+
use Symfony\Component\Mercure\HubInterface;
1517
use Symfony\Component\Mercure\Update;
16-
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1718
use Symfony\Component\Notifier\Exception\LogicException;
1819
use Symfony\Component\Notifier\Exception\RuntimeException;
19-
use Symfony\Component\Notifier\Exception\TransportException;
2020
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
2121
use Symfony\Component\Notifier\Message\ChatMessage;
2222
use Symfony\Component\Notifier\Message\MessageInterface;
2323
use Symfony\Component\Notifier\Message\SentMessage;
2424
use Symfony\Component\Notifier\Transport\AbstractTransport;
2525
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
26-
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
27-
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
2826
use Symfony\Contracts\HttpClient\HttpClientInterface;
2927

3028
/**
3129
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
3230
*/
3331
final class MercureTransport extends AbstractTransport
3432
{
35-
private $publisher;
36-
private $publisherId;
33+
private $hub;
34+
private $hubId;
3735
private $topics;
3836

3937
/**
4038
* @param string|string[]|null $topics
4139
*/
42-
public function __construct(PublisherInterface $publisher, string $publisherId, $topics = null, ?HttpClientInterface $client = null, ?EventDispatcherInterface $dispatcher = null)
40+
public function __construct(HubInterface $hub, string $hubId, $topics = null, ?HttpClientInterface $client = null, ?EventDispatcherInterface $dispatcher = null)
4341
{
4442
if (null !== $topics && !\is_array($topics) && !\is_string($topics)) {
4543
throw new \TypeError(sprintf('"%s()" expects parameter 3 to be an array of strings, a string or null, "%s" given.', __METHOD__, get_debug_type($topics)));
4644
}
4745

48-
$this->publisher = $publisher;
49-
$this->publisherId = $publisherId;
46+
$this->hub = $hub;
47+
$this->hubId = $hubId;
5048
$this->topics = $topics ?? 'https://symfony.com/notifier';
5149

5250
parent::__construct($client, $dispatcher);
5351
}
5452

5553
public function __toString(): string
5654
{
57-
return sprintf('mercure://%s?%s', $this->publisherId, http_build_query(['topic' => $this->topics]));
55+
return sprintf('mercure://%s?%s', $this->hubId, http_build_query(['topic' => $this->topics]));
5856
}
5957

6058
public function supports(MessageInterface $message): bool
@@ -87,18 +85,14 @@ protected function doSend(MessageInterface $message): SentMessage
8785
]), $options->isPrivate(), $options->getId(), $options->getType(), $options->getRetry());
8886

8987
try {
90-
$messageId = ($this->publisher)($update);
88+
$messageId = $this->hub->publish($update);
9189

9290
$sentMessage = new SentMessage($message, (string) $this);
9391
$sentMessage->setMessageId($messageId);
9492

9593
return $sentMessage;
96-
} catch (HttpExceptionInterface $e) {
97-
throw new TransportException('Unable to post the Mercure message: '.$e->getResponse()->getContent(false), $e->getResponse(), $e->getCode(), $e);
98-
} catch (ExceptionInterface $e) {
94+
} catch (MercureRuntimeException | InvalidArgumentException $e) {
9995
throw new RuntimeException('Unable to post the Mercure message: '.$e->getMessage(), $e->getCode(), $e);
100-
} catch (\InvalidArgumentException $e) {
101-
throw new InvalidArgumentException('Unable to post the Mercure message: '.$e->getMessage(), $e->getCode(), $e);
10296
}
10397
}
10498
}

src/Symfony/Component/Notifier/Bridge/Mercure/MercureTransportFactory.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,26 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Mercure;
1313

14-
use Symfony\Bundle\MercureBundle\MercureBundle;
15-
use Symfony\Component\Mercure\PublisherInterface;
16-
use Symfony\Component\Notifier\Exception\LogicException;
14+
use Symfony\Component\Mercure\Exception\InvalidArgumentException;
15+
use Symfony\Component\Mercure\HubRegistry;
16+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
1717
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
1818
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
1919
use Symfony\Component\Notifier\Transport\Dsn;
2020
use Symfony\Component\Notifier\Transport\TransportInterface;
21-
use Symfony\Contracts\Service\ServiceProviderInterface;
2221

2322
/**
2423
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
2524
*/
2625
final class MercureTransportFactory extends AbstractTransportFactory
2726
{
28-
private $publisherLocator;
27+
private $registry;
2928

30-
/**
31-
* @param ServiceProviderInterface $publisherLocator A container that holds {@see PublisherInterface} instances
32-
*/
33-
public function __construct(ServiceProviderInterface $publisherLocator)
29+
public function __construct(HubRegistry $registry)
3430
{
3531
parent::__construct();
3632

37-
$this->publisherLocator = $publisherLocator;
33+
$this->registry = $registry;
3834
}
3935

4036
/**
@@ -46,18 +42,16 @@ public function create(Dsn $dsn): TransportInterface
4642
throw new UnsupportedSchemeException($dsn, 'mercure', $this->getSupportedSchemes());
4743
}
4844

49-
$publisherId = $dsn->getHost();
50-
if (!$this->publisherLocator->has($publisherId)) {
51-
if (!class_exists(MercureBundle::class) && !$this->publisherLocator->getProvidedServices()) {
52-
throw new LogicException('No publishers found. Did you forget to install the MercureBundle? Try running "composer require symfony/mercure-bundle".');
53-
}
45+
$hubId = $dsn->getHost();
46+
$topic = $dsn->getOption('topic');
5447

55-
throw new LogicException(sprintf('"%s" not found. Did you mean one of: %s?', $publisherId, implode(', ', array_keys($this->publisherLocator->getProvidedServices()))));
48+
try {
49+
$hub = $this->registry->getHub($hubId);
50+
} catch (InvalidArgumentException $exception) {
51+
throw new IncompleteDsnException(sprintf('Hub "%s" not found. Did you mean one of: "%s"?', $hubId, implode('", "', array_keys($this->registry->all()))));
5652
}
5753

58-
$topic = $dsn->getOption('topic');
59-
60-
return new MercureTransport($this->publisherLocator->get($publisherId), $publisherId, $topic);
54+
return new MercureTransport($hub, $hubId, $topic);
6155
}
6256

6357
protected function getSupportedSchemes(): array

src/Symfony/Component/Notifier/Bridge/Mercure/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ DSN example
77
-----------
88

99
```
10-
MERCURE_DSN=mercure://PUBLISHER_SERVICE_ID?topic=TOPIC
10+
MERCURE_DSN=mercure://HUB_ID?topic=TOPIC
1111
```
1212

1313
where:
14-
- `PUBLISHER_SERVICE_ID` is the Mercure publisher service id
14+
- `HUB_ID` is the Mercure hub id
1515
- `TOPIC` is the topic IRI (optional, default: `https://symfony.com/notifier`. Could be either a single topic: `topic=https://foo` or multiple topics: `topic[]=/foo/1&topic[]=https://bar`)
1616

1717
Resources

src/Symfony/Component/Notifier/Bridge/Mercure/Tests/MercureTransportFactoryTest.php

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Mercure\Tests;
1313

14-
use LogicException;
15-
use Symfony\Bridge\PhpUnit\ClassExistsMock;
16-
use Symfony\Bundle\MercureBundle\MercureBundle;
17-
use Symfony\Component\Mercure\PublisherInterface;
14+
use Symfony\Component\Mercure\HubInterface;
15+
use Symfony\Component\Mercure\HubRegistry;
1816
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
17+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
1918
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
2019
use Symfony\Component\Notifier\Transport\Dsn;
2120
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
22-
use Symfony\Contracts\Service\ServiceProviderInterface;
2321

2422
/**
2523
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
@@ -28,73 +26,49 @@ final class MercureTransportFactoryTest extends TransportFactoryTestCase
2826
{
2927
public function createFactory(): TransportFactoryInterface
3028
{
31-
$publisherLocator = $this->createMock(ServiceProviderInterface::class);
32-
$publisherLocator->method('has')->willReturn(true);
33-
$publisherLocator->method('get')->willReturn($this->createMock(PublisherInterface::class));
29+
$hub = $this->createMock(HubInterface::class);
30+
$hubRegistry = new HubRegistry($hub, ['hubId' => $hub]);
3431

35-
return new MercureTransportFactory($publisherLocator);
32+
return new MercureTransportFactory($hubRegistry);
3633
}
3734

3835
public function supportsProvider(): iterable
3936
{
40-
yield [true, 'mercure://publisherId?topic=topic'];
41-
yield [false, 'somethingElse://publisherId?topic=topic'];
37+
yield [true, 'mercure://hubId?topic=topic'];
38+
yield [false, 'somethingElse://hubId?topic=topic'];
4239
}
4340

4441
public function createProvider(): iterable
4542
{
4643
yield [
47-
'mercure://publisherId?topic=%2Ftopic%2F1',
48-
'mercure://publisherId?topic=/topic/1',
44+
'mercure://hubId?topic=%2Ftopic%2F1',
45+
'mercure://hubId?topic=/topic/1',
4946
];
5047

5148
yield [
52-
'mercure://publisherId?topic%5B0%5D=%2Ftopic%2F1&topic%5B1%5D=%2Ftopic%2F2',
53-
'mercure://publisherId?topic[]=/topic/1&topic[]=/topic/2',
49+
'mercure://hubId?topic%5B0%5D=%2Ftopic%2F1&topic%5B1%5D=%2Ftopic%2F2',
50+
'mercure://hubId?topic[]=/topic/1&topic[]=/topic/2',
5451
];
5552

5653
yield [
57-
'mercure://publisherId?topic=https%3A%2F%2Fsymfony.com%2Fnotifier',
58-
'mercure://publisherId',
54+
'mercure://hubId?topic=https%3A%2F%2Fsymfony.com%2Fnotifier',
55+
'mercure://hubId',
5956
];
6057
}
6158

6259
public function unsupportedSchemeProvider(): iterable
6360
{
64-
yield ['somethingElse://publisherId?topic=topic'];
61+
yield ['somethingElse://hubId?topic=topic'];
6562
}
6663

67-
public function testCreateWithEmptyServiceProviderAndWithoutMercureBundleThrows()
64+
public function testNotFoundHubThrows()
6865
{
69-
ClassExistsMock::register(MercureTransportFactory::class);
70-
ClassExistsMock::withMockedClasses([MercureBundle::class => false]);
66+
$hub = $this->createMock(HubInterface::class);
67+
$hubRegistry = new HubRegistry($hub, ['hubId' => $hub, 'anotherHubId' => $hub]);
68+
$factory = new MercureTransportFactory($hubRegistry);
7169

72-
$publisherLocator = $this->createMock(ServiceProviderInterface::class);
73-
$publisherLocator->method('has')->willReturn(false);
74-
$publisherLocator->method('getProvidedServices')->willReturn([]);
75-
76-
$factory = new MercureTransportFactory($publisherLocator);
77-
78-
$this->expectException(LogicException::class);
79-
$this->expectExceptionMessage('No publishers found. Did you forget to install the MercureBundle? Try running "composer require symfony/mercure-bundle".');
80-
81-
try {
82-
$factory->create(new Dsn('mercure://publisherId'));
83-
} finally {
84-
ClassExistsMock::withMockedClasses([MercureBundle::class => true]);
85-
}
86-
}
87-
88-
public function testNotFoundPublisherThrows()
89-
{
90-
$publisherLocator = $this->createMock(ServiceProviderInterface::class);
91-
$publisherLocator->method('has')->willReturn(false);
92-
$publisherLocator->method('getProvidedServices')->willReturn(['fooPublisher' => 'fooFqcn', 'barPublisher' => 'barFqcn']);
93-
94-
$factory = new MercureTransportFactory($publisherLocator);
95-
96-
$this->expectException(LogicException::class);
97-
$this->expectExceptionMessage('"publisherId" not found. Did you mean one of: fooPublisher, barPublisher?');
98-
$factory->create(new Dsn('mercure://publisherId'));
70+
$this->expectException(IncompleteDsnException::class);
71+
$this->expectExceptionMessage('Hub "wrongHubId" not found. Did you mean one of: "hubId", "anotherHubId"?');
72+
$factory->create(new Dsn('mercure://wrongHubId'));
9973
}
10074
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy