tanora /tanora.org/www/framework/system/loader.php

Language PHP Lines 174
MD5 Hash 1018a9e43dc0fac7b1597845b3983d0d Estimated Cost $2,611 (why?)
Repository https://bitbucket.org/ekkl/tanora View Raw File View Project SPDX
  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
<?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();
	}

	
}


?>
Back to Top