-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathDispatcher.php
More file actions
75 lines (56 loc) · 1.69 KB
/
Dispatcher.php
File metadata and controls
75 lines (56 loc) · 1.69 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
<?php
/*!
* Dispatcher Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Dispatches events for Pattern Lab that can be listened to by plug-ins
*
*/
namespace PatternLab;
use \PatternLab\Config;
use \PatternLab\Timer;
use \Symfony\Component\EventDispatcher\EventDispatcher;
class Dispatcher {
protected static $instance;
/**
* Get instance when requested
*/
public static function getInstance() {
return self::$instance;
}
/**
* Check to see if the given pattern type has a pattern subtype associated with it
* @param {String} the name of the pattern
*
* @return {Boolean} if it was found or not
*/
public static function init() {
self::$instance = new EventDispatcher();
self::loadListeners();
}
/**
* Load listeners that may be a part of plug-ins that should be notified by the dispatcher
*/
protected static function loadListeners() {
// default var
$configDir = Config::getOption("configDir");
// make sure the listener data exists
if (file_exists($configDir."/listeners.json")) {
// get listener list data
$listenerList = json_decode(file_get_contents($configDir."/listeners.json"), true);
// get the listener info
foreach ($listenerList["listeners"] as $listenerName) {
if ($listenerName[0] != "_") {
$listener = new $listenerName();
$listeners = $listener->getListeners();
foreach ($listeners as $event => $eventProps) {
$eventPriority = (isset($eventProps["priority"])) ? $eventProps["priority"] : 0;
self::$instance->addListener($event, array($listener, $eventProps["callable"]), $eventPriority);
}
}
}
}
}
}