laravel.php 7.2 KB

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