PageRenderTime 56ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/ekkl/tanora
PHP | 173 lines | 110 code | 32 blank | 31 comment | 17 complexity | 1018a9e43dc0fac7b1597845b3983d0d MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * This class is used to load components of an MVC framework
  4. */
  5. class Loader {
  6. private $databases = array();
  7. /**
  8. * Initializes the database.
  9. */
  10. public function load_database($key = 'default') {
  11. $config = Framework::config('database',$key);
  12. // Set to defaults if not defined.
  13. $host = isset($config['host']) ? $config['host'] : '';
  14. $user = isset($config['user']) ? $config['user'] : '';
  15. $pass = isset($config['pass']) ? $config['pass'] : '';
  16. $name = isset($config['name']) ? $config['name'] : '';
  17. $type = isset($config['type']) ? $config['type'] : '';
  18. $port = isset($config['port']) ? $config['port'] : '';
  19. $sock = isset($config['sock']) ? $config['sock'] : '';
  20. require_once(FRAMEWORK_PATH.'system/database/'.$type.'/database.php');
  21. $class = ucfirst($type) . 'Database';
  22. if(!isset($this->databases[$key])) {
  23. $this->databases[$key] = new $class($host, $user, $pass, $name, $port, $sock);
  24. }
  25. if(Framework::config('logging','loader')) {
  26. Framework::log('Loaded database '.$key,1);
  27. }
  28. if(Framework::config('debugging','loader')) {
  29. Framework::record('Loaded database '.$key);
  30. }
  31. return $this->databases[$key];
  32. //return new Database($type, $host, $user, $pass, $name, $port, $sock);
  33. }
  34. /**
  35. * Finds and includes the file, then returns an instance of the
  36. * controller.
  37. */
  38. public function load_controller($name) {
  39. // array of files in order of priority they should be checked
  40. $dirs = array(CONTROLLER_PATH, FRAMEWORK_PATH.'controllers/');
  41. foreach($dirs as $dir) {
  42. $file = $dir.$name.'.php';
  43. if(file_exists($file)) {
  44. include_once($file);
  45. $path = array_reverse(explode('/',$name));
  46. $controller = ucfirst($path[0]).'Controller';
  47. if(Framework::config('logging','loader')) {
  48. Framework::log('Loaded controller '.$name.' from '.$file);
  49. }
  50. if(Framework::config('debugging','loader')) {
  51. Framework::record('Loaded controller '.$name.' from '.$file);
  52. }
  53. if(class_exists($controller)) {
  54. return new $controller();
  55. }
  56. }
  57. }
  58. return FALSE;
  59. }
  60. /**
  61. * Returns a new model object with appropriate database reference.
  62. */
  63. public function load_model($name) {
  64. // array of files in order of priority they should be checked
  65. $dirs = array(MODEL_PATH, FRAMEWORK_PATH.'models/');
  66. foreach($dirs as $dir) {
  67. $file = $dir.$name.'.php';
  68. if(file_exists($file)) {
  69. require_once(FRAMEWORK_PATH.'system/model.php');
  70. require_once(FRAMEWORK_PATH.'system/activerecord.php');
  71. include_once($file);
  72. $path = array_reverse(explode('/',$name));
  73. $model = ucfirst($path[0]).'Model';
  74. if(Framework::config('logging', 'loader')) {
  75. Framework::log('Loaded model '.$name.' from '.$file, 1);
  76. }
  77. if(Framework::config('debugging', 'loader')) {
  78. Framework::record('Loaded model '.$name.' from '.$file);
  79. }
  80. if(class_exists($model)) {
  81. return new $model();
  82. }
  83. }
  84. }
  85. Framework::error('Cannot load model: '.$name);
  86. return FALSE;
  87. }
  88. /**
  89. * Loads a library file and returns the reference.
  90. */
  91. public function load_library($name) {
  92. // array of files in order of priority they should be checked
  93. $dirs = array(LIBRARY_PATH, FRAMEWORK_PATH.'library/');
  94. foreach($dirs as $dir) {
  95. $file = $dir.strtolower($name).'.php';
  96. if(file_exists($file)) {
  97. include_once($file);
  98. if(Framework::config('logging', 'loader')) {
  99. Framework::log('Loaded library '.$name.' from '.$file, 1);
  100. }
  101. if(Framework::config('debugging', 'loader')) {
  102. Framework::record('Loaded library '.$name.' from '.$file);
  103. }
  104. //$path = array_reverse(explode('/',$name));
  105. //return new $path[0]();
  106. return TRUE;
  107. }
  108. }
  109. Framework::error('Cannot load library: '.$name);
  110. return FALSE;
  111. }
  112. /**
  113. * Finds and includes a View object with a reference to the view
  114. * file.
  115. */
  116. public function load_view($name) {
  117. // array of files in order of priority they should be checked
  118. $files = array(VIEW_PATH.$name.'.php', $file = FRAMEWORK_PATH.'views/'.$name.'.php');
  119. foreach($files as $file) {
  120. if(file_exists($file) !== FALSE) {
  121. require_once(FRAMEWORK_PATH.'system/view.php');
  122. if(Framework::config('logging', 'loader')) {
  123. Framework::log('Loaded view '.$name.' from '.$file,1);
  124. }
  125. if(Framework::config('debugging', 'loader')) {
  126. Framework::record('Loaded view '.$name.' from '.$file);
  127. }
  128. return new View($file);
  129. }
  130. }
  131. Framework::error('Cannot load view: '.$name);
  132. return FALSE;
  133. }
  134. /**
  135. * Returns Loader instance
  136. */
  137. static public function init() {
  138. return new Loader();
  139. }
  140. }
  141. ?>