/third_party/gas/bootstrap.php

https://github.com/toopay/gas-orm · PHP · 230 lines · 113 code · 32 blank · 85 comment · 13 complexity · 3359a6619125eed0ec7ac18cb1aacbbf MD5 · raw file

  1. <?php defined('BASEPATH') or die('No direct access allowed');
  2. /**
  3. *---------------------------------------------------------------
  4. * Load the DB packages and environment preparation
  5. *---------------------------------------------------------------
  6. */
  7. // Set error level
  8. if (ENVIRONMENT === 'testing') error_reporting(E_STRICT);
  9. // Check the environment var
  10. if ( ! defined('DB_GROUP'))
  11. {
  12. define('DB_GROUP', 'default');
  13. }
  14. else
  15. {
  16. unset($DB);
  17. }
  18. // Define this root folder as Gas ORM base path
  19. defined('GASPATH') or define('GASPATH', __DIR__.DIRECTORY_SEPARATOR);
  20. // Load everything on base gas directory
  21. require_once GASPATH.'classes'.DIRECTORY_SEPARATOR.'core.php';
  22. require_once GASPATH.'classes'.DIRECTORY_SEPARATOR.'data.php';
  23. require_once GASPATH.'classes'.DIRECTORY_SEPARATOR.'janitor.php';
  24. require_once GASPATH.'classes'.DIRECTORY_SEPARATOR.'orm.php';
  25. require_once GASPATH.'interfaces'.DIRECTORY_SEPARATOR.'extension.php';
  26. // Load needed DB files
  27. require_once Gas\Janitor::path('base').'database'.DIRECTORY_SEPARATOR.'DB.php';
  28. require_once Gas\Janitor::path('base').'database'.DIRECTORY_SEPARATOR.'DB_forge.php';
  29. require_once Gas\Janitor::path('base').'database'.DIRECTORY_SEPARATOR.'DB_utility.php';
  30. // Define DB path
  31. define('DBPATH', Gas\Janitor::path('base').'database'.DIRECTORY_SEPARATOR);
  32. define('DBDRIVERSPATH', DBPATH.'drivers'.DIRECTORY_SEPARATOR);
  33. // Mock internal CI instance and low-level functions,
  34. // in case we run Gas ORM outside CI scope
  35. if ( ! function_exists('get_instance'))
  36. {
  37. // Build our own TRON!
  38. class Tron {
  39. /**
  40. * @var object Tron super-object
  41. */
  42. private static $instance;
  43. /**
  44. * Constructor
  45. *
  46. * @param mixed CI DB instance
  47. * @return void
  48. */
  49. function __construct($DB)
  50. {
  51. $this->db = $DB;
  52. static::$instance = $this;
  53. }
  54. /**
  55. * Get Tron super-object
  56. *
  57. * @return object
  58. */
  59. public static function &get_instance()
  60. {
  61. return static::$instance;
  62. }
  63. /**
  64. * Serve static call for TRON instantiation
  65. *
  66. * @return object
  67. */
  68. public static function make()
  69. {
  70. return new static(NULL);
  71. }
  72. /**
  73. * Serve loader
  74. *
  75. * @param mixed
  76. * @return object
  77. */
  78. public static function load($args)
  79. {
  80. // TODO : resolve this loader to perform any necessarily
  81. // action, to load the real requested class.
  82. return static::$instance;
  83. }
  84. /**
  85. * Serve undefined Core CI properties
  86. */
  87. public function __get($name)
  88. {
  89. if ($name == 'load')
  90. {
  91. return static::$instance;
  92. }
  93. }
  94. /**
  95. * Serve other methods, to capture the error for the very least
  96. *
  97. * @param string
  98. * @param mixed
  99. * @throws LogicException Serve show error method
  100. * @return mixed
  101. */
  102. public function __call($name, $arguments)
  103. {
  104. // Only response for error
  105. if ($name == 'show_error')
  106. {
  107. // Get any meaning information
  108. $internal_error = (array_key_exists(2, $arguments)) ? $arguments[2] : 'Undefined CI Error';
  109. // Good bye
  110. throw new LogicException('CI Internal Error with message : '.$internal_error);
  111. }
  112. // For package path
  113. elseif ($name == 'get_package_paths')
  114. {
  115. return array();
  116. }
  117. }
  118. }
  119. /**
  120. * global is_php method
  121. */
  122. function is_php($version = '5.0.0')
  123. {
  124. static $_is_php;
  125. $version = (string)$version;
  126. if ( ! isset($_is_php[$version]))
  127. {
  128. $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
  129. }
  130. return $_is_php[$version];
  131. }
  132. /**
  133. * global get_instance method
  134. */
  135. function &get_instance() {
  136. $instance =& Tron::get_instance();
  137. // If Tron not initialized yet, build a mock
  138. if (is_null($instance))
  139. {
  140. $instance = new stdClass();
  141. $instance->load = new Tron('undefined');
  142. }
  143. return $instance;
  144. }
  145. /**
  146. * global log_message method
  147. */
  148. function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
  149. {
  150. // Good bye
  151. throw new LogicException('CI Internal Error with message : '.$message);
  152. }
  153. /**
  154. * global log_message method
  155. */
  156. function log_message(){}
  157. /**
  158. * global load_class method
  159. */
  160. function load_class(){
  161. // Capture argument
  162. $args = func_get_args();
  163. // Return new TRON to resolve any possible error
  164. return Tron::make()->load($args);
  165. }
  166. }
  167. // Validate DB instance
  168. if ( ! class_exists('CI_DB'))
  169. {
  170. try{
  171. $DB = &DB(DB_GROUP);
  172. } catch (Exception $e) {
  173. die('Cant connect to database : '.$e->getMessage().', please check config/testing/database.php.'."\n");
  174. }
  175. }
  176. if ( ! $DB instanceof CI_DB_Driver)
  177. {
  178. throw new InvalidArgumentException('db_connection_error:default');
  179. }
  180. // Load required utility files once
  181. require_once(DBPATH.'DB_forge.php');
  182. require_once(DBPATH.'DB_utility.php');
  183. require_once(DBDRIVERSPATH.$DB->dbdriver.DIRECTORY_SEPARATOR.$DB->dbdriver.'_utility.php');
  184. require_once(DBDRIVERSPATH.$DB->dbdriver.DIRECTORY_SEPARATOR.$DB->dbdriver.'_forge.php');
  185. // if we run Gas ORM outside CI scope
  186. if ( class_exists('Tron'))
  187. {
  188. // MayDay!! Call TRON
  189. $tron = new Tron($DB);
  190. }
  191. /**
  192. *---------------------------------------------------------------
  193. * Instantiate Gas ORM Core classes
  194. *---------------------------------------------------------------
  195. */
  196. // Instantiate core class then clean up global variables
  197. Gas\Core::make($DB, $config)->init();
  198. unset($DB, $config);