laravel.php 8.0 KB

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