-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPusherPlugin.php
More file actions
131 lines (117 loc) · 4.5 KB
/
PusherPlugin.php
File metadata and controls
131 lines (117 loc) · 4.5 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/*
* ZenMagick - Smart e-commerce
* Copyright (C) 2006-2012 zenmagick.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace ZenMagick\plugins\pusher;
use ZenMagick\Base\Plugins\Plugin;
use ZenMagick\Http\View\TemplateView;
use Pusher\Pusher;
/**
* Pusher plugin.
*
* @author DerManoMann <mano@zenmagick.org>
*/
class PusherPlugin extends Plugin
{
const EVENT_QUEUE_HISTORY_CACHE_KEY = 'zenmagick.plugins.pusher.EventQueueHistory';
private $pusher = null;
/**
* Inject scripts.
*/
public function onViewStart($event)
{
$view = $event->getArgument('view');
if ($view instanceof TemplateView) {
// got content, so lets see what we need to add
$resourceManager = $view->getResourceManager();
if ($this->get('activityStream')) {
$resourceManager->cssFile('css/activity-streams.css');
//$resourceManager->jsFile(sprintf('//js.pusher.com/%s/pusher.min.js', $this->get('pusherVersion')), $resourceManager::FOOTER);
$resourceManager->jsFile('js/pusher.1.11.min.js', $resourceManager::FOOTER);
$resourceManager->jsFile('js/PusherActivityStreamer.js', $resourceManager::FOOTER);
}
// also provide a event queue history to pre-populate
$cache = $this->container->get('persistentCache');
if (null === ($eventQueueHistory = $cache->lookup(self::EVENT_QUEUE_HISTORY_CACHE_KEY))) {
$eventQueueHistory = array();
}
$view->setVariable('eventQueueHistory', (array) $eventQueueHistory);
}
}
/**
* Inject streamer.
*/
public function onFinaliseContent($event)
{
$code = null;
$appKey = $this->get('appKey');
$activityStream = $this->get('activityStream');
if (!empty($activityStream)) {
$channel = trim($this->get('channel'));
$events = implode("', '", explode(',', trim($this->get('events'))));
$handler = trim($this->get('eventHandler'));
$maxItems = (int) $this->get('maxItems');
$code = <<<EOT
<script type="text/javascript">
var pusher = new Pusher('$appKey');
var channel = pusher.subscribe('$channel');
new PusherActivityStreamer(channel, document.getElementById('$activityStream'), { maxItems: $maxItems, events: ['$events'], handler: $handler });
</script>
EOT;
}
if ($code) {
$content = $event->getArgument('content');
$content = preg_replace('/<\/body>/', $code . '</body>', $content, 1);
$event->setArgument('content', $content);
}
}
/**
* Get a pusher instance.
*/
public function getPusher()
{
if (null == $this->pusher) {
$this->pusher = new Pusher($this->get('appKey'), $this->get('appSecret'), $this->get('appId'));
}
return $this->pusher;
}
/**
* Push an event.
*
* @param event The event name.
* @param mixed data The event data.
*/
public function pushEvent($event, $data)
{
$subscribed = explode(',', $this->get('events'));
// only keep if subscribed
if (in_array($event, $subscribed)) {
$cache = $this->container->get('persistentCache');
if (!$eventQueueHistory = $cache->lookup(self::EVENT_QUEUE_HISTORY_CACHE_KEY)) {
$eventQueueHistory = array();
}
array_unshift($eventQueueHistory, array('type' => $event, 'data' => $data));
$maxItems = $this->get('maxItems');
if (count($eventQueueHistory) > $maxItems) {
array_pop($eventQueueHistory);
}
$cache->save($eventQueueHistory, self::EVENT_QUEUE_HISTORY_CACHE_KEY);
}
$this->getPusher()->trigger($this->get('channel'), $event, $data);
}
}