Commit d1a969bd authored by Taylor Otwell's avatar Taylor Otwell

added uri class. refactored.

parent 2758b4c1
......@@ -31,29 +31,6 @@ return array(
'log' => false,
/*
|--------------------------------------------------------------------------
| Error Handler
|--------------------------------------------------------------------------
|
| Because of the various ways of managing errors, you get complete freedom
| to manage errors as you desire. Any error that occurs in your application
| will be sent to this Closure.
|
| By default, when error detail is disabled, a generic error page will be
| rendered by the handler. After this handler is complete, the framework
| will stop processing the request and "exit" will be called.
|
*/
'handler' => function($exception, $config)
{
if ( ! $config['detail'])
{
Response::error('500')->send();
}
},
/*
|--------------------------------------------------------------------------
| Error Logger
......@@ -73,11 +50,13 @@ return array(
|
*/
'logger' => function($exception, $config)
'logger' => function($e, $config)
{
$message = date('Y-m-d H:i:s').' - '.$exception->getMessage().PHP_EOL;
$format = '%s | Message: %s | File: %s | Line: %s';
$message = sprintf($format, date('Y-m-d H:i:s'), $e->getMessage(), $e->getFile(), $e->getLine());
File::append(STORAGE_PATH.'log.txt', $message);
File::append(STORAGE_PATH.'log.txt', $message.PHP_EOL);
}
);
\ No newline at end of file
......@@ -58,7 +58,6 @@ require SYS_PATH.'autoloader'.EXT;
*/
Config::load('application');
Config::load('session');
Config::load('error');
/**
* Register the Autoloader's "load" method on the auto-loader stack.
......
<?php namespace Laravel; use Closure;
if (trim(Config::get('application.key')) === '')
if (trim(Config::$items['application']['key']) === '')
{
throw new \Exception('The cookie class may not be used without an application key.');
}
......
......@@ -19,6 +19,8 @@ class IoC {
/**
* Bootstrap the application IoC container.
*
* This method is called automatically the first time the class is loaded.
*
* @param array $registry
* @return void
*/
......@@ -158,4 +160,9 @@ class IoC {
}
/**
* We only bootstrap the IoC container once the class has been
* loaded since there isn't any reason to load the container
* configuration until the class is first requested.
*/
IoC::bootstrap();
\ No newline at end of file
......@@ -30,8 +30,6 @@ $handler = function($exception)
call_user_func($config['logger'], $exception, $config);
}
call_user_func($config['handler'], $exception, $config);
if ($config['detail'])
{
echo "<html><h2>Uncaught Exception</h2>
......@@ -42,6 +40,10 @@ $handler = function($exception)
<h3>Stack Trace:</h3>
<pre>".$exception->getTraceAsString()."</pre></html>";
}
else
{
Response::error('500')->send();
}
};
/**
......@@ -84,19 +86,15 @@ register_shutdown_function(function() use ($handler)
* 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.
*/
error_reporting(-1);
/**
*
* 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.
*/
if ( ! Config::$items['error']['detail'])
{
ini_set('display_errors', 'Off');
}
error_reporting(-1);
ini_set('display_errors', 'Off');
/**
* Load the session and session manager instance. The session
......@@ -122,6 +120,7 @@ if (Config::$items['session']['driver'] !== '')
* we can avoid using the loader for these classes. This saves us
* some overhead on each request.
*/
require SYS_PATH.'uri'.EXT;
require SYS_PATH.'input'.EXT;
require SYS_PATH.'request'.EXT;
require SYS_PATH.'response'.EXT;
......@@ -183,7 +182,7 @@ $router = new Routing\Router($loader, CONTROLLER_PATH);
IoC::instance('laravel.routing.router', $router);
Request::$route = $router->route(Request::method(), Request::uri());
Request::$route = $router->route(Request::method(), URI::current());
if ( ! is_null(Request::$route))
{
......
......@@ -23,9 +23,7 @@ class Redirect extends Response {
*/
public static function to($url, $status = 302, $https = false)
{
$response = new static('', $status);
return $response->header('Location', URL::to($url, $https));
return static::make('', $status)->header('Location', URL::to($url, $https));
}
/**
......
......@@ -27,40 +27,14 @@ class Request {
* Get the URI for the current request.
*
* If the request is to the root of the application, a single forward slash
* will be returned. Otherwise, the URI will be returned without any leading
* or trailing slashes.
* will be returned. Otherwise, the URI will be returned with all of the
* leading and trailing slashes removed.
*
* @return string
*/
public static function uri()
{
if ( ! is_null(static::$uri)) return static::$uri;
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// Remove the root application URL from the request URI. If the application
// is nested within a sub-directory of the web document root, this will get
// rid of all of the the sub-directories from the request URI.
$base = parse_url(Config::$items['application']['url'], PHP_URL_PATH);
if (strpos($uri, $base) === 0)
{
$uri = substr($uri, strlen($base));
}
$index = '/'.Config::$items['application']['index'];
if ($index !== '/' and strpos($uri, $index) === 0)
{
$uri = substr($uri, strlen($index));
}
$uri = trim($uri, '/');
// Format the final request URI. If there is nothing left, we will just
// return a single forward slash. Otherwise, we'll remove all of the
// leading and trailing spaces from the URI before returning it.
return static::$uri = ($uri !== '') ? $uri : '/';
return URI::get();
}
/**
......
......@@ -221,7 +221,10 @@ class Route {
*/
public function __call($method, $parameters)
{
if (strpos($method, 'is_') === 0) return $this->is(substr($method, 3));
if (strpos($method, 'is_') === 0)
{
return $this->is(substr($method, 3));
}
throw new \Exception("Call to undefined method [$method] on Route class.");
}
......
......@@ -217,22 +217,6 @@ class Router {
}
}
/**
* Get the request formats for which the route provides responses.
*
* @param mixed $callback
* @return array
*/
protected function formats($callback)
{
if (is_array($callback) and isset($callback['provides']))
{
return (is_string($provides = $callback['provides'])) ? explode('|', $provides) : $provides;
}
return array('html');
}
/**
* Translate route URI wildcards into actual regular expressions.
*
......
......@@ -5,7 +5,7 @@ class Memcached implements Driver {
/**
* The Memcache cache driver instance.
*
* @var Memcached
* @var Cache\Drivers\Memcached
*/
private $memcached;
......
......@@ -17,7 +17,7 @@ class Str {
{
if (function_exists('mb_strtolower'))
{
return mb_strtolower($value, Config::get('application.encoding'));
return mb_strtolower($value, Config::$items['application']['encoding']);
}
return strtolower($value);
......@@ -38,7 +38,7 @@ class Str {
{
if (function_exists('mb_strtoupper'))
{
return mb_strtoupper($value, Config::get('application.encoding'));
return mb_strtoupper($value, Config::$items['application']['encoding']);
}
return strtoupper($value);
......@@ -59,7 +59,7 @@ class Str {
{
if (function_exists('mb_convert_case'))
{
return mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding'));
return mb_convert_case($value, MB_CASE_TITLE, Config::$items['application']['encoding']);
}
return ucwords(strtolower($value));
......@@ -80,7 +80,7 @@ class Str {
{
if (function_exists('mb_strlen'))
{
return mb_strlen($value, Config::get('application.encoding'));
return mb_strlen($value, Config::$items['application']['encoding']);
}
return strlen($value);
......@@ -108,7 +108,7 @@ class Str {
if (function_exists('mb_substr'))
{
return mb_substr($value, 0, $limit, Config::get('application.encoding')).$end;
return mb_substr($value, 0, $limit, Config::$items['application']['encoding']).$end;
}
return substr($value, 0, $limit).$end;
......
<?php namespace Laravel;
class URI {
/**
* The URI for the current request.
*
* @var string
*/
protected static $uri;
/**
* The URI segments for the current request.
*
* @var array
*/
protected static $segments = array();
/**
* Get the URI for the current request.
*
* If the request is to the root of the application, a single forward slash
* will be returned. Otherwise, the URI will be returned with all of the
* leading and trailing slashes removed.
*
* @return string
*/
public static function current()
{
if ( ! is_null(static::$uri)) return static::$uri;
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// Remove the root application URL from the request URI. If the application
// is nested within a sub-directory of the web document root, this will get
// rid of all of the the sub-directories from the request URI.
$uri = static::remove($uri, parse_url(Config::$items['application']['url'], PHP_URL_PATH));
if (($index = '/'.Config::$items['application']['index']) !== '/')
{
$uri = static::remove($uri, $index);
}
// Format the final request URI. If there is nothing left, we will just
// return a single forward slash. Otherwise, we'll remove all of the
// leading and trailing spaces from the URI before returning it.
static::$uri = $uri = static::format($uri);
static::$segments = explode('/', $uri);
return static::$uri;
}
/**
* Get a specific segment of the request URI via an one-based index.
*
* <code>
* // Get the first segment of the request URI
* $segment = URI::segment(1);
*
* // Get the second segment of the URI, or return a default value
* $segment = URI::segment(2, 'Taylor');
* </code>
*
* @param int $index
* @param mixed $default
* @return string
*/
public static function segment($index, $default = null)
{
static::current();
return Arr::get(static::$segments, $index - 1, $default);
}
/**
* Remove a given value from the URI.
*
* @param string $uri
* @param string $value
* @return string
*/
protected static function remove($uri, $value)
{
if (strpos($uri, $value) === 0)
{
return substr($uri, strlen($value));
}
}
/**
* Format a given URI.
*
* If the URI is an empty string, a single forward slash will be returned.
* Otherwise, we will simply trim the URI's leading and trailing slashes.
*
* @param string $uri
* @return string
*/
protected static function format($uri)
{
return (($uri = trim($uri, '/')) !== '') ? $uri : '/';
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment