-
Notifications
You must be signed in to change notification settings - Fork 4
/
library.php
executable file
·97 lines (75 loc) · 1.83 KB
/
library.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
<?php
class LibraryNotFoundException extends Exception {}
/*
Class:
Library
Keeps track and loads libraries.
*/
class Library
{
// Property: <Library::$path>
// The directory in which library files are contained.
private static $path = 'library/';
// Property: Library::$app_path
// Path to app-wide libraries
private static $app_path = 'app/library/';
/*
Method:
Library::load
Loads all libraries. If they are not found in <Library::$path> a <LibraryNotFoundException> is thrown.
Parameters:
string $library1 - The name of the library to load
string $libraryN - ...
*/
public static function load()
{
foreach ( func_get_args() as $library )
{
$lib = explode("/", $library);
$path = self::$path;
if ( $lib[0] == "app" && count($lib) > 1 )
{
$library = $lib[1];
$path = self::$app_path;
}
$name = $path . strtolower($library) . '.php';
if ( ! file_exists($name) )
throw new LibraryNotFoundException($library);
// Already exists?
if ( class_exists($library) )
continue;
require($name);
}
}
/*
Method:
Library::loadInstance
Load $library and return an instance of that class.
Parameters:
string $library - The name of the library to load.
mixed $arg1 - Argument to be passed to the constructor of the new instance.
mixed $argN - ...
*/
public static function loadInstance($library)
{
self::load($library);
// Get constructor arguments
$args = func_get_args();
$args = array_slice($args, 1);
return call_user_func_array(
array(new ReflectionClass($library), 'newInstance'),
$args
);
}
/*
Method:
Library::setPath
Set the path in which libraries are loaded from.
Parameters:
string $path - The path to the libraries.
*/
public static function setPath($path)
{
self::$path = $path;
}
}