-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionRegistry.php
More file actions
75 lines (67 loc) · 2.27 KB
/
ExtensionRegistry.php
File metadata and controls
75 lines (67 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Arcp\Extensions;
/**
* Tracks the extensions advertised by a session (RFC §7, §21.2).
*
* Both client and runtime hold one of these per active session: the client
* learns which extensions the runtime supports; the runtime tracks what
* the client requested. Filters and dispatch consult this registry to
* decide whether to deliver, drop, or `nack` an unknown-type message
* (RFC §21.3).
*/
final class ExtensionRegistry
{
/** @var array<string, true> */
private array $advertised = [];
/**
* @param iterable<string> $extensions Initial extensions advertised
* by the local end. Each is
* validated against {@see
* ExtensionNamespace::isValidExtension()}.
*/
public function __construct(iterable $extensions = [])
{
foreach ($extensions as $ext) {
$this->advertise($ext);
}
}
public function advertise(string $extension): void
{
ExtensionNamespace::ensureValidExtension($extension);
$this->advertised[$extension] = true;
}
public function isAdvertised(string $extension): bool
{
return isset($this->advertised[$extension]);
}
/**
* Decide what to do with an inbound message of `$type` per RFC §21.3.
*
* - Core type → `core` (handler must exist or `nack UNIMPLEMENTED`).
* - Advertised extension → `advertised`.
* - Unadvertised extension, marked optional → `drop`.
* - Unadvertised extension, mandatory → `nack`.
* - Anything else (malformed namespace) → `nack`.
*
* @return 'core'|'advertised'|'drop'|'nack'
*/
public function dispositionFor(string $type, bool $optional): string
{
if (ExtensionNamespace::isCore($type)) {
return 'core';
}
if (!ExtensionNamespace::isValidExtension($type)) {
return 'nack';
}
if ($this->isAdvertised($type)) {
return 'advertised';
}
return $optional ? 'drop' : 'nack';
}
/** @return list<string> */
public function listAdvertised(): array
{
return array_keys($this->advertised);
}
}