-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
130 lines (109 loc) · 3.55 KB
/
Module.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
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
<?PHP
class Module_Core{
//Array of registered modules
protected static $modules=array();
public static $loaded_module;
//Module name
public $module_name;
//Module path
public $module_path;
//Load all registered modules into include_paths (or not)
public static $load_registered_modules=false;
//Hook for the system.post_routing event
public static function hook()
{
if(Router::$controller==null AND self::is_registered(Router::$rsegments[0]))
{
self::$loaded_module=self::get(Router::$rsegments[0]);
self::$loaded_module->route();
}
Event::run('module.post_loading');
$paths=array();
if(self::$load_registered_modules==true)
{
foreach(self::$modules as $module)
{
$paths[]=$module->module_path;
}
}
else
{
$paths[]=self::$loaded_module->module_path;
}
//Load triggered (or all) module into include paths so views, models and libraries can be loaded the normal way.
Config::set('core.modules',array_merge(Config::item('core.modules'),$paths));
}
//Register new modules
public static function register($name,$path)
{
self::$modules[$name]=new Module($name,$path);
}
//Register all directories in a directory as modules
public static function register_dir($path)
{
$items=(Kohana::list_files($path));
foreach($items as $item)
{
if(is_dir($item))
{
$pos=strlen($item)-strrpos($item,'/')-1;
$name= substr($item,-$pos );
self::register($name,$item);
}
}
}
//Check whether module is registered
public static function is_registered($name)
{
return array_key_exists($name,self::$modules);
}
//get instance of module
public static function get($name)
{
return self::$modules[$name];
}
protected function __construct($name,$path)
{
$path=rtrim($path,'/');
$this->module_name=$name;
$this->module_path=$path;
}
//Routing method to find the controller in the module directory
public function route()
{
Router::$directory = $this->module_path.'/controllers/';
Router::$controller = isset(Router::$rsegments[1]) ? Router::$rsegments[1] : Config::item('routes._default');
Router::$method = isset(Router::$rsegments[2]) ? Router::$rsegments[2] : 'index';
Router::$arguments = isset(Router::$rsegments[3]) ? array_slice(Router::$rsegments, 3) : array();
pr(Router::$controller);
Event::run('module.post_routing');
}
public function find_file($directory, $filename, $required = FALSE, $ext = FALSE, $use_cache = TRUE)
{
static $found = array();
$search = $directory.'/'.$filename;
$hash = sha1($search.$ext);
if ($use_cache AND isset($found[$hash]))
return $found[$hash];
if ($directory == 'config' OR $directory == 'i18n' OR $directory === 'l10n')
{
$fnd = array();
if (is_file($this->module_path.'/'.$search.EXT)) $fnd[] = $this->module_path.'/'.$search.EXT;
// If required and nothing was found, throw an exception
if ($required == TRUE AND $fnd === array())
throw new Kohana_Exception('core.resource_not_found', Kohana::lang('core.'.inflector::singular($directory)), $filename);
return $found[$hash] = $fnd;
}
else
{
// Users can define their own extensions, css, xml, html, etc
$ext = ($ext == FALSE) ? EXT : '.'.ltrim($ext, '.');
if (is_file($this->module_path.'/'.$search.$ext))
return $found[$hash] = $this->module_path.'/'.$search.$ext;
// If the file is required, throw an exception
if ($required == TRUE)
throw new Kohana_Exception('core.resource_not_found', Kohana::lang('core.'.inflector::singular($directory)), $filename);
return $found[$hash] = FALSE;
}
}
}