-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleHandler.php
84 lines (64 loc) · 1.68 KB
/
ModuleHandler.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
<?php
class ModuleHandler
{
private static $loadedModules = null;
private static $files = null;
public static function init()
{
self::$loadedModules = array();
self::$files = array();
}
public static function getRoot() {
return __DIR__;
}
public static function loadModule($path)
{
$path = __DIR__.$path;
if(!is_dir($path))
throw new Exception("There is no module at path $path");
if(is_file($path.'/lib/lib.php'))
require($path.'/lib/lib.php');
$moduleFiles = array();
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $file)
{
if(endsWith($file, '.')) continue;
$moduleFiles[] = str_replace('\\', '/', $file->getPathname());
}
sort($moduleFiles);
foreach($moduleFiles as $file)
{
$logicalFile = substr($file, strlen($path));
if(!isset(self::$files[$logicalFile]))
self::$files[$logicalFile] = array();
self::$files[$logicalFile][] = $file;
}
}
public static function getFiles($file)
{
return self::$files[$file];
}
public static function getFile($file)
{
return self::$files[$file][count(self::$files[$file]) - 1];
}
public static function getFilesMatching($pattern)
{
$pattern = preg_quote($pattern);
$pattern = str_replace('\*\*', '.*', $pattern);
$pattern = str_replace('\*', '[^/]*', $pattern);
$pattern = '#^'.$pattern.'$#';
$res = array();
foreach(self::$files as $file => $files)
if(preg_match($pattern, $file))
foreach($files as $entry)
$res[] = $entry;
return $res;
}
public static function toWebPath($file)
{
if(is_array($file))
return array_map(array('ModuleHandler', 'toWebPath'), $file);
else
return substr($file, strlen(__DIR__)+1);
}
}