laravel.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 exception handler.
  27. */
  28. set_error_handler(function($code, $error, $file, $line)
  29. {
  30. Error::native($code, $error, $file, $line);
  31. });
  32. /**
  33. * Register the shutdown handler. This function will be called at the
  34. * end of the PHP script or on a fatal PHP error. If a PHP error has
  35. * occured, we will convert it to an ErrorException and pass it
  36. * to the common exception handler for the framework.
  37. */
  38. register_shutdown_function(function()
  39. {
  40. Error::shutdown();
  41. });
  42. /**
  43. * Setting the PHP error reporting level to -1 essentially forces
  44. * PHP to report every error, and it is guranteed to show every
  45. * error on future versions of PHP.
  46. *
  47. * If error detail is turned off, we will turn off all PHP error
  48. * reporting and display since the framework will be displaying
  49. * a generic message and we do not want any sensitive details
  50. * about the exception leaking into the views.
  51. */
  52. error_reporting(-1);
  53. ini_set('display_errors', 'Off');
  54. /**
  55. * Even though "Magic Quotes" are deprecated in PHP 5.3, they may
  56. * still be enabled on the server. To account for this, we will
  57. * strip slashes on all input arrays if magic quotes are turned
  58. * on for the server environment.
  59. */
  60. if (magic_quotes())
  61. {
  62. $magics = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
  63. foreach ($magics as &$magic)
  64. {
  65. $magic = array_strip_slashes($magic);
  66. }
  67. }
  68. /**
  69. * Load the session using the session manager. The payload will
  70. * be registered in the IoC container as an instance so it can
  71. * be easily access throughout the framework.
  72. */
  73. if (Config::get('session.driver') !== '')
  74. {
  75. Session::start(Config::get('session.driver'));
  76. Session::load(Cookie::get(Config::get('session.cookie')));
  77. IoC::instance('laravel.session', Session::$instance);
  78. }
  79. /**
  80. * Gather the input to the application based on the global input
  81. * variables for the current request. The input will be gathered
  82. * based on the current request method and will be set on the
  83. * Input manager class' static $input property.
  84. */
  85. $input = array();
  86. switch (Request::method())
  87. {
  88. case 'GET':
  89. $input = $_GET;
  90. break;
  91. case 'POST':
  92. $input = $_POST;
  93. break;
  94. case 'PUT':
  95. case 'DELETE':
  96. if (Request::spoofed())
  97. {
  98. $input = $_POST;
  99. }
  100. else
  101. {
  102. parse_str(file_get_contents('php://input'), $input);
  103. if (magic_quotes()) $input = array_strip_slashes($input);
  104. }
  105. }
  106. /**
  107. * The spoofed request method is removed from the input so it is not
  108. * unexpectedly included in Input::all() or Input::get(). Leaving it
  109. * in the input array could cause unexpected results if an Eloquent
  110. * model is filled with the input.
  111. */
  112. unset($input[Request::spoofer]);
  113. Input::$input = $input;
  114. /**
  115. * Load the "application" bundle. Though the application folder is
  116. * not typically considered a bundle, it is started like one and
  117. * essentially serves as the "default" bundle.
  118. */
  119. Bundle::start(DEFAULT_BUNDLE);
  120. /**
  121. * Start all of the bundles that are specified in the configuration
  122. * array of auto-loaded bundles. This lets the developer have an
  123. * easy way to load bundles for every request.
  124. */
  125. foreach (Bundle::all() as $bundle => $config)
  126. {
  127. if ($config['auto']) Bundle::start($bundle);
  128. }
  129. /**
  130. * Route the request to the proper route in the application. If a
  131. * route is found, the route will be called via the request class
  132. * static property. If no route is found, the 404 response will
  133. * be returned to the browser.
  134. */
  135. if (count(URI::$segments) > 15)
  136. {
  137. throw new \Exception("Invalid request. Too many URI segments.");
  138. }
  139. Request::$route = Routing\Router::route(Request::method(), URI::current());
  140. if ( ! is_null(Request::$route))
  141. {
  142. $response = Request::$route->call();
  143. }
  144. else
  145. {
  146. $response = Response::error('404');
  147. }
  148. /**
  149. * Close the session and write the active payload to persistent
  150. * storage. The session cookie will also be written and if the
  151. * driver is a sweeper, session garbage collection might be
  152. * performed depending on the "sweepage" probability.
  153. */
  154. if (Config::get('session.driver') !== '')
  155. {
  156. Session::save();
  157. }
  158. $response->send();