|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MallardDuck\DynamicEcho\Channels; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractChannelParameters |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* A class name for the type of channel this will use. |
|
11
|
|
|
* |
|
12
|
|
|
* This should most often be defined using a `::class` reference. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
public string $channelType; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The channel's route name with bindings. |
|
20
|
|
|
* |
|
21
|
|
|
* This supports the same route binding style laravel supports in normal web routes. |
|
22
|
|
|
* So it can either be a static, but unique string; or a dynamic route with bindings. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
public string $channelAuthName; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* This needs to be either a callable to check authorization, or a Channel class name. |
|
30
|
|
|
* |
|
31
|
|
|
* Your callback should do a authorization check to see if the authenticated user is allowed to use the channel. |
|
32
|
|
|
* |
|
33
|
|
|
* @var callable|string |
|
34
|
|
|
*/ |
|
35
|
|
|
// TODO: remove string/class name option once this package supports Channel based discovery. |
|
36
|
|
|
public $channelAuthCallback; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* An array of channel options to be used. |
|
40
|
|
|
* |
|
41
|
|
|
* Most often used to adjust guard settings for a single channel. |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
public array $channelAuthOptions = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var callable|Closure|string |
|
49
|
|
|
*/ |
|
50
|
|
|
public $channelContextBindingCallback; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* This callable that will resolve the channel name bindings for the event. |
|
54
|
|
|
* |
|
55
|
|
|
* The callback will be passed the event it's related to and have access to the public properties and methods. |
|
56
|
|
|
* Using those from the event, how would the dynamic bindings be resolved to the specific requests' values. |
|
57
|
|
|
* |
|
58
|
|
|
* @var callable |
|
59
|
|
|
*/ |
|
60
|
|
|
public $eventChannelIdentifierBindingCallback; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* A representation of the $channelAuthName intended for the browser context. |
|
64
|
|
|
* |
|
65
|
|
|
* This property is automatically generated from the $channelAuthName on class construction. |
|
66
|
|
|
* This should automagically turn into the true JS look-up formula by the time it's rendered. |
|
67
|
|
|
* |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
public string $channelJsIdentifier; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var array Singleton instances array. |
|
74
|
|
|
*/ |
|
75
|
|
|
private static array $parametersInstances = []; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* AbstractChannelParameters constructor. |
|
79
|
|
|
* |
|
80
|
|
|
* @note Must be called by implementing class. |
|
81
|
|
|
*/ |
|
82
|
1 |
|
protected function __construct() |
|
83
|
|
|
{ |
|
84
|
1 |
|
$this->channelJsIdentifier = $this->getJSChannelIdentifier(); |
|
85
|
1 |
|
} |
|
86
|
|
|
|
|
87
|
4 |
|
public static function getInstance() |
|
88
|
|
|
{ |
|
89
|
4 |
|
$class = static::class; |
|
90
|
4 |
|
if (!isset(self::$parametersInstances[$class])) { |
|
91
|
1 |
|
self::$parametersInstances[$class] = new $class(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
4 |
|
return self::$parametersInstances[$class]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
public function getJSChannelIdentifier(): string |
|
98
|
|
|
{ |
|
99
|
|
|
// TODO: Maybe make this configurable? |
|
100
|
1 |
|
$baseJsVarScope = "window.dynamicEcho"; |
|
101
|
|
|
// TODO: make this easier to manage in-case channelStack stuff needs to be dynamic. |
|
102
|
1 |
|
$baseJsVar = sprintf("%s.channelStack['%s'].", $baseJsVarScope, md5($this->channelAuthName)); |
|
103
|
|
|
// Replace the Laravel braces with JS braces and variable roots. |
|
104
|
1 |
|
$jsRouteTemplate = str_replace('{', '${' . $baseJsVar, $this->channelAuthName); |
|
105
|
|
|
// Wrap the string in backticks/graves for JS template literals. |
|
106
|
1 |
|
return "`$jsRouteTemplate`"; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|