|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Eole\Sandstone; |
|
4
|
|
|
|
|
5
|
|
|
use Alcalyn\AuthorizationHeaderFix\AuthorizationHeaderFixListener; |
|
6
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
7
|
|
|
use Silex\Controller; |
|
8
|
|
|
use Silex\Application as BaseApplication; |
|
9
|
|
|
use Eole\Sandstone\Websocket\ServiceProvider as WebsocketServiceProvider; |
|
10
|
|
|
use Eole\Sandstone\Push\ServiceProvider as PushServiceProvider; |
|
11
|
|
|
|
|
12
|
|
|
class Application extends BaseApplication |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Events lists marked as to forward before application boot. |
|
16
|
|
|
* Needs to be forwarded once application booted. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string[][] |
|
19
|
|
|
*/ |
|
20
|
|
|
private $events; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@InheritDoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(array $values = array()) |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct($values); |
|
28
|
|
|
|
|
29
|
|
|
$this->events = []; |
|
30
|
|
|
|
|
31
|
|
|
$this->fixAuthorizationHeader(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Use AuthorizationHeaderFix to add Authorization header in Symfony requests. |
|
36
|
|
|
*/ |
|
37
|
|
|
private function fixAuthorizationHeader() |
|
38
|
|
|
{ |
|
39
|
|
|
$this['sandstone.listener.authorization_header_fix'] = function () { |
|
40
|
|
|
return new AuthorizationHeaderFixListener(); |
|
41
|
|
|
}; |
|
42
|
|
|
|
|
43
|
|
|
$this->on( |
|
44
|
|
|
KernelEvents::REQUEST, |
|
45
|
|
|
array( |
|
46
|
|
|
$this['sandstone.listener.authorization_header_fix'], |
|
47
|
|
|
'onKernelRequest' |
|
48
|
|
|
), |
|
49
|
|
|
10 |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Add a new topic route. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $pattern |
|
57
|
|
|
* @param callable $factory |
|
58
|
|
|
* |
|
59
|
|
|
* @return Controller |
|
60
|
|
|
*/ |
|
61
|
|
|
public function topic($pattern, callable $factory) |
|
62
|
|
|
{ |
|
63
|
|
|
if (!$this->offsetExists('sandstone.websocket.topics')) { |
|
64
|
|
|
throw new \LogicException(sprintf( |
|
65
|
|
|
'You must register Websocket server service provider (%s) in order to use %s method.', |
|
66
|
|
|
WebsocketServiceProvider::class, |
|
67
|
|
|
__METHOD__ |
|
68
|
|
|
)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this['sandstone.websocket.topics']->match($pattern, $factory); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns whether Push server is registered and enabled. |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
public function isPushEnabled() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->offsetExists('sandstone.push') && $this['sandstone.push.enabled']; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Automatically forward rest API event to push server. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $eventName |
|
88
|
|
|
* |
|
89
|
|
|
* @return self |
|
90
|
|
|
*/ |
|
91
|
|
|
public function forwardEventToPushServer($eventName) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->forwardEventsToPushServer([$eventName]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Automatically forward rest API events to push server. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string[] $eventsNames |
|
100
|
|
|
* |
|
101
|
|
|
* @return self |
|
102
|
|
|
*/ |
|
103
|
|
|
public function forwardEventsToPushServer(array $eventsNames) |
|
104
|
|
|
{ |
|
105
|
|
|
if (!$this->offsetExists('sandstone.push')) { |
|
106
|
|
|
throw new \LogicException(sprintf( |
|
107
|
|
|
'You must register a Push server service provider (%s) in order to use %s method.', |
|
108
|
|
|
PushServiceProvider::class, |
|
109
|
|
|
__METHOD__ |
|
110
|
|
|
)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($this->booted) { |
|
114
|
|
|
$this['sandstone.push.event_forwarder']->forwardAllEvents($eventsNames); |
|
115
|
|
|
} else { |
|
116
|
|
|
$this->events []= $eventsNames; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@InheritDoc} |
|
124
|
|
|
* |
|
125
|
|
|
* Forward events marked as to be forwarded before application boot. |
|
126
|
|
|
* |
|
127
|
|
|
* Allow to use forwardEventsToPushServer in register |
|
128
|
|
|
* instead of forcing user to forward event only at boot. |
|
129
|
|
|
*/ |
|
130
|
|
|
public function boot() |
|
131
|
|
|
{ |
|
132
|
|
|
if ($this->booted) { |
|
133
|
|
|
return; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
parent::boot(); |
|
137
|
|
|
|
|
138
|
|
|
foreach ($this->events as $eventsNames) { |
|
139
|
|
|
$this->forwardEventsToPushServer($eventsNames); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$this->events = []; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|