laravel.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php namespace Laravel;
  2. /**
  3. * Bootstrap the core framework components like the IoC container,
  4. * configuration class, and the class auto-loader. Once this file
  5. * has run, the framework is essentially ready for use.
  6. */
  7. require 'bootstrap/core.php';
  8. /**
  9. * Register the default timezone for the application. This will be
  10. * the default timezone used by all date / timezone functions in
  11. * the entire application.
  12. */
  13. date_default_timezone_set(Config::$items['application']['timezone']);
  14. /**
  15. * Create the exception logging function. All of the error logging
  16. * is routed through here to avoid duplicate code. This Closure
  17. * will determine if the actual logging Closure should be called.
  18. */
  19. $logger = function($exception)
  20. {
  21. if (Config::$items['error']['log'])
  22. {
  23. call_user_func(Config::$items['error']['logger'], $exception);
  24. }
  25. };
  26. /**
  27. * Create the exception handler function. All of the error handlers
  28. * registered by the framework call this closure to avoid duplicate
  29. * code. This Closure will pass the exception to the developer
  30. * defined handler in the configuration file.
  31. */
  32. $handler = function($exception) use ($logger)
  33. {
  34. $logger($exception);
  35. if (Config::$items['error']['detail'])
  36. {
  37. echo "<html><h2>Unhandled Exception</h2>
  38. <h3>Message:</h3>
  39. <pre>".$exception->getMessage()."</pre>
  40. <h3>Location:</h3>
  41. <pre>".$exception->getFile()." on line ".$exception->getLine()."</pre>
  42. <h3>Stack Trace:</h3>
  43. <pre>".$exception->getTraceAsString()."</pre></html>";
  44. }
  45. else
  46. {
  47. Response::error('500')->send();
  48. }
  49. exit(1);
  50. };
  51. /**
  52. * Register the PHP exception handler. The framework throws exceptions
  53. * on every error that cannot be handled. All of those exceptions will
  54. * be sent through this closure for processing.
  55. */
  56. set_exception_handler(function($exception) use ($handler)
  57. {
  58. $handler($exception);
  59. });
  60. /**
  61. * Register the PHP error handler. All PHP errors will fall into this
  62. * handler, which will convert the error into an ErrorException object
  63. * and pass the exception into the common exception handler. Suppressed
  64. * errors are ignored and errors in the developer configured whitelist
  65. * are silently logged.
  66. */
  67. set_error_handler(function($code, $error, $file, $line) use ($logger)
  68. {
  69. if (error_reporting() === 0) return;
  70. $exception = new \ErrorException($error, $code, 0, $file, $line);
  71. if (in_array($code, Config::$items['error']['ignore']))
  72. {
  73. return $logger($exception);
  74. }
  75. throw $exception;
  76. });
  77. /**
  78. * Register the PHP shutdown handler. This function will be called
  79. * at the end of the PHP script or on a fatal PHP error. If an error
  80. * has occured, we will convert it to an ErrorException and pass it
  81. * to the common exception handler for the framework.
  82. */
  83. register_shutdown_function(function() use ($handler)
  84. {
  85. if ( ! is_null($error = error_get_last()))
  86. {
  87. extract($error, EXTR_SKIP);
  88. $handler(new \ErrorException($message, $type, 0, $file, $line));
  89. }
  90. });
  91. /**
  92. * Setting the PHP error reporting level to -1 essentially forces
  93. * PHP to report every error, and is guranteed to show every error
  94. * on future versions of PHP.
  95. *
  96. * If error detail is turned off, we will turn off all PHP error
  97. * reporting and display since the framework will be displaying a
  98. * generic message and we don't want any sensitive details about
  99. * the exception leaking into the views.
  100. */
  101. error_reporting(-1);
  102. ini_set('display_errors', 'Off');
  103. /**
  104. * Load the session and session manager instance. The session
  105. * payload will be registered in the IoC container as an instance
  106. * so it can be retrieved easily throughout the application.
  107. */
  108. if (Config::$items['session']['driver'] !== '')
  109. {
  110. require SYS_PATH.'ioc'.EXT;
  111. require SYS_PATH.'session/payload'.EXT;
  112. require SYS_PATH.'session/drivers/driver'.EXT;
  113. require SYS_PATH.'session/drivers/factory'.EXT;
  114. $id = Cookie::get(Config::$items['session']['cookie']);
  115. $driver = Session\Drivers\Factory::make(Config::$items['session']['driver']);
  116. IoC::instance('laravel.session', new Session\Payload($driver, $id));
  117. }
  118. /**
  119. * Manually load some core classes that are used on every request so
  120. * we can avoid using the loader for these classes. This saves us
  121. * some overhead on each request.
  122. */
  123. require SYS_PATH.'uri'.EXT;
  124. require SYS_PATH.'input'.EXT;
  125. require SYS_PATH.'request'.EXT;
  126. require SYS_PATH.'response'.EXT;
  127. require SYS_PATH.'routing/route'.EXT;
  128. require SYS_PATH.'routing/router'.EXT;
  129. require SYS_PATH.'routing/loader'.EXT;
  130. require SYS_PATH.'routing/filter'.EXT;
  131. /**
  132. * Gather the input to the application based on the current request.
  133. * The input will be gathered based on the current request method and
  134. * will be set on the Input manager.
  135. */
  136. $input = array();
  137. switch (Request::method())
  138. {
  139. case 'GET':
  140. $input = $_GET;
  141. break;
  142. case 'POST':
  143. $input = $_POST;
  144. break;
  145. case 'PUT':
  146. case 'DELETE':
  147. if (Request::spoofed())
  148. {
  149. $input = $_POST;
  150. }
  151. else
  152. {
  153. parse_str(file_get_contents('php://input'), $input);
  154. }
  155. }
  156. /**
  157. * The spoofed request method is removed from the input so it is not
  158. * unexpectedly included in Input::all() or Input::get(). Leaving it
  159. * in the input array could cause unexpected results if the developer
  160. * fills an Eloquent model with the input.
  161. */
  162. unset($input[Request::spoofer]);
  163. Input::$input = $input;
  164. /**
  165. * Route the request to the proper route in the application. If a
  166. * route is found, the route will be called with the current request
  167. * instance. If no route is found, the 404 response will be returned
  168. * to the browser.
  169. */
  170. Routing\Filter::register(require APP_PATH.'filters'.EXT);
  171. $loader = new Routing\Loader(APP_PATH, ROUTE_PATH);
  172. $router = new Routing\Router($loader, CONTROLLER_PATH);
  173. IoC::instance('laravel.routing.router', $router);
  174. Request::$route = $router->route(Request::method(), URI::current());
  175. if ( ! is_null(Request::$route))
  176. {
  177. $response = Request::$route->call();
  178. }
  179. else
  180. {
  181. $response = Response::error('404');
  182. }
  183. /**
  184. * Close the session and write the active payload to persistent
  185. * storage. The session cookie will also be written and if the
  186. * driver is a sweeper, session garbage collection might be
  187. * performed depending on the "sweepage" probability.
  188. */
  189. if (Config::$items['session']['driver'] !== '')
  190. {
  191. IoC::core('session')->save();
  192. }
  193. $response->send();