laravel.php 5.3 KB

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