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
|
|
|
* {@InheritDoc} |
16
|
|
|
*/ |
17
|
|
|
public function __construct(array $values = array()) |
18
|
|
|
{ |
19
|
|
|
parent::__construct($values); |
20
|
|
|
|
21
|
|
|
$this->fixAuthorizationHeader(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Use AuthorizationHeaderFix to add Authorization header in Symfony requests. |
26
|
|
|
*/ |
27
|
|
|
private function fixAuthorizationHeader() |
28
|
|
|
{ |
29
|
|
|
$this['sandstone.listener.authorization_header_fix'] = function () { |
30
|
|
|
return new AuthorizationHeaderFixListener(); |
31
|
|
|
}; |
32
|
|
|
|
33
|
|
|
$this->on( |
34
|
|
|
KernelEvents::REQUEST, |
35
|
|
|
array( |
36
|
|
|
$this['sandstone.listener.authorization_header_fix'], |
37
|
|
|
'onKernelRequest' |
38
|
|
|
), |
39
|
|
|
10 |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add a new topic route. |
45
|
|
|
* |
46
|
|
|
* @param string $pattern |
47
|
|
|
* @param callable $factory |
48
|
|
|
* |
49
|
|
|
* @return Controller |
50
|
|
|
*/ |
51
|
|
|
public function topic($pattern, callable $factory) |
52
|
|
|
{ |
53
|
|
|
if (!$this->offsetExists('sandstone.websocket.topics')) { |
54
|
|
|
throw new \LogicException(sprintf( |
55
|
|
|
'You must register Websocket server service provider (%s) in order to use %s method.', |
56
|
|
|
WebsocketServiceProvider::class, |
57
|
|
|
__METHOD__ |
58
|
|
|
)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this['sandstone.websocket.topics']->match($pattern, $factory); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns whether Push server is registered and enabled. |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function isPushEnabled() |
70
|
|
|
{ |
71
|
|
|
return $this->offsetExists('sandstone.push') && $this['sandstone.push.enabled']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Automatically forward rest API event to push server. |
76
|
|
|
* |
77
|
|
|
* @param string $eventName |
78
|
|
|
* |
79
|
|
|
* @return self |
80
|
|
|
*/ |
81
|
|
|
public function forwardEventToPushServer($eventName) |
82
|
|
|
{ |
83
|
|
|
return $this->forwardEventsToPushServer([$eventName]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Automatically forward rest API events to push server. |
88
|
|
|
* |
89
|
|
|
* @param string[] $eventsNames |
90
|
|
|
* |
91
|
|
|
* @return self |
92
|
|
|
*/ |
93
|
|
|
public function forwardEventsToPushServer(array $eventsNames) |
94
|
|
|
{ |
95
|
|
|
if (!$this->offsetExists('sandstone.push')) { |
96
|
|
|
throw new \LogicException(sprintf( |
97
|
|
|
'You must register a Push server service provider (%s) in order to use %s method.', |
98
|
|
|
PushServiceProvider::class, |
99
|
|
|
__METHOD__ |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->before(function () use ($eventsNames) { |
104
|
|
|
$this['sandstone.push.event_forwarder']->forwardAllEvents($eventsNames); |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|