forked from MarcWeber/phamlp
-
Notifications
You must be signed in to change notification settings - Fork 83
/
SassLoader.php
86 lines (78 loc) · 2.29 KB
/
SassLoader.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
<?php
/**
* SASS Auto loader
*
* @author Tim Lochmüller <tim@fruit-lab.de>
*/
/**
* SASS Auto loader
*
* @author Tim Lochmüller <tim@fruit-lab.de>
*/
class SassLoader {
/**
* Base dir of the SASS Framework
*
* @var string
*/
static private $basePath = NULL;
/**
* Loader file extension
*/
const FILE_EXTENSION = '.php';
/**
* Load the given class
*
* @param string $className
*
* @todo More smarter Implementation, if the files have a smarter structure
*/
static public function load($className) {
$base = self::getBasePath();
$psrFile = self::getPsrFilePath($className);
if (file_exists($base . $psrFile)) {
require_once($base . $psrFile);
} else if (file_exists($base . $className . self::FILE_EXTENSION)) {
require_once($base . $className . self::FILE_EXTENSION);
} else if (file_exists($base . 'tree/' . $className . self::FILE_EXTENSION)) {
require_once($base . 'tree/' . $className . self::FILE_EXTENSION);
} else if (file_exists($base . 'renderers/' . $className . self::FILE_EXTENSION)) {
require_once($base . 'renderers/' . $className . self::FILE_EXTENSION);
} else if (file_exists($base . 'script/' . $className . self::FILE_EXTENSION)) {
require_once($base . 'script/' . $className . self::FILE_EXTENSION);
} else if (file_exists($base . 'script/literals/' . $className . self::FILE_EXTENSION)) {
require_once($base . 'script/literals/' . $className . self::FILE_EXTENSION);
} else {
}
}
/**
* Build the filename for a PSR conform className
*
* @param string $className
*
* @return string
*/
static private function getPsrFilePath($className) {
$className = ltrim($className, '\\');
$fileName = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . self::FILE_EXTENSION;
return $fileName;
}
/**
* Get the current base path
*
* @return string
*/
static private function getBasePath() {
if (self::$basePath === NULL) {
self::$basePath = dirname(__FILE__) . '/';
}
return self::$basePath;
}
}
spl_autoload_register('SassLoader::load');