laravel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php namespace Laravel;
  2. // --------------------------------------------------------------
  3. // Define the PHP file extension.
  4. // --------------------------------------------------------------
  5. define('EXT', '.php');
  6. // --------------------------------------------------------------
  7. // Define the core framework paths.
  8. // --------------------------------------------------------------
  9. define('APP_PATH', realpath($application).'/');
  10. define('BASE_PATH', realpath(str_replace('laravel', '', $laravel)).'/');
  11. define('PACKAGE_PATH', realpath($packages).'/');
  12. define('PUBLIC_PATH', realpath($public).'/');
  13. define('STORAGE_PATH', realpath($storage).'/');
  14. define('SYS_PATH', realpath($laravel).'/');
  15. unset($laravel, $application, $config, $packages, $public, $storage);
  16. // --------------------------------------------------------------
  17. // Define various other framework paths.
  18. // --------------------------------------------------------------
  19. define('CACHE_PATH', STORAGE_PATH.'cache/');
  20. define('CONFIG_PATH', APP_PATH.'config/');
  21. define('CONTROLLER_PATH', APP_PATH.'controllers/');
  22. define('DATABASE_PATH', STORAGE_PATH.'database/');
  23. define('LANG_PATH', APP_PATH.'language/');
  24. define('SCRIPT_PATH', PUBLIC_PATH.'js/');
  25. define('SESSION_PATH', STORAGE_PATH.'sessions/');
  26. define('STYLE_PATH', PUBLIC_PATH.'css/');
  27. define('SYS_CONFIG_PATH', SYS_PATH.'config/');
  28. define('SYS_LANG_PATH', SYS_PATH.'language/');
  29. define('VIEW_PATH', APP_PATH.'views/');
  30. // --------------------------------------------------------------
  31. // Load the classes used by the auto-loader.
  32. // --------------------------------------------------------------
  33. require SYS_PATH.'loader'.EXT;
  34. require SYS_PATH.'config'.EXT;
  35. require SYS_PATH.'arr'.EXT;
  36. // --------------------------------------------------------------
  37. // Register the auto-loader.
  38. // --------------------------------------------------------------
  39. Loader::bootstrap(Config::get('aliases'), array(APP_PATH.'libraries/', APP_PATH.'models/'));
  40. spl_autoload_register(array('Laravel\\Loader', 'load'));
  41. // --------------------------------------------------------------
  42. // Bootstrap the IoC container.
  43. // --------------------------------------------------------------
  44. require SYS_PATH.'ioc'.EXT;
  45. IoC::bootstrap(Config::get('container'));
  46. // --------------------------------------------------------------
  47. // Set the error reporting and display levels.
  48. // --------------------------------------------------------------
  49. error_reporting(E_ALL | E_STRICT);
  50. ini_set('display_errors', 'Off');
  51. // --------------------------------------------------------------
  52. // Register the error / exception handlers.
  53. // --------------------------------------------------------------
  54. $error_dependencies = function()
  55. {
  56. require_once SYS_PATH.'exception/handler'.EXT;
  57. require_once SYS_PATH.'exception/examiner'.EXT;
  58. require_once SYS_PATH.'file'.EXT;
  59. };
  60. set_exception_handler(function($e) use ($error_dependencies)
  61. {
  62. call_user_func($error_dependencies);
  63. Exception\Handler::make(new Exception\Examiner($e, new File))->handle();
  64. });
  65. set_error_handler(function($number, $error, $file, $line) use ($error_dependencies)
  66. {
  67. call_user_func($error_dependencies);
  68. $e = new \ErrorException($error, $number, 0, $file, $line);
  69. Exception\Handler::make(new Exception\Examiner($e, new File))->handle();
  70. });
  71. register_shutdown_function(function() use ($error_dependencies)
  72. {
  73. if ( ! is_null($error = error_get_last()))
  74. {
  75. call_user_func($error_dependencies);
  76. $e = new \ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line']);
  77. Exception\Handler::make(new Exception\Examiner($e, new File))->handle();
  78. }
  79. });
  80. // --------------------------------------------------------------
  81. // Set the default timezone.
  82. // --------------------------------------------------------------
  83. date_default_timezone_set(Config::get('application.timezone'));
  84. // --------------------------------------------------------------
  85. // Load all of the core routing and response classes.
  86. // --------------------------------------------------------------
  87. require SYS_PATH.'input'.EXT;
  88. require SYS_PATH.'request'.EXT;
  89. require SYS_PATH.'response'.EXT;
  90. require SYS_PATH.'routing/route'.EXT;
  91. require SYS_PATH.'routing/router'.EXT;
  92. require SYS_PATH.'routing/handler'.EXT;
  93. // --------------------------------------------------------------
  94. // Initialize the request instance for the request.
  95. // --------------------------------------------------------------
  96. $request = new Request($_SERVER);
  97. IoC::container()->instance('laravel.request', $request);
  98. // --------------------------------------------------------------
  99. // Hydrate the input for the current request.
  100. // --------------------------------------------------------------
  101. $request->input = new Input($request->method(), $request->is_spoofed(), $_GET, $_POST, $_FILES, new Cookie($_COOKIE));
  102. // --------------------------------------------------------------
  103. // Load the session.
  104. // --------------------------------------------------------------
  105. if (Config::get('session.driver') != '')
  106. {
  107. Session\Manager::driver()->start($request->input->cookies->get('laravel_session'));
  108. }
  109. // --------------------------------------------------------------
  110. // Load the packages that are in the auto-loaded packages array.
  111. // --------------------------------------------------------------
  112. if (count(Config::get('application.packages')) > 0)
  113. {
  114. require SYS_PATH.'package'.EXT;
  115. Package::load(Config::get('application.packages'));
  116. }
  117. // --------------------------------------------------------------
  118. // Route the request and get the response from the route.
  119. // --------------------------------------------------------------
  120. $route = IoC::container()->resolve('laravel.routing.router')->route();
  121. $response = ( ! is_null($route)) ? IoC::container()->resolve('laravel.routing.handler')->handle($route) : new Error('404');
  122. // --------------------------------------------------------------
  123. // Stringify the response.
  124. // --------------------------------------------------------------
  125. $response->content = $response->render();
  126. // --------------------------------------------------------------
  127. // Close the session.
  128. // --------------------------------------------------------------
  129. if (Config::get('session.driver') != '')
  130. {
  131. $driver = Session\Manager::driver();
  132. $driver->flash('laravel_old_input', $request->input->get());
  133. $driver->close($request->input->cookies);
  134. if ($driver instanceof Session\Sweeper and mt_rand(1, 100) <= 2)
  135. {
  136. $driver->sweep(time() - (Config::get('session.lifetime') * 60));
  137. }
  138. }
  139. // --------------------------------------------------------------
  140. // Send the response to the browser.
  141. // --------------------------------------------------------------
  142. $response->send();