Bugfixes
[carbonphp.git] / Source / carbon / libraries / Loader.php
blobe923c3bae1434c0528edfe6005d59fd680373509
1 <?php
2 /*------------------------------------------------------------
3 * CarbonPHP framework (C) Tom Bell
4 * http://tombell.org.uk
5 *------------------------------------------------------------*/
7 if (!defined('CARBON_PATH'))
9 exit('Direct script access is not allowed.');
12 class Carbon_Loader
14 private $loader_ob_level;
15 private $loader_cached_vars = array();
16 private $loader_classes = array();
17 private $loader_models = array();
18 private $loader_utilities = array();
19 private $loader_varmap = array();
20 private $loader_view_path = '';
22 public function __construct()
24 $this->loader_view_path = APP_PATH . 'views/';
25 $this->loader_ob_level = ob_get_level();
27 log_message('debug', 'Loader class initialised.');
30 public function library($library = '', $params = null)
32 if ($library == '')
34 return false;
37 if (is_array($library))
39 foreach ($library as $class_name)
41 $this->c_load_class($class_name, $params);
44 else
46 $this->c_load_class($library, $params);
49 $this->c_assign_to_models();
52 public function model($model, $name = '', $db_conn = false)
54 if ($model == '')
56 return false;
59 if (strpos($model, '/') === false)
61 $path = '';
63 else
65 $x = explode('/', $model);
66 $model = end($x);
67 unset($x[count($x) - 1]);
68 $path = implode('/', $x) . '/';
71 if ($name == '')
73 $name = $model;
76 if (in_array($name, $this->loader_models, true))
78 return true;
81 $carbon = get_instance();
83 if (isset($carbon->$name))
85 display_error('The model name you are trying to load shared a name with a resource already in use: ' . $name);
88 $model = strtolower($model);
90 if (!file_exists(APP_PATH . 'models/' . $path . $model . FILE_EXT))
92 display_error('Unable to load the model file you have specified: ' . $model);
95 if ($db_conn !== false && !class_exists('Carbon_Database'))
97 if ($db_conn == true)
99 $db_conn = '';
102 $carbon->load->database($db_conn, false, true);
105 if (!class_exists('Model'))
107 require_once(CARBON_PATH . 'libraries/Model' . FILE_EXT);
110 require_once(APP_PATH . 'models/' . $path . $model . FILE_EXT);
112 $model = ucfirst($model);
114 $carbon->$name = new $model();
116 $carbon->$name->assign_libraries();
117 $this->loader_models[] = $name;
120 public function database($params = '', $return = false, $active_record = false)
122 if (class_exists('Carbon_Database') && $return == false && $active_record == false)
124 return false;
127 require_once(CARBON_PATH . 'database/Database' . FILE_EXT);
129 if ($return === true)
131 return Database($params, $active_record);
134 $carbon = get_instance();
135 $carbon->db = '';
136 $carbon->db = Database($params, $active_record);
137 $this->c_assign_to_models();
140 public function dbutils()
142 if (!class_exists('Carbon_Database'))
144 $this->database();
147 $carbon = get_instance();
149 require_once(CARBON_PATH . 'database/Database_utility' . FILE_EXT);
150 require_once(CARBON_PATH . 'database/drivers/' . $carbon->db->dbdriver . '/' . $carbon->db->dbdriver . '_utility' . FILE_EXT);
152 $class = 'Carbon_Database_' . $carbon->db->dbdriver . '_utility';
154 $carbon->dbutils = new $class();
156 $carbon->load->c_assign_to_models();
159 public function view($view, $vars = array(), $return = false)
161 return $this->c_load(array('view' => $view, 'vars' => $this->c_object_to_array($vars), 'return' => $return));
164 public function file($path, $return = false)
166 return $this->c_load(array('path' => $path, 'return' => $return));
169 public function vars($vars = array())
171 $vars = $this->c_object_to_array($vars);
173 if (is_array($vars) && count($vars) > 0)
175 foreach ($vars as $key => $val)
177 $this->loader_cached_vars[$key] = $val;
182 public function utility($utilities = array())
184 if (!is_array($utilities))
186 $utilities = array($utilities);
189 foreach ($utilities as $utility)
191 $utility = strtolower(str_replace(FILE_EXT, '', str_replace('_utility', '', $utility)) . '_utility');
193 if (isset($this->loader_utilities[$utility]))
195 continue;
198 if (file_exists(APP_PATH . 'utilities/' . $utility . FILE_EXT))
200 include_once(APP_PATH . 'utilities/' . $utility . FILE_EXT);
202 else
204 if (file_exists(CARBON_PATH . 'utilities/' . $utility . FILE_EXT))
206 include(CARBON_PATH . 'utilities/' . $utility . FILE_EXT);
208 else
210 display_error('Unable to load the utility file: utilities/' . $utility . FILE_EXT);
214 $this->loader_utilities[$utility] = true;
217 log_message('debug', 'Utilities loaded: ' . implode(', ', $utilities));
220 public function language($file = array(), $lang = '')
222 $carbon = get_instance();
224 if (!is_array($file))
226 $file = array($file);
229 foreach ($file as $langfile)
231 $carbon->lang->load($langfile, $lang);
235 public function scaffold_language($file = '', $lang = '', $return = false)
237 $carbon = get_instance();
239 return $carbon->lang->load($file, $lang, $return);
242 public function config($file = '', $use_sections = false, $fail_gracefully = false)
244 $carbon = get_instance();
245 $carbon->config->load($file, $use_sections, $fail_gracefully);
248 public function scaffolding($table = '')
250 if ($table === false)
252 display_error('You must include the name of the table you would like to access when you initialise scaffolding');
255 $carbon = get_instance();
256 $carbon->set_scaffolding(true);
257 $carbon->set_scaff_table($table);
260 private function c_load($data)
263 $view = $vars = $path = $return = '';
265 foreach (array('view', 'vars', 'path', 'return') as $val)
267 $$val = (!isset($data[$val])) ? false : $data[$val];
270 if ($path == '')
272 $ext = pathinfo($view, PATHINFO_EXTENSION);
273 $file = ($ext =='') ? $view . FILE_EXT : $view;
274 $path = $this->loader_view_path . $file;
276 else
278 $x = explode('/', $path);
279 $file = end($x);
282 if (!file_exists($path))
284 display_error('Unable to load the specified file: ' . $file);
287 $carbon = get_instance();
289 foreach (get_object_vars($carbon) as $key => $var)
291 $this->$key = $carbon->$key;
294 if (is_array($vars))
296 $this->loader_cached_vars = array_merge($this->loader_cached_vars, $vars);
299 extract($this->loader_cached_vars);
301 ob_start();
303 if ((bool) @ini_get('short_open_tag') === false && get_config_item('short_tag_rewrite') == true)
305 echo eval('?>' . preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($path))) . '<?php ');
307 else
309 include($path);
312 log_message('debug', 'Loaded file: ' . $path);
314 if ($return === true)
316 $buffer = ob_get_contents();
317 @ob_end_clean();
319 return $buffer;
322 if (ob_get_level() > $this->loader_ob_level + 1)
324 ob_end_flush();
326 else
328 global $output;
329 $output->set_final_output(ob_get_contents());
330 @ob_end_clean();
334 private function c_load_class($class_name, $params = null)
336 $class_name = str_replace(FILE_EXT, '', $class_name);
338 foreach (array(ucfirst($class_name), strtolower($class_name)) as $class)
340 $subclass = APP_PATH . 'libraries/' . get_config_value('subclass_prefix') . $class . FILE_EXT;
342 if (file_exists($subclass))
344 $base_class = CARBON_PATH . 'libraries/' . ucfirst($class) . FILE_EXT;
346 if (!file_exists($base_class))
348 log_message('error', 'Unable to load the requested class file: ' . $class);
349 display_error('Unable to load the requested class file: ' . $class);
351 return false;
354 if (in_array($subclass, $this->loader_classes))
356 $is_duplicated = true;
357 log_message('debug', 'Class already loaded, ignoring second load attempt: ' . $class);
359 return false;
362 include($base_class);
363 include($subclass);
365 $this->loader_classes[] = $subclass;
367 return $this->c_init_class($class, get_config_item('subclass_prefix'), $params);
370 $is_duplicated = false;
372 for ($i = 1; $i < 3; $i++)
374 $path = ($i % 2) ? APP_PATH : CARBON_PATH;
375 $file_path = $path . 'libraries/' . $class . FILE_EXT;
377 if (!file_exists($file_path))
379 continue;
382 if (in_array($file_path, $this->loader_classes))
384 $is_duplicated = true;
385 log_message('debug', 'Class already loaded, ignoring second load attempt: ' . $class);
387 return false;
390 include($file_path);
392 $this->loader_classes[] = $file_path;
394 return $this->c_init_class($class, '', $params);
398 if ($is_duplicated == false)
400 log_message('error', 'Unable to load the requested class file: ' . $class);
401 display_error('Unable to load the requested class file: ' . $class);
403 return false;
407 private function c_init_class($class, $prefix = '', $config = false)
409 if ($config === null)
411 $config = null;
413 if (file_exists(APP_PATH . 'config/' . $class . FILE_EXT))
415 include(APP_PATH . 'config/' . $class . FILE_EXT);
419 if ($prefix == '')
421 $name = (class_exists('Carbon_' . $class)) ? 'Carbon_' . $class : $class;
423 else
425 $name = $prefix . $class;
428 $class = strtolower($class);
429 $classvar = (!isset($this->loader_varmap[$class])) ? $class : $this->loader_varmap[$class];
431 $carbon = get_instance();
433 if ($config !== null)
435 $carbon->$classvar = new $name($config);
437 else
439 $carbon->$classvar = new $name;
443 public function autoloader()
445 include(APP_PATH . 'config/autoload' . FILE_EXT);
447 if (!isset($autoload))
449 return false;
452 if (count($autoload['config']) > 0)
454 $carbon = get_instance();
456 foreach ($autoload['config'] as $key => $value)
458 $carbon->config->load($value);
462 foreach (array('utility', 'language') as $type)
464 if (isset($autoload[$type]) && count($autoload[$type]) > 0)
466 $this->$type($autoload[$type]);
470 if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)
472 if (in_array('database', $autoload['libraries']))
474 $this->database();
475 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
478 if (in_array('model', $autoload['libraries']))
480 $this->model();
481 $autoload['libraries'] = array_diff($autoload['libraries'], array('model'));
484 if (in_array('scaffolding', $autoload['libraries']))
486 $this->scaffolding();
487 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
490 foreach ($autoload['libraries'] as $library)
492 $this->library($library);
497 public function set_view_path($view_path)
499 $this->loader_view_path = $view_path;
502 private function c_assign_to_models()
504 if (count($this->loader_models) == 0)
506 return false;
509 $carbon = get_instance();
511 foreach ($this->loader_models as $model)
513 $carbon->$model->assign_libraries();
517 private function c_object_to_array($object)
519 return (is_object($object)) ? get_object_vars($object) : $object;