-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hippy.php
executable file
·344 lines (298 loc) · 7.84 KB
/
Hippy.php
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
include_once dirname(__FILE__).'/Hippy/exceptions.php';
include_once dirname(__FILE__).'/Hippy/driver.php';
/**
* Hippy - PHP client for HipChat. Designed for incidental notifications from an application.
*
* Example
* -------
*
* <code>
* require 'Hippy/Hippy.php';
*
* Hippy::speak('Did the build succedded');
*
* Hippy::add('Or even');
* Hippy::add('queue some messages');
* Hippy::go();
* </code>
*
* @author Rob "VivaLaCrowe" Crowe <hello@vivalacrowe.com>
* @license MIT
*/
class Hippy
{
/**
* @var Hippy
*/
protected static $instance;
/**
* @var array Hippy settings. Loads Hippy/config.php in by default
*/
protected $config = array();
/**
* @var array Instead of sending a message straight away queue it up with add() and send with go()
*/
protected $queue = array();
/**
* This method is deprecated, use instance() instead.
*
* @deprecated since 0.5
*/
public static function getInstance()
{
return static::instance();
}
/**
* Create Hippy object
*
* @param array Hippy config options
* @param bool Retrieve a clean instance of Hippy. See clean()
* @return Hippy
*/
public static function instance(array $config = array(), $clean = false)
{
if(!isset(self::$instance) OR $clean)
{
$class = __CLASS__;
static::$instance = new $class;
}
// If no config has been set, load the default
if(count(static::$instance->config) === 0)
{
$default_config = include dirname(__FILE__).'/Hippy/config.php';
static::$instance->settings($default_config);
}
// If a custom config is passed in when returning an instance
if(count($config) > 0)
{
static::$instance->settings($config);
}
return static::$instance;
}
/**
* Get a clean instance of Hippy. Shortcut for calling instance with clear flag.
*
* @param array Hippy config options
*/
public static function clean(array $config = array())
{
return static::instance($config, true);
}
/**
* Get the value of a config variable or return the current queue of messages
* if there are any.
*
* @param string Name of the config variable
* @return mixed
*/
public function __get($name)
{
if($name === 'queue')
{
return $this->queue;
}
return (isset($this->config[$name])) ? $this->config[$name] : null;
}
/**
* Deprecated as of 0.5, use $hippy->api_endpoint. Returns URL to API endpoint
*
* @deprecated since 0.5
* @return String
*/
public function endpoint()
{
return $this->api_endpoint;
}
/**
* Deprecated as of 0.5, use Hippy::instance($config). Set configuration for all Hippy messages
*
* @deprecated since 0.5
* @param Array $config Settings to set
* @return Array Settings
*/
public static function config($config = null)
{
$instance = static::instance();
//Set any settings if passed in
if(!is_null($config) AND is_array($config))
{
$instance->settings($config);
}
//Return new merged settings
return $instance->config;
}
/**
* Sets valid settings. Renames shorthand to full names to meet API requirements
*
* @param Array $config Settings to set
*/
protected function settings(array $config)
{
//Rename `token` to use correct name `auth_token`
if(isset($config['token']))
{
$this->config['auth_token'] = $config['token'];
unset($config['token']);
}
//Rename `room` to use correct name `room_id`
if(isset($config['room']))
{
$this->config['room_id'] = $config['room'];
unset($config['room']);
}
//Convert boolean notify flag to integer
if(isset($config['notify']))
{
$this->config['notify'] = (int)$config['notify'];
unset($config['notify']);
}
// Merge any other settings
$this->config = array_merge($this->config, $config);
// Load the driver
if(isset($config['driver']))
{
// Pass your own driver in at runtime
if(is_object($config['driver']))
{
$this->config['driver'] = $config['driver'];
$this->config['driver']->init($this->config);
}
else
{
$driver = $config['driver'];
// Make sure the driver exists
if(!file_exists($path = dirname(__FILE__).'/Hippy/driver/'.strtolower($driver).'.php'))
{
throw new HippyUnknownDriverException('Can not find driver: '.$path);
}
include_once $path;
$class = 'Hippy_'.ucfirst($driver);
$this->config['driver'] = new $class;
$this->config['driver']->init($this->config);
}
}
}
/**
* Checks that neccessery settings are set before attempting to send a new message
*
* @deprecated since 0.5 - Use Hippy_Driver::valid_settings() to check this.
* @throws HippyException
*/
public function validSettings()
{
$required = array(
array(
'setting' => 'auth_token',
'map' => 'token',
),
array(
'setting' => 'room_id',
'map' => 'room',
),
array(
'setting' => 'from',
'map' => 'from',
),
array(
'setting' => 'api_endpoint',
'map' => 'api_endpoint',
),
);
foreach($required as $setting)
{
if($this->$setting['setting'] === null OR strlen(trim($this->$setting['setting'])) === 0)
{
//Setting not set, throw exception
throw new HippyMissingSettingException('Missing setting: '.$setting['map']);
}
}
return true;
}
/**
* Send a single message to your Hipchat room.
*
* @param string Message
* @param array Runtime config options
* @throws HippyMissingSettingException
* @throws HippyResponseException
* @throws HippyNotSentException
*/
public static function speak($msg, array $config = array())
{
$instance = static::instance();
// Set any runtime settings
$instance->settings($config);
// Initalize driver with latest config and send message
return $instance->driver->init($instance->config)->send($msg);
}
/**
* Add a message to the queue. Message will be bundled as one message with line breaks, then sent with Hippy::go().
*
* @param string $msg Message you want to add the queue and send.
*/
public static function add($msg)
{
$instance = static::instance();
$instance->queue[] = $msg; //Add message to queue
}
/**
* Joins all the messages in the queue together with a line break and sends it.
*
* @param bool Whether to join the queue of messages and send as one message, or seperate messages. Default TRUE.
* @throws HippyEmptyQueueException
*/
public static function go($join = true)
{
$instance = static::instance();
$driver = $instance->driver;
if(count($instance->queue) === 0)
{
throw new HippyEmptyQueueException('Can not send queue. Queue is empty!');
}
if($join OR count($instance->queue) === 1)
{
// Sending as one big message
$msg = implode('<br />', $instance->queue);
$response = $instance->driver->send($msg);
// Clear the queue, an exception would have been called
// if there was an error sending
static::clear_queue();
// Return details about the sent message
// Mainly used for testing
return $response;
}
// Hold the individual responses for each msg sent
$responses = array();
foreach($instance->queue as $msg)
{
$responses[] = $instance->driver->send($msg);
}
// Clear the queue, an exception would have been called
// if there was an error sending
static::clear_queue();
// Return details about the sent messages
// Mainly used for testing
return $responses;
}
/**
* Force the message queue empty
*/
public static function clear_queue()
{
static::instance()->queue = array();
}
/**
* Send any messages that are in the queue. This is the same as Hippy::go apart from no exception is generated.
*
* @param bool Whether to join the queue of messages and send as one message, or seperate messages. Default TRUE.
*/
public static function flush_queue($join = true)
{
$instance = static::instance();
if(count($instance->queue) > 0)
{
return static::go($join);
}
}
}