From d1a969bd29571a11bafec099d4cfc26800004fb1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 14 Nov 2011 21:18:18 -0600 Subject: [PATCH] added uri class. refactored. --- application/config/error.php | 31 ++------ laravel/bootstrap/core.php | 1 - laravel/cookie.php | 2 +- laravel/ioc.php | 7 ++ laravel/laravel.php | 21 +++--- laravel/redirect.php | 4 +- laravel/request.php | 32 +------- laravel/routing/route.php | 5 +- laravel/routing/router.php | 16 ---- laravel/session/drivers/memcached.php | 2 +- laravel/str.php | 10 +-- laravel/uri.php | 105 ++++++++++++++++++++++++++ 12 files changed, 142 insertions(+), 94 deletions(-) create mode 100644 laravel/uri.php diff --git a/application/config/error.php b/application/config/error.php index a39fcb9c..c63ba57e 100644 --- a/application/config/error.php +++ b/application/config/error.php @@ -31,29 +31,6 @@ '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 @@ | */ - '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'; - File::append(STORAGE_PATH.'log.txt', $message); + $message = sprintf($format, date('Y-m-d H:i:s'), $e->getMessage(), $e->getFile(), $e->getLine()); + + File::append(STORAGE_PATH.'log.txt', $message.PHP_EOL); } ); \ No newline at end of file diff --git a/laravel/bootstrap/core.php b/laravel/bootstrap/core.php index 8442839d..c8378bc0 100644 --- a/laravel/bootstrap/core.php +++ b/laravel/bootstrap/core.php @@ -58,7 +58,6 @@ */ Config::load('application'); Config::load('session'); -Config::load('error'); /** * Register the Autoloader's "load" method on the auto-loader stack. diff --git a/laravel/cookie.php b/laravel/cookie.php index 24a9b0b9..09094ddf 100644 --- a/laravel/cookie.php +++ b/laravel/cookie.php @@ -1,6 +1,6 @@

Uncaught Exception

@@ -42,6 +40,10 @@

Stack Trace:

".$exception->getTraceAsString()."
"; } + else + { + Response::error('500')->send(); + } }; /** @@ -84,19 +86,15 @@ * 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 @@ * 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 @@ 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)) { diff --git a/laravel/redirect.php b/laravel/redirect.php index 884ee5e5..a891b387 100644 --- a/laravel/redirect.php +++ b/laravel/redirect.php @@ -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)); } /** diff --git a/laravel/request.php b/laravel/request.php index 0b6afebb..04f27cc4 100644 --- a/laravel/request.php +++ b/laravel/request.php @@ -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(); } /** diff --git a/laravel/routing/route.php b/laravel/routing/route.php index 3c14dacc..230aa2d2 100644 --- a/laravel/routing/route.php +++ b/laravel/routing/route.php @@ -221,7 +221,10 @@ public function handles($uri) */ 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."); } diff --git a/laravel/routing/router.php b/laravel/routing/router.php index 79c6823e..32e38a83 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -217,22 +217,6 @@ protected function controller_key($segments) } } - /** - * 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. * diff --git a/laravel/session/drivers/memcached.php b/laravel/session/drivers/memcached.php index 5ea208b9..3c7a1305 100644 --- a/laravel/session/drivers/memcached.php +++ b/laravel/session/drivers/memcached.php @@ -5,7 +5,7 @@ class Memcached implements Driver { /** * The Memcache cache driver instance. * - * @var Memcached + * @var Cache\Drivers\Memcached */ private $memcached; diff --git a/laravel/str.php b/laravel/str.php index 8e463ac5..fcb05bea 100644 --- a/laravel/str.php +++ b/laravel/str.php @@ -17,7 +17,7 @@ public static function lower($value) { 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 @@ public static function upper($value) { 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 @@ public static function title($value) { 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 @@ public static function length($value) { 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 @@ public static function limit($value, $limit = 100, $end = '...') 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; diff --git a/laravel/uri.php b/laravel/uri.php new file mode 100644 index 00000000..1cd52a38 --- /dev/null +++ b/laravel/uri.php @@ -0,0 +1,105 @@ + + * // 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'); + * + * + * @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