forked from pattern-lab/patternlab-php-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDispatcher.php
More file actions
61 lines (45 loc) · 1.69 KB
/
Copy pathDispatcher.php
File metadata and controls
61 lines (45 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
<?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 \Symfony\Component\EventDispatcher\EventDispatcher;
class Dispatcher {
public static $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() {
$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(Config::$options["packagesDir"]), \RecursiveIteratorIterator::CHILD_FIRST);
// make sure dots are skipped
$objects->setFlags(\FilesystemIterator::SKIP_DOTS);
foreach($objects as $name => $object) {
if ((strpos($name,"PatternLabListener.php") !== false) && (strpos($name,"plugins/vendor/") === false)) {
$dirs = explode("/",$object->getPath());
$listenerName = "\\".$dirs[count($dirs)-2]."\\".$dirs[count($dirs)-1]."\\".str_replace(".php","",$object->getFilename());
$listener = new $listenerName();
foreach ($listener->listeners as $event => $eventProps) {
$eventPriority = (isset($eventProps["priority"])) ? $eventProps["priority"] : 0;
self::$instance->addListener($event, array($listener, $eventProps["callable"]), $eventPriority);
}
}
}
}
}