From 1f9024763d9224c4cd9a181bac27e6b9f12ad672 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 18 Sep 2024 14:14:53 -0400 Subject: refactor --- src/web/core/component.php | 119 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/web/core/component.php (limited to 'src/web/core/component.php') diff --git a/src/web/core/component.php b/src/web/core/component.php new file mode 100644 index 0000000..376e24d --- /dev/null +++ b/src/web/core/component.php @@ -0,0 +1,119 @@ +newInstance(); + Component::$loaded[$path] = $obj; + + return $obj; + } + + /** + * Loads a model + * @param string $name - the name of the model to load + */ + protected function load_model($name): Model|NULL + { + $dir = WEB_ROOT . '/_model'; + return $this->load_type($name, $dir, 'model'); + } + + /** + * Loads a controller + * @param string $name - the name of the controller to load + */ + public function load_controller($name): Controller|NULL + { + $dir = WEB_ROOT . '/_controller'; + return $this->load_type($name, $dir, 'controller'); + } + +// ========================================= LANG == + + /** + * Loads a php lang file into the lang array + */ + private static function load_lang_file(string $file): void + { + $lang = $GLOBALS['__lang']; + require($file); + $GLOBALS['__lang'] = $lang; + } + + /** + * Loads each php file lang strings in a directory + */ + private static function load_lang_dir(string $dir): void + { + if ($handle = opendir($dir)) { + while (false !== ($entry = readdir($handle))) { + if ($entry === '.' || $entry === '..') + continue; + Component::load_lang_file($entry); + } + } + } + + /** + * Loads the given common lang + */ + protected static function load_lang(string ...$langs): void + { + $root = WEB_ROOT . '/lang'; + + foreach ($langs as $lang) { + $file = "{$root}/{$lang}.php"; + $dir = "{$root}/{$lang}"; + + if (file_exists($file)) + Component::load_lang_file($file); + else if (is_dir($dir)) + Component::load_lang_dir($dir); + + } + } + +} -- cgit v1.2.3-freya