Files
lgp-admin-plus-api/laravel/laravel.php

203 lines
5.3 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-09-22 00:09:53 -05:00
/**
2011-09-29 21:22:48 -05:00
* Bootstrap the core framework components like the IoC container,
* configuration class, and the class auto-loader. Once this file
* has run, the framework is essentially ready for use.
2011-09-22 00:09:53 -05:00
*/
2011-11-22 18:00:17 -06:00
require 'core.php';
2011-11-10 22:46:01 -06:00
/**
* Create the exception logging function. All of the error logging
* is routed through here to avoid duplicate code. This Closure
* will determine if the actual logging Closure should be called.
2011-11-10 22:46:01 -06:00
*/
$logger = function($exception)
2011-11-10 22:46:01 -06:00
{
if (Config::$items['error']['log'])
2011-11-10 22:46:01 -06:00
{
call_user_func(Config::$items['error']['logger'], $exception);
2011-11-10 22:46:01 -06:00
}
};
/**
* Create the exception handler function. All of the error handlers
* registered by the framework call this closure to avoid duplicate
* code. This Closure will pass the exception to the developer
* defined handler in the configuration file.
*/
$handler = function($exception) use ($logger)
{
$logger($exception);
2011-11-10 22:46:01 -06:00
if (Config::$items['error']['detail'])
2011-11-10 22:46:01 -06:00
{
2011-11-15 19:15:31 -06:00
echo "<html><h2>Unhandled Exception</h2>
2011-11-12 00:07:13 -06:00
<h3>Message:</h3>
<pre>".$exception->getMessage()."</pre>
<h3>Location:</h3>
<pre>".$exception->getFile()." on line ".$exception->getLine()."</pre>
<h3>Stack Trace:</h3>
<pre>".$exception->getTraceAsString()."</pre></html>";
2011-11-10 22:46:01 -06:00
}
2011-11-14 21:18:18 -06:00
else
{
Response::error('500')->send();
}
2011-11-15 19:15:31 -06:00
exit(1);
2011-11-10 22:46:01 -06:00
};
/**
* Register the PHP exception handler. The framework throws exceptions
* on every error that cannot be handled. All of those exceptions will
* be sent through this closure for processing.
*/
set_exception_handler(function($exception) use ($handler)
{
$handler($exception);
2011-11-10 22:46:01 -06:00
});
/**
* Register the PHP error handler. All PHP errors will fall into this
* handler, which will convert the error into an ErrorException object
* and pass the exception into the common exception handler. Suppressed
* errors are ignored and errors in the developer configured whitelist
* are silently logged.
2011-11-10 22:46:01 -06:00
*/
2011-11-16 11:46:27 -06:00
set_error_handler(function($code, $error, $file, $line) use ($logger)
2011-11-10 22:46:01 -06:00
{
2011-11-16 11:46:27 -06:00
if (error_reporting() === 0) return;
$exception = new \ErrorException($error, $code, 0, $file, $line);
if (in_array($code, Config::$items['error']['ignore']))
{
2011-11-16 11:46:27 -06:00
return $logger($exception);
}
2011-11-16 11:46:27 -06:00
throw $exception;
2011-11-10 22:46:01 -06:00
});
/**
* Register the PHP shutdown handler. This function will be called
* at the end of the PHP script or on a fatal PHP error. If an error
* has occured, we will convert it to an ErrorException and pass it
* to the common exception handler for the framework.
*/
register_shutdown_function(function() use ($handler)
{
if ( ! is_null($error = error_get_last()))
{
extract($error, EXTR_SKIP);
$handler(new \ErrorException($message, $type, 0, $file, $line));
}
});
/**
* Setting the PHP error reporting level to -1 essentially forces
* PHP to report every error, and is guranteed to show every error
* on future versions of PHP.
2011-11-14 21:18:18 -06:00
*
2011-11-10 22:46:01 -06:00
* If error detail is turned off, we will turn off all PHP error
* reporting and display since the framework will be displaying a
* generic message and we don't want any sensitive details about
* the exception leaking into the views.
*/
2011-11-14 21:18:18 -06:00
error_reporting(-1);
ini_set('display_errors', 'Off');
2011-11-10 22:46:01 -06:00
2011-09-22 00:09:53 -05:00
/**
2011-09-29 21:22:48 -05:00
* Load the session and session manager instance. The session
* payload will be registered in the IoC container as an instance
* so it can be retrieved easily throughout the application.
2011-09-22 00:09:53 -05:00
*/
2011-10-10 21:34:15 -05:00
if (Config::$items['session']['driver'] !== '')
2011-08-26 21:42:04 -05:00
{
$driver = Session\Drivers\Factory::make(Config::$items['session']['driver']);
2011-11-22 07:47:21 -06:00
$session = new Session\Payload($driver);
$session->load(Cookie::get(Config::$items['session']['cookie']));
IoC::instance('laravel.session', $session);
2011-09-08 17:49:16 -05:00
}
2011-08-26 21:42:04 -05:00
2011-10-29 22:31:50 -05:00
/**
* Gather the input to the application based on the current request.
* The input will be gathered based on the current request method and
* will be set on the Input manager.
2011-10-29 22:31:50 -05:00
*/
$input = array();
switch (Request::method())
{
case 'GET':
$input = $_GET;
break;
case 'POST':
$input = $_POST;
break;
case 'PUT':
case 'DELETE':
if (Request::spoofed())
{
$input = $_POST;
}
else
{
parse_str(file_get_contents('php://input'), $input);
}
}
/**
* The spoofed request method is removed from the input so it is not
* unexpectedly included in Input::all() or Input::get(). Leaving it
* in the input array could cause unexpected results if the developer
* fills an Eloquent model with the input.
2011-10-29 22:31:50 -05:00
*/
unset($input[Request::spoofer]);
Input::$input = $input;
2011-10-05 18:32:48 -05:00
/**
* Route the request to the proper route in the application. If a
* route is found, the route will be called with the current request
* instance. If no route is found, the 404 response will be returned
* to the browser.
*/
Routing\Filter::register(require APP_PATH.'filters'.EXT);
$loader = new Routing\Loader(APP_PATH, ROUTE_PATH);
$router = new Routing\Router($loader, CONTROLLER_PATH);
2011-11-11 23:36:31 -06:00
IoC::instance('laravel.routing.router', $router);
2011-11-14 21:18:18 -06:00
Request::$route = $router->route(Request::method(), URI::current());
2011-08-26 21:42:04 -05:00
2011-10-29 22:31:50 -05:00
if ( ! is_null(Request::$route))
2011-08-26 21:42:04 -05:00
{
2011-10-29 22:31:50 -05:00
$response = Request::$route->call();
2011-08-26 21:42:04 -05:00
}
else
{
$response = Response::error('404');
2011-08-26 21:42:04 -05:00
}
2011-09-22 00:09:53 -05:00
/**
2011-09-29 21:22:48 -05:00
* Close the session and write the active payload to persistent
2011-10-31 22:15:47 -05:00
* storage. The session cookie will also be written and if the
* driver is a sweeper, session garbage collection might be
* performed depending on the "sweepage" probability.
2011-09-22 00:09:53 -05:00
*/
2011-10-12 23:15:10 -05:00
if (Config::$items['session']['driver'] !== '')
2011-08-19 20:12:39 -05:00
{
2011-11-21 19:09:48 -06:00
IoC::core('session')->save();
2011-08-19 20:12:39 -05:00
}
2011-11-21 21:57:11 -06:00
$response->send();