blob: 209153379f454887f6d7ca4b22a2d55ec01c1f09 (
plain)
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
|
<?php /* Copyright (c) 2024 Freya Murphy */
class Loader {
// keep track of what has been loaded
private $loaded;
function __construct() {
$this->loaded = array();
}
/**
* Loads a $type of object from a $dir with a given $name
* @param string $name - the name of the object to load
* @param string $dir - the directory theese objects are stored in
* @param string $type - the type of the object
*/
private function load_type($name, $dir, $type): object|NULL {
$path = $dir . '/' . $name . '.php';
if (array_key_exists($path, $this->loaded)) {
return $this->loaded[$path];
}
if (!file_exists($path)) {
return NULL;
}
$parts = explode('/', $name);
$part = end($parts);
$class = ucfirst($part) . '_' . $type;
require($path);
$ref = NULL;
try {
$ref = new ReflectionClass($class);
} catch (Exception $_e) {}
if ($ref === NULL) {
return NULL;
}
$obj = $ref->newInstance($this);
$this->loaded[$path] = $obj;
return $obj;
}
/**
* Loads a model
* @param string $name - the name of the model to load
*/
public function model($name): object|NULL {
$root = $GLOBALS['webroot'];
$dir = $root . '/_model';
return $this->load_type($name, $dir, 'model');
}
/**
* Loads a controller
* @param string $name - the name of the controller to load
*/
public function controller($name): Controller|NULL {
$root = $GLOBALS['webroot'];
$dir = $root . '/_controller';
return $this->load_type($name, $dir, 'controller');
}
/**
* Loads the given common lang
* @param string $lang_code 0 the language code
*/
public function lang($lang_code): void {
$dir = $GLOBALS['webroot'] . '/lang/' . $lang_code . '/';
$lang = $GLOBALS['lang'];
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry === '.' || $entry === '..' || $entry === 'apps') {
continue;
}
$path = $dir . $entry;
require($path);
}
}
$GLOBALS['lang'] = $lang;
}
/**
* Loads a given app specific lang
* @param string $lang_code - the language code
* @param string $name - the name of the app
*/
public function app_lang($lang_code, $name): void {
$dir = $GLOBALS['webroot'] . '/lang/' . $lang_code . '/apps/';
$file = $dir . $name . '.php';
if (file_exists($file)) {
$lang = $GLOBALS['lang'];
require($dir . $name . '.php');
$GLOBALS['lang'] = $lang;
}
}
}
|