laravel.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php namespace Laravel;
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Bootstrap The Framework Core
  5. |--------------------------------------------------------------------------
  6. |
  7. | By including this file, the core of the framework will be setup which
  8. | includes the class auto-loader, and the registration of any bundles.
  9. | Basically, once this file has been included, the entire framework
  10. | may be used by the developer.
  11. |
  12. */
  13. require 'core.php';
  14. /*
  15. |--------------------------------------------------------------------------
  16. | Setup Error & Exception Handling
  17. |--------------------------------------------------------------------------
  18. |
  19. | Next we'll register custom handlers for all errors and exceptions so we
  20. | can display a clean error message for all errors, as well as do any
  21. | custom error logging that may be setup by the developer.
  22. |
  23. */
  24. set_exception_handler(function($e)
  25. {
  26. require_once path('sys').'error'.EXT;
  27. Error::exception($e);
  28. });
  29. set_error_handler(function($code, $error, $file, $line)
  30. {
  31. require_once path('sys').'error'.EXT;
  32. Error::native($code, $error, $file, $line);
  33. });
  34. register_shutdown_function(function()
  35. {
  36. require_once path('sys').'error'.EXT;
  37. Error::shutdown();
  38. });
  39. /*
  40. |--------------------------------------------------------------------------
  41. | Report All Errors
  42. |--------------------------------------------------------------------------
  43. |
  44. | By setting error reporting to -1, we essentially force PHP to report
  45. | every error, and this is guaranteed to show every error on future
  46. | releases of PHP. This allows everything to be fixed early!
  47. |
  48. */
  49. error_reporting(-1);
  50. /*
  51. |--------------------------------------------------------------------------
  52. | Start The Application Bundle
  53. |--------------------------------------------------------------------------
  54. |
  55. | The application "bundle" is the default bundle for the installation and
  56. | we'll fire it up first. In this bundle's bootstrap, more configuration
  57. | will take place and the developer can hook into some of the core
  58. | framework events such as the configuration loader.
  59. |
  60. */
  61. Bundle::start(DEFAULT_BUNDLE);
  62. /*
  63. |--------------------------------------------------------------------------
  64. | Auto-Start Other Bundles
  65. |--------------------------------------------------------------------------
  66. |
  67. | Bundles that are used throughout the application may be auto-started
  68. | so they are immediately available on every request without needing
  69. | to explicitly start them within the application.
  70. |
  71. */
  72. foreach (Bundle::$bundles as $bundle => $config)
  73. {
  74. if ($config['auto']) Bundle::start($bundle);
  75. }
  76. /*
  77. |--------------------------------------------------------------------------
  78. | Register The Catch-All Route
  79. |--------------------------------------------------------------------------
  80. |
  81. | This route will catch all requests that do not hit another route in
  82. | the application, and will raise the 404 error event so the error
  83. | can be handled by the developer in their 404 event listener.
  84. |
  85. */
  86. Routing\Router::register('*', '(:all)', function()
  87. {
  88. return Event::first('404');
  89. });
  90. /*
  91. |--------------------------------------------------------------------------
  92. | Gather The URI And Locales
  93. |--------------------------------------------------------------------------
  94. |
  95. | When routing, we'll need to grab the URI and the supported locales for
  96. | the route so we can properly set the language and route the request
  97. | to the proper end-point in the application.
  98. |
  99. */
  100. $uri = URI::current();
  101. $languages = Config::get('application.languages', array());
  102. $languages[] = Config::get('application.language');
  103. /*
  104. |--------------------------------------------------------------------------
  105. | Set The Locale Based On The Route
  106. |--------------------------------------------------------------------------
  107. |
  108. | If the URI starts with one of the supported languages, we will set
  109. | the default lagnauge to match that URI segment and shorten the
  110. | URI we'll pass to the router to not include the lang segment.
  111. |
  112. */
  113. foreach ($languages as $language)
  114. {
  115. if (starts_with($uri, $language))
  116. {
  117. Config::set('application.language', $language);
  118. $uri = trim(substr($uri, strlen($language)), '/'); break;
  119. }
  120. }
  121. if ($uri == '') $uri = '/';
  122. URI::$uri = $uri;
  123. /*
  124. |--------------------------------------------------------------------------
  125. | Route The Incoming Request
  126. |--------------------------------------------------------------------------
  127. |
  128. | Phew! We can finally route the request to the appropriate route and
  129. | execute the route to get the response. This will give an instance
  130. | of the Response object that we can send back to the browser
  131. |
  132. */
  133. Request::$route = Routing\Router::route(Request::method(), $uri);
  134. $response = Request::$route->call();
  135. /*
  136. |--------------------------------------------------------------------------
  137. | "Render" The Response
  138. |--------------------------------------------------------------------------
  139. |
  140. | The render method evaluates the content of the response and converts it
  141. | to a string. This evaluates any views and sub-responses within the
  142. | content and sets the raw string result as the new response.
  143. |
  144. */
  145. $response->render();
  146. /*
  147. |--------------------------------------------------------------------------
  148. | Persist The Session To Storage
  149. |--------------------------------------------------------------------------
  150. |
  151. | If a session driver has been configured, we will save the session to
  152. | storage so it is available for the next request. This will also set
  153. | the session cookie in the cookie jar to be sent to the user.
  154. |
  155. */
  156. if (Config::get('session.driver') !== '')
  157. {
  158. Session::save();
  159. }
  160. /*
  161. |--------------------------------------------------------------------------
  162. | Send The Response To The Browser
  163. |--------------------------------------------------------------------------
  164. |
  165. | We'll send the response back to the browser here. This method will also
  166. | send all of the response headers to the browser as well as the string
  167. | content of the Response. This should make the view available to the
  168. | browser and show something pretty to the user.
  169. |
  170. */
  171. $response->send();
  172. /*
  173. |--------------------------------------------------------------------------
  174. | And We're Done!
  175. |--------------------------------------------------------------------------
  176. |
  177. | Raise the "done" event so extra output can be attached to the response.
  178. | This allows the adding of debug toolbars, etc. to the view, or may be
  179. | used to do some kind of logging by the application.
  180. |
  181. */
  182. Event::fire('laravel.done', array($response));
  183. /*
  184. |--------------------------------------------------------------------------
  185. | Finish the request for PHP-FastCGI
  186. |--------------------------------------------------------------------------
  187. |
  188. | Stopping the PHP process for PHP-FastCGI users to speed up some
  189. | PHP queries. Acceleration is possible when there are actions in the
  190. | process of script execution that do not affect server response.
  191. | For example, saving the session in memcached can occur after the page
  192. | has been formed and passed to a web server.
  193. */
  194. $response->foundation->finish();