1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Twig; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\DatabaseNotificationManager; |
6
|
|
|
use IrishDan\NotificationBundle\Notification\NotifiableInterface; |
7
|
|
|
use IrishDan\NotificationBundle\PusherManager; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class NotificationExtension |
12
|
|
|
* |
13
|
|
|
* @package IrishDan\NotificationBundle\Twig |
14
|
|
|
*/ |
15
|
|
|
class NotificationExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface |
16
|
|
|
{ |
17
|
|
|
protected $pusherManager; |
18
|
|
|
protected $router; |
19
|
|
|
protected $databaseNotificationManager; |
20
|
|
|
protected $availableChannels = []; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
PusherManager $pusherManager, |
24
|
|
|
Router $router, |
25
|
|
|
DatabaseNotificationManager $databaseNotificationManager, |
26
|
|
|
array $availableChannels |
27
|
|
|
) { |
28
|
|
|
$this->pusherManager = $pusherManager; |
29
|
|
|
$this->router = $router; |
30
|
|
|
$this->databaseNotificationManager = $databaseNotificationManager; |
31
|
|
|
$this->availableChannels = $availableChannels; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getFunctions() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
// Database notifications. |
38
|
|
|
new \Twig_SimpleFunction('notification_unread_count', [$this, 'getUserUnreadCount']), |
39
|
|
|
new \Twig_SimpleFunction('notification_read_count', [$this, 'getUserReadCount']), |
40
|
|
|
new \Twig_SimpleFunction('notification_all_count', [$this, 'getUserAllCount']), |
41
|
|
|
// Pusher twig functions. |
42
|
|
|
// Mainly for generating the javascript for each user. |
43
|
|
|
new \Twig_SimpleFunction('notification_pusher_channel_name', |
44
|
|
|
[$this, 'getUserChannelName']), |
45
|
|
|
new \Twig_SimpleFunction('notification_new_pusher_js', [$this, 'createNewPusherJS'], |
46
|
|
|
['is_safe' => ['html']]), |
47
|
|
|
new \Twig_SimpleFunction('notification_new_pusher_channel_js', |
48
|
|
|
[$this, 'createNewPusherChannelJS'], |
49
|
|
|
['is_safe' => ['html']]), |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getGlobals() |
54
|
|
|
{ |
55
|
|
|
if ($this->channelEnabled()) { |
56
|
|
|
return [ |
57
|
|
|
'notification_pusher_auth_endpoint' => $this->getAuthEndpoint(), |
58
|
|
|
'notification_pusher_auth_key' => $this->pusherManager->getAuthKey(), |
59
|
|
|
'notification_pusher_app_id' => $this->pusherManager->getAppId(), |
60
|
|
|
'notification_pusher_event' => $this->pusherManager->getEvent(), |
61
|
|
|
'notification_pusher_cluster' => $this->pusherManager->getCluster(), |
62
|
|
|
'notification_pusher_encrypted' => $this->pusherManager->getEncrypted(), |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function channelEnabled($channel = 'pusher') |
70
|
|
|
{ |
71
|
|
|
return in_array($channel, $this->availableChannels); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getUserAllCount(NotifiableInterface $user) |
75
|
|
|
{ |
76
|
|
|
if ($this->channelEnabled('database')) { |
77
|
|
|
return $this->databaseNotificationManager->getUsersNotificationCount($user, ''); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getUserUnreadCount(NotifiableInterface $user) |
84
|
|
|
{ |
85
|
|
|
if ($this->channelEnabled('database')) { |
86
|
|
|
return $this->databaseNotificationManager->getUsersNotificationCount($user, 'unread'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return ''; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getUserReadCount(NotifiableInterface $user) |
93
|
|
|
{ |
94
|
|
|
if ($this->channelEnabled('database')) { |
95
|
|
|
return $this->databaseNotificationManager->getUsersNotificationCount($user, 'read'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return ''; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getUserChannelName(NotifiableInterface $user) |
102
|
|
|
{ |
103
|
|
|
if ($this->channelEnabled()) { |
104
|
|
|
if ($user) { |
105
|
|
|
return $this->pusherManager->getUserChannelName($user); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function createNewPusherChannelJS(NotifiableInterface $user) |
113
|
|
|
{ |
114
|
|
|
if ($this->channelEnabled()) { |
115
|
|
|
$channelName = $this->getUserChannelName($user); |
116
|
|
|
|
117
|
|
|
return "var channel = pusher.subscribe('" . $channelName . "');"; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return ''; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function createNewPusherJS() |
124
|
|
|
{ |
125
|
|
|
if ($this->channelEnabled()) { |
126
|
|
|
return 'var pusher = new Pusher("' . $this->pusherManager->getAuthKey() . '", { |
127
|
|
|
authEndpoint: "' . $this->getAuthEndpoint() . '", |
128
|
|
|
cluster: "' . $this->pusherManager->getCluster() . '", |
129
|
|
|
encrypted: ' . $this->pusherManager->getEncrypted() . ' |
130
|
|
|
});'; |
131
|
|
|
} else { |
132
|
|
|
return 'console.log("Pusher channel is not enabled")'; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getAuthEndpoint() |
137
|
|
|
{ |
138
|
|
|
if ($this->channelEnabled()) { |
139
|
|
|
$exists = $this->router->getRouteCollection()->get('notification.pusher_auth'); |
140
|
|
|
if (null !== $exists) { |
141
|
|
|
return $this->router->generate('notification.pusher_auth'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return ''; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getName() |
149
|
|
|
{ |
150
|
|
|
return 'notification_extension'; |
151
|
|
|
} |
152
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: