/tanora.org/www/framework/system/loader.php
PHP | 173 lines | 110 code | 32 blank | 31 comment | 17 complexity | 1018a9e43dc0fac7b1597845b3983d0d MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
- <?php
- /**
- * This class is used to load components of an MVC framework
- */
-
- class Loader {
-
- private $databases = array();
-
- /**
- * Initializes the database.
- */
- public function load_database($key = 'default') {
-
- $config = Framework::config('database',$key);
-
- // Set to defaults if not defined.
- $host = isset($config['host']) ? $config['host'] : '';
- $user = isset($config['user']) ? $config['user'] : '';
- $pass = isset($config['pass']) ? $config['pass'] : '';
- $name = isset($config['name']) ? $config['name'] : '';
- $type = isset($config['type']) ? $config['type'] : '';
- $port = isset($config['port']) ? $config['port'] : '';
- $sock = isset($config['sock']) ? $config['sock'] : '';
-
- require_once(FRAMEWORK_PATH.'system/database/'.$type.'/database.php');
-
- $class = ucfirst($type) . 'Database';
-
- if(!isset($this->databases[$key])) {
- $this->databases[$key] = new $class($host, $user, $pass, $name, $port, $sock);
- }
-
- if(Framework::config('logging','loader')) {
- Framework::log('Loaded database '.$key,1);
- }
- if(Framework::config('debugging','loader')) {
- Framework::record('Loaded database '.$key);
- }
-
- return $this->databases[$key];
- //return new Database($type, $host, $user, $pass, $name, $port, $sock);
- }
-
- /**
- * Finds and includes the file, then returns an instance of the
- * controller.
- */
- public function load_controller($name) {
- // array of files in order of priority they should be checked
- $dirs = array(CONTROLLER_PATH, FRAMEWORK_PATH.'controllers/');
- foreach($dirs as $dir) {
- $file = $dir.$name.'.php';
- if(file_exists($file)) {
- include_once($file);
-
- $path = array_reverse(explode('/',$name));
-
- $controller = ucfirst($path[0]).'Controller';
-
- if(Framework::config('logging','loader')) {
- Framework::log('Loaded controller '.$name.' from '.$file);
- }
- if(Framework::config('debugging','loader')) {
- Framework::record('Loaded controller '.$name.' from '.$file);
- }
- if(class_exists($controller)) {
- return new $controller();
- }
- }
- }
- return FALSE;
- }
-
- /**
- * Returns a new model object with appropriate database reference.
- */
- public function load_model($name) {
- // array of files in order of priority they should be checked
- $dirs = array(MODEL_PATH, FRAMEWORK_PATH.'models/');
- foreach($dirs as $dir) {
- $file = $dir.$name.'.php';
- if(file_exists($file)) {
- require_once(FRAMEWORK_PATH.'system/model.php');
- require_once(FRAMEWORK_PATH.'system/activerecord.php');
- include_once($file);
-
- $path = array_reverse(explode('/',$name));
-
- $model = ucfirst($path[0]).'Model';
-
- if(Framework::config('logging', 'loader')) {
- Framework::log('Loaded model '.$name.' from '.$file, 1);
- }
- if(Framework::config('debugging', 'loader')) {
- Framework::record('Loaded model '.$name.' from '.$file);
- }
- if(class_exists($model)) {
- return new $model();
- }
- }
- }
- Framework::error('Cannot load model: '.$name);
- return FALSE;
- }
-
- /**
- * Loads a library file and returns the reference.
- */
- public function load_library($name) {
- // array of files in order of priority they should be checked
- $dirs = array(LIBRARY_PATH, FRAMEWORK_PATH.'library/');
- foreach($dirs as $dir) {
- $file = $dir.strtolower($name).'.php';
- if(file_exists($file)) {
- include_once($file);
-
- if(Framework::config('logging', 'loader')) {
- Framework::log('Loaded library '.$name.' from '.$file, 1);
- }
- if(Framework::config('debugging', 'loader')) {
- Framework::record('Loaded library '.$name.' from '.$file);
- }
-
- //$path = array_reverse(explode('/',$name));
-
- //return new $path[0]();
- return TRUE;
- }
- }
- Framework::error('Cannot load library: '.$name);
- return FALSE;
- }
-
- /**
- * Finds and includes a View object with a reference to the view
- * file.
- */
- public function load_view($name) {
- // array of files in order of priority they should be checked
- $files = array(VIEW_PATH.$name.'.php', $file = FRAMEWORK_PATH.'views/'.$name.'.php');
- foreach($files as $file) {
- if(file_exists($file) !== FALSE) {
- require_once(FRAMEWORK_PATH.'system/view.php');
-
- if(Framework::config('logging', 'loader')) {
- Framework::log('Loaded view '.$name.' from '.$file,1);
- }
- if(Framework::config('debugging', 'loader')) {
- Framework::record('Loaded view '.$name.' from '.$file);
- }
-
- return new View($file);
- }
- }
- Framework::error('Cannot load view: '.$name);
- return FALSE;
- }
-
- /**
- * Returns Loader instance
- */
- static public function init() {
- return new Loader();
- }
-
- }
- ?>