laravel.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php namespace System;
  2. // --------------------------------------------------------------
  3. // Define the PHP file extension.
  4. // --------------------------------------------------------------
  5. define('EXT', '.php');
  6. // --------------------------------------------------------------
  7. // Define various other framework paths.
  8. // --------------------------------------------------------------
  9. $constants = array(
  10. 'CACHE_PATH' => STORAGE_PATH.'cache/',
  11. 'CONFIG_PATH' => APP_PATH.'config/',
  12. 'DATABASE_PATH' => STORAGE_PATH.'db/',
  13. 'LANG_PATH' => APP_PATH.'lang/',
  14. 'LIBRARY_PATH' => APP_PATH.'libraries/',
  15. 'MODEL_PATH' => APP_PATH.'models/',
  16. 'ROUTE_PATH' => APP_PATH.'routes/',
  17. 'SCRIPT_PATH' => PUBLIC_PATH.'js/',
  18. 'SESSION_PATH' => STORAGE_PATH.'sessions/',
  19. 'STYLE_PATH' => PUBLIC_PATH.'css/',
  20. 'VIEW_PATH' => APP_PATH.'views/',
  21. );
  22. foreach ($constants as $key => $value)
  23. {
  24. define($key, $value);
  25. }
  26. unset($constants, $system);
  27. // --------------------------------------------------------------
  28. // Load the classes used by the auto-loader.
  29. // --------------------------------------------------------------
  30. require SYS_PATH.'loader'.EXT;
  31. require SYS_PATH.'config'.EXT;
  32. require SYS_PATH.'arr'.EXT;
  33. // --------------------------------------------------------------
  34. // Detect all of the application modules.
  35. // --------------------------------------------------------------
  36. Config::set('application.modules', $modules = array_map('basename', glob(MODULE_PATH.'*', GLOB_ONLYDIR)));
  37. // --------------------------------------------------------------
  38. // Register the auto-loader.
  39. // --------------------------------------------------------------
  40. Loader::bootstrap();
  41. spl_autoload_register(array('System\\Loader', 'load'));
  42. // --------------------------------------------------------------
  43. // Set the error reporting and display levels.
  44. // --------------------------------------------------------------
  45. error_reporting(E_ALL | E_STRICT);
  46. ini_set('display_errors', 'Off');
  47. // --------------------------------------------------------------
  48. // Register the error handlers.
  49. // --------------------------------------------------------------
  50. set_exception_handler(function($e)
  51. {
  52. require_once SYS_PATH.'error'.EXT;
  53. Error::handle($e);
  54. });
  55. set_error_handler(function($number, $error, $file, $line)
  56. {
  57. require_once SYS_PATH.'error'.EXT;
  58. Error::handle(new \ErrorException($error, $number, 0, $file, $line));
  59. });
  60. register_shutdown_function(function()
  61. {
  62. if ( ! is_null($error = error_get_last()))
  63. {
  64. require_once SYS_PATH.'error'.EXT;
  65. Error::handle(new \ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line']));
  66. }
  67. });
  68. // --------------------------------------------------------------
  69. // Set the default timezone.
  70. // --------------------------------------------------------------
  71. date_default_timezone_set(Config::get('application.timezone'));
  72. // --------------------------------------------------------------
  73. // Load the session.
  74. // --------------------------------------------------------------
  75. if (Config::get('session.driver') != '')
  76. {
  77. Session::load(Cookie::get('laravel_session'));
  78. }
  79. // --------------------------------------------------------------
  80. // Load all of the core routing classes.
  81. // --------------------------------------------------------------
  82. require SYS_PATH.'request'.EXT;
  83. require SYS_PATH.'response'.EXT;
  84. require SYS_PATH.'routing/route'.EXT;
  85. require SYS_PATH.'routing/router'.EXT;
  86. require SYS_PATH.'routing/loader'.EXT;
  87. require SYS_PATH.'routing/filter'.EXT;
  88. // --------------------------------------------------------------
  89. // Register the route filters.
  90. // --------------------------------------------------------------
  91. Routing\Filter::register(require APP_PATH.'filters'.EXT);
  92. // --------------------------------------------------------------
  93. // Load the packages that are in the auto-loaded packages array.
  94. // --------------------------------------------------------------
  95. require SYS_PATH.'package'.EXT;
  96. Package::load(Config::get('application.packages'));
  97. // --------------------------------------------------------------
  98. // Execute the global "before" filter.
  99. // --------------------------------------------------------------
  100. $response = Routing\Filter::call('before', array(), true);
  101. // --------------------------------------------------------------
  102. // Route the request and call the appropriate route function.
  103. // --------------------------------------------------------------
  104. if (is_null($response))
  105. {
  106. $path = (in_array($module = Request::segment(1), $modules)) ? MODULE_PATH.$module.'/' : APP_PATH;
  107. if ($path !== APP_PATH)
  108. {
  109. if (file_exists($filters = $path.'filters'.EXT))
  110. {
  111. Routing\Filter::register(require $filters);
  112. }
  113. }
  114. $route = Routing\Router::make(Request::method(), Request::uri(), new Routing\Loader($path))->route();
  115. $response = (is_null($route)) ? Response::error('404') : $route->call();
  116. }
  117. else
  118. {
  119. $response = Response::prepare($response);
  120. }
  121. // --------------------------------------------------------------
  122. // Execute the global "after" filter.
  123. // --------------------------------------------------------------
  124. Routing\Filter::call('after', array($response));
  125. // --------------------------------------------------------------
  126. // Stringify the response.
  127. // --------------------------------------------------------------
  128. $response->content = (string) $response->content;
  129. // --------------------------------------------------------------
  130. // Close the session.
  131. // --------------------------------------------------------------
  132. if (Config::get('session.driver') != '')
  133. {
  134. Session::close();
  135. }
  136. // --------------------------------------------------------------
  137. // Send the response to the browser.
  138. // --------------------------------------------------------------
  139. $response->send();