laravel.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php namespace System;
  2. // --------------------------------------------------------------
  3. // Define the core framework paths.
  4. // --------------------------------------------------------------
  5. define('APP_PATH', realpath($application).'/');
  6. define('SYS_PATH', realpath($system).'/');
  7. define('PUBLIC_PATH', realpath($public).'/');
  8. define('PACKAGE_PATH', realpath($packages).'/');
  9. define('MODULE_PATH', realpath($modules).'/');
  10. define('STORAGE_PATH', realpath($storage).'/');
  11. define('BASE_PATH', realpath(str_replace('system', '', $system)).'/');
  12. unset($application, $system, $public, $packages, $modules, $storage);
  13. // --------------------------------------------------------------
  14. // Define various other framework paths.
  15. // --------------------------------------------------------------
  16. define('CACHE_PATH', STORAGE_PATH.'cache/');
  17. define('CONFIG_PATH', APP_PATH.'config/');
  18. define('DATABASE_PATH', STORAGE_PATH.'db/');
  19. define('LANG_PATH', APP_PATH.'lang/');
  20. define('LIBRARY_PATH', APP_PATH.'libraries/');
  21. define('MODEL_PATH', APP_PATH.'models/');
  22. define('ROUTE_PATH', APP_PATH.'routes/');
  23. define('SCRIPT_PATH', PUBLIC_PATH.'js/');
  24. define('SESSION_PATH', STORAGE_PATH.'sessions/');
  25. define('STYLE_PATH', PUBLIC_PATH.'css/');
  26. define('VIEW_PATH', APP_PATH.'views/');
  27. // --------------------------------------------------------------
  28. // Define the PHP file extension.
  29. // --------------------------------------------------------------
  30. define('EXT', '.php');
  31. // --------------------------------------------------------------
  32. // Load the classes used by the auto-loader.
  33. // --------------------------------------------------------------
  34. require SYS_PATH.'loader'.EXT;
  35. require SYS_PATH.'config'.EXT;
  36. require SYS_PATH.'arr'.EXT;
  37. // --------------------------------------------------------------
  38. // Register the auto-loader.
  39. // --------------------------------------------------------------
  40. Loader::bootstrap();
  41. spl_autoload_register(array('System\\Loader', 'load'));
  42. // --------------------------------------------------------------
  43. // Set the error reporting and display levels.
  44. // --------------------------------------------------------------
  45. error_reporting(E_ALL | E_STRICT);
  46. ini_set('display_errors', 'Off');
  47. // --------------------------------------------------------------
  48. // Register the error handlers.
  49. // --------------------------------------------------------------
  50. set_exception_handler(function($e)
  51. {
  52. require_once SYS_PATH.'exception/handler'.EXT;
  53. require_once SYS_PATH.'exception/examiner'.EXT;
  54. Exception\Handler::make($e)->handle();
  55. });
  56. set_error_handler(function($number, $error, $file, $line)
  57. {
  58. require_once SYS_PATH.'exception/handler'.EXT;
  59. require_once SYS_PATH.'exception/examiner'.EXT;
  60. Exception\Handler::make(new \ErrorException($error, $number, 0, $file, $line))->handle();
  61. });
  62. register_shutdown_function(function()
  63. {
  64. if ( ! is_null($error = error_get_last()))
  65. {
  66. require_once SYS_PATH.'exception/handler'.EXT;
  67. require_once SYS_PATH.'exception/examiner'.EXT;
  68. extract($error);
  69. Exception\Handler::make(new \ErrorException($message, $type, 0, $file, $line))->handle();
  70. }
  71. });
  72. // --------------------------------------------------------------
  73. // Set the default timezone.
  74. // --------------------------------------------------------------
  75. date_default_timezone_set(Config::get('application.timezone'));
  76. // --------------------------------------------------------------
  77. // Load the session.
  78. // --------------------------------------------------------------
  79. if (Config::get('session.driver') != '')
  80. {
  81. Session::load(Cookie::get('laravel_session'));
  82. }
  83. // --------------------------------------------------------------
  84. // Load all of the core routing classes.
  85. // --------------------------------------------------------------
  86. require SYS_PATH.'request'.EXT;
  87. require SYS_PATH.'response'.EXT;
  88. require SYS_PATH.'routing/route'.EXT;
  89. require SYS_PATH.'routing/router'.EXT;
  90. require SYS_PATH.'routing/loader'.EXT;
  91. require SYS_PATH.'routing/filter'.EXT;
  92. // --------------------------------------------------------------
  93. // Load the packages that are in the auto-loaded packages array.
  94. // --------------------------------------------------------------
  95. if (count(Config::get('application.packages')) > 0)
  96. {
  97. require SYS_PATH.'package'.EXT;
  98. Package::load(Config::get('application.packages'));
  99. }
  100. // --------------------------------------------------------------
  101. // Determine the module that should handle the request.
  102. // --------------------------------------------------------------
  103. $segments = explode('/', Request::uri());
  104. define('ACTIVE_MODULE', (in_array($segments[0], Config::get('application.modules'))) ? $segments[0] : 'application');
  105. // --------------------------------------------------------------
  106. // Determine the path to the root of the active module.
  107. // --------------------------------------------------------------
  108. define('ACTIVE_MODULE_PATH', (ACTIVE_MODULE == 'application') ? APP_PATH : MODULE_PATH.ACTIVE_MODULE.'/');
  109. // --------------------------------------------------------------
  110. // Register the filters for the active module.
  111. // --------------------------------------------------------------
  112. Routing\Filter::register(require APP_PATH.'filters'.EXT);
  113. if (ACTIVE_MODULE !== 'application' and file_exists($filters = ACTIVE_MODULE_PATH.'filters'.EXT))
  114. {
  115. Routing\Filter::register(require $filters);
  116. }
  117. // --------------------------------------------------------------
  118. // Call the "before" filter for the application and module.
  119. // --------------------------------------------------------------
  120. foreach (array('before', ACTIVE_MODULE.'::before') as $filter)
  121. {
  122. $response = Routing\Filter::call($filter, array(Request::method(), Request::uri()), true);
  123. if ( ! is_null($response)) break;
  124. }
  125. // --------------------------------------------------------------
  126. // Route the request and get the response from the route.
  127. // --------------------------------------------------------------
  128. if (is_null($response))
  129. {
  130. $route = Routing\Router::make(Request::method(), Request::uri(), new Routing\Loader(ACTIVE_MODULE_PATH))->route();
  131. $response = (is_null($route)) ? Response::error('404') : $route->call();
  132. }
  133. $response = Response::prepare($response);
  134. // --------------------------------------------------------------
  135. // Call the "after" filter for the application and module.
  136. // --------------------------------------------------------------
  137. foreach (array(ACTIVE_MODULE.'::after', 'after') as $filter)
  138. {
  139. Routing\Filter::call($filter, array($response, Request::method(), Request::uri()));
  140. }
  141. // --------------------------------------------------------------
  142. // Stringify the response.
  143. // --------------------------------------------------------------
  144. $response->content = (string) $response->content;
  145. // --------------------------------------------------------------
  146. // Close the session.
  147. // --------------------------------------------------------------
  148. if (Config::get('session.driver') != '')
  149. {
  150. Session::close();
  151. }
  152. // --------------------------------------------------------------
  153. // Send the response to the browser.
  154. // --------------------------------------------------------------
  155. $response->send();