-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathmodule.php
More file actions
182 lines (159 loc) · 3.75 KB
/
module.php
File metadata and controls
182 lines (159 loc) · 3.75 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Part of the Fuel framework.
*
* @package Fuel
* @version 1.8
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2016 Fuel Development Team
* @link http://fuelphp.com
*/
namespace Fuel\Core;
/**
* This exception is thrown when a module cannot be found.
*
* @package Core
*/
class ModuleNotFoundException extends \FuelException { }
/**
* Handles all the loading, unloading and management of modules.
*
* @package Core
*/
class Module
{
/**
* @var array $modules Holds all the loaded module information.
*/
protected static $modules = array();
/**
* Loads the given module. If a path is not given, then 'module_paths' is used.
* It also accepts an array of modules as the first parameter.
*
* @param string|array $module The module name or array of modules.
* @param string|null $path The path to the module
* @return bool True on success, False on fail or already loaded.
* @throws \ModuleNotFoundException
*/
public static function load($module, $path = null)
{
if (is_array($module))
{
$result = true;
foreach ($module as $mod => $path)
{
if (is_numeric($mod))
{
$mod = $path;
$path = null;
}
$result = $result and static::load($mod, $path);
}
return $result;
}
if (static::loaded($module))
{
return false;
}
// if no path is given, try to locate the module
if ($path === null)
{
$paths = \Config::get('module_paths', array());
if ( ! empty($paths))
{
foreach ($paths as $modpath)
{
if (is_dir($path = $modpath.strtolower($module).DS))
{
break;
}
}
}
}
else
{
// make sure it's terminated properly
$path = rtrim($path, DS).DS;
}
// make sure the path exists
if ( ! is_dir($path))
{
throw new \ModuleNotFoundException("Module '$module' could not be found at '".\Fuel::clean_path($path)."'");
}
// determine the module namespace
$ns = '\\'.ucfirst($module);
// add the namespace to the autoloader
\Autoloader::add_namespaces(array(
$ns => $path.'classes'.DS,
), true);
static::$modules[$module] = $path;
return true;
}
/**
* Unloads a module from the stack.
*
* @param string $module The module name
* @return void
*/
public static function unload($module)
{
// we can only unload a loaded module
if (isset(static::$modules[$module]))
{
$path = static::$modules[$module];
if (is_file($path .= 'config/routes.php'))
{
// load and add the module routes
$module_routes = \Fuel::load($path);
$routes = \Router::parse_module_routes($module_routes, $module);
$route_names = array_keys($routes);
// delete the defined module routes
\Router::delete($route_names);
}
}
// delete this module
unset(static::$modules[$module]);
}
/**
* Checks if the given module is loaded, if no module is given then
* all loaded modules are returned.
*
* @param string|null $module The module name or null
* @return bool|array Whether the module is loaded, or all modules
*/
public static function loaded($module = null)
{
if ($module === null)
{
return static::$modules;
}
return array_key_exists($module, static::$modules);
}
/**
* Checks if the given module exists.
*
* @param string $module The module name
* @return bool|string Path to the module found, or false if not found
*/
public static function exists($module)
{
if (array_key_exists($module, static::$modules))
{
return static::$modules[$module];
}
else
{
$paths = \Config::get('module_paths', array());
$module = strtolower($module);
foreach ($paths as $path)
{
if (is_dir($path.$module))
{
return $path.$module.DS;
}
}
}
return false;
}
}