laravel.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php namespace Laravel;
  2. /**
  3. * Bootstrap the core framework components like the IoC container and
  4. * the 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. * Register the default timezone for the application. This will be the
  10. * default timezone used by all date / timezone functions throughout
  11. * the entire application.
  12. */
  13. date_default_timezone_set(Config::get('application.timezone'));
  14. /**
  15. * Register the PHP exception handler. The framework throws exceptions
  16. * on every error that cannot be handled. All of those exceptions will
  17. * be sent through this closure for processing.
  18. */
  19. set_exception_handler(function($e)
  20. {
  21. Error::exception($e);
  22. });
  23. /**
  24. * Register the PHP error handler. All PHP errors will fall into this
  25. * handler, which will convert the error into an ErrorException object
  26. * and pass the exception into the common exception handler. Suppressed
  27. * errors are ignored and errors in the developer configured whitelist
  28. * are silently logged.
  29. */
  30. set_error_handler(function($code, $error, $file, $line)
  31. {
  32. Error::native($code, $error, $file, $line);
  33. });
  34. /**
  35. * Register the PHP shutdown handler. This function will be called
  36. * at the end of the PHP script or on a fatal PHP error. If an error
  37. * has occured, we will convert it to an ErrorException and pass it
  38. * to the common exception handler for the framework.
  39. */
  40. register_shutdown_function(function()
  41. {
  42. Error::shutdown();
  43. });
  44. /**
  45. * Setting the PHP error reporting level to -1 essentially forces
  46. * PHP to report every error, and is guranteed to show every error
  47. * on future versions of PHP.
  48. *
  49. * If error detail is turned off, we will turn off all PHP error
  50. * reporting and display since the framework will be displaying a
  51. * generic message and we don't want any sensitive details about
  52. * the exception leaking into the views.
  53. */
  54. error_reporting(-1);
  55. ini_set('display_errors', 'Off');
  56. /**
  57. * Load the session and session manager instance. The session
  58. * payload will be registered in the IoC container as an instance
  59. * so it can be retrieved easily throughout the application.
  60. */
  61. if (Config::get('session.driver') !== '')
  62. {
  63. Session::start(Config::get('session.driver'));
  64. Session::load(Cookie::get(Config::get('session.cookie')));
  65. }
  66. /**
  67. * Gather the input to the application based on the current request.
  68. * The input will be gathered based on the current request method
  69. * and will be set on the Input manager.
  70. */
  71. $input = array();
  72. switch (Request::method())
  73. {
  74. case 'GET':
  75. $input = $_GET;
  76. break;
  77. case 'POST':
  78. $input = $_POST;
  79. break;
  80. case 'PUT':
  81. case 'DELETE':
  82. if (Request::spoofed())
  83. {
  84. $input = $_POST;
  85. }
  86. else
  87. {
  88. parse_str(file_get_contents('php://input'), $input);
  89. }
  90. }
  91. /**
  92. * The spoofed request method is removed from the input so it is not
  93. * unexpectedly included in Input::all() or Input::get(). Leaving it
  94. * in the input array could cause unexpected results if an Eloquent
  95. * model is filled with the input.
  96. */
  97. unset($input[Request::spoofer]);
  98. Input::$input = $input;
  99. /**
  100. * Start all of the bundles that are specified in the configuration
  101. * array of auto-loaded bundles. This gives the developer the ability
  102. * to conveniently and automatically load bundles that are used on
  103. * every request to their application.
  104. */
  105. foreach (Config::get('application.bundles') as $bundle)
  106. {
  107. Bundle::start($bundle);
  108. }
  109. /**
  110. * Load the "application" bundle. Though the application folder is
  111. * not typically considered a bundle, it is started like one and
  112. * essentially serves as the "default" bundle.
  113. */
  114. Bundle::start(DEFAULT_BUNDLE);
  115. /**
  116. * If the first segment of the request URI corresponds with a bundle,
  117. * we will start that bundle. By convention, bundles handle all URIs
  118. * which begin with their bundle name.
  119. */
  120. $bundle = URI::segment(1);
  121. if ( ! is_null($bundle) and Bundle::routable($bundle))
  122. {
  123. Bundle::start($bundle);
  124. }
  125. /**
  126. * Route the request to the proper route in the application. If a
  127. * route is found, the route will be called with the current request
  128. * instance. If no route is found, the 404 response will be returned
  129. * to the browser.
  130. */
  131. if (count(URI::$segments) > 15)
  132. {
  133. throw new \Exception("Invalid request. Too many URI segments.");
  134. }
  135. Request::$route = Routing\Router::route(Request::method(), URI::current());
  136. if ( ! is_null(Request::$route))
  137. {
  138. $response = Request::$route->call();
  139. }
  140. else
  141. {
  142. $response = Response::error('404');
  143. }
  144. /**
  145. * Close the session and write the active payload to persistent
  146. * storage. The session cookie will also be written and if the
  147. * driver is a sweeper, session garbage collection might be
  148. * performed depending on the "sweepage" probability.
  149. */
  150. if (Config::get('session.driver') !== '')
  151. {
  152. Session::save();
  153. }
  154. $response->send();