1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[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 Silverback\ApiComponentsBundle\Mercure; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Api\IriConverterInterface; |
17
|
|
|
use ApiPlatform\Api\UrlGeneratorInterface; |
18
|
|
|
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
19
|
|
|
use ApiPlatform\Util\ResourceClassInfoTrait; |
20
|
|
|
use Silverback\ApiComponentsBundle\HttpCache\ResourceChangedPropagatorInterface; |
21
|
|
|
use Symfony\Component\Mercure\HubInterface; |
22
|
|
|
use Symfony\Component\Mercure\Update; |
23
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
24
|
|
|
use Symfony\Component\Serializer\SerializerAwareTrait; |
25
|
|
|
|
26
|
|
|
class MercureResourcePublisher implements SerializerAwareInterface, ResourceChangedPropagatorInterface |
27
|
|
|
{ |
28
|
|
|
use ResourceClassInfoTrait; |
29
|
|
|
use SerializerAwareTrait; |
30
|
|
|
|
31
|
|
|
// Do we want MessageBusInterface instead ? we don't have messenger installed yet, probably just use the default hub for now |
32
|
|
|
public function __construct( |
33
|
|
|
private readonly HubInterface $hub, |
34
|
|
|
private readonly IriConverterInterface $iriConverter, |
35
|
|
|
private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory |
36
|
|
|
){} |
37
|
|
|
|
38
|
|
|
public function collectResource($entity): void |
39
|
|
|
{ |
40
|
|
|
$this->collectItem($entity); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function collectItems($value): void |
44
|
|
|
{ |
45
|
|
|
// TODO: Implement collectItems() method. |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function collectItem($value): void |
49
|
|
|
{ |
50
|
|
|
// TODO: Implement collectItem() method. |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function propagate(): void |
54
|
|
|
{ |
55
|
|
|
// TODO: Implement propagate() method. |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// public function publishResourceUpdate(object $object): void |
59
|
|
|
// { |
60
|
|
|
// $resourceClass = $this->getObjectClass($object); |
61
|
|
|
// $operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation(); |
62
|
|
|
// $mercureOptions = $operation->getMercure() ?? []; |
63
|
|
|
// $context = $operation->getNormalizationContext() ?? []; |
64
|
|
|
// |
65
|
|
|
// $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL); |
66
|
|
|
// $data = $this->serializer->serialize($object, 'jsonld', $context); |
67
|
|
|
// |
68
|
|
|
// $this->hub->publish(new Update(data: $data, topics: [$iri], private: $mercureOptions['private'] ?? false)); |
69
|
|
|
// } |
70
|
|
|
} |
71
|
|
|
|