Commit 47db2ff1 authored by Taylor Otwell's avatar Taylor Otwell

refactoring, added uri class.

parent 1463617f
......@@ -41,6 +41,7 @@ return array(
'Loader' => 'Laravel\\Facades\\Loader',
'Messages' => 'Laravel\\Validation\\Messages',
'Package' => 'Laravel\\Facades\\Package',
'URI' => 'Laravel\\Facades\\URI',
'URL' => 'Laravel\\Facades\\URL',
'Redirect' => 'Laravel\\Facades\\Redirect',
'Request' => 'Laravel\\Facades\\Request',
......
# Laravel Change Log
## Version 1.5.9
- Fixed bug in Eloquent relationship loading.
### Upgrading from 1.5.8
- Replace **system** directory.
## Version 1.5.8
- Fixed bug in form class that prevent name attributes from being set properly.
......
<?php namespace Laravel;
use Closure;
class Arr {
/**
......@@ -30,7 +32,7 @@ class Arr {
{
if ( ! is_array($array) or ! array_key_exists($segment, $array))
{
return ($default instanceof \Closure) ? call_user_func($default) : $default;
return ($default instanceof Closure) ? call_user_func($default) : $default;
}
$array = $array[$segment];
......@@ -107,7 +109,7 @@ class Arr {
if (call_user_func($callback, $key, $value)) return $value;
}
return ($default instanceof \Closure) ? call_user_func($default) : $default;
return ($default instanceof Closure) ? call_user_func($default) : $default;
}
/**
......
<?php namespace Laravel\Cache\Drivers;
use Laravel\Proxy;
class APC_Engine {
/**
* Retrieve an item from the APC cache.
*
* @param string $key
* @return mixed
*/
public function fetch($key)
{
return apc_fetch($key);
}
/**
* Store an item in the APC cache.
*
* @param string $key
* @param mixed $value
* @param int $seconds
* @return void
*/
public function store($key, $value, $seconds)
{
apc_store($key, $value, $seconds);
}
/**
* Delete an item from the APC cache.
*
* @param string $key
* @return void
*/
public function delete($key)
{
apc_delete($key);
}
}
class APC extends Driver {
/**
* The proxy class instance.
* The APC engine instance.
*
* @var Proxy
* @var APC_Engine
*/
private $proxy;
private $apc;
/**
* The cache key from the cache configuration file.
......@@ -21,14 +58,14 @@ class APC extends Driver {
/**
* Create a new APC cache driver instance.
*
* @param Proxy $proxy
* @param string $key
* @param APC_Engine $apc
* @param string $key
* @return void
*/
public function __construct(Proxy $proxy, $key)
public function __construct(APC_Engine $apc, $key)
{
$this->apc = $apc;
$this->key = $key;
$this->proxy = $proxy;
}
/**
......@@ -50,7 +87,7 @@ class APC extends Driver {
*/
protected function retrieve($key)
{
if ( ! is_null($cache = $this->proxy->apc_fetch($this->key.$key))) return $cache;
if ( ! is_null($cache = $this->apc->fetch($this->key.$key))) return $cache;
}
/**
......@@ -63,7 +100,7 @@ class APC extends Driver {
*/
public function put($key, $value, $minutes)
{
$this->proxy->apc_store($this->key.$key, $value, $minutes * 60);
$this->apc->store($this->key.$key, $value, $minutes * 60);
}
/**
......@@ -74,7 +111,7 @@ class APC extends Driver {
*/
public function forget($key)
{
$this->proxy->apc_delete($this->key.$key);
$this->apc->delete($this->key.$key);
}
}
\ No newline at end of file
This diff is collapsed.
......@@ -69,4 +69,17 @@ spl_autoload_register(array($container->resolve('laravel.loader'), 'load'));
// --------------------------------------------------------------
// Set the application environment configuration option.
// --------------------------------------------------------------
$container->resolve('laravel.config')->set('application.env', $env);
\ No newline at end of file
$container->resolve('laravel.config')->set('application.env', $env);
// --------------------------------------------------------------
// Define some convenient global functions.
// --------------------------------------------------------------
function e($value)
{
return IoC::container()->resolve('laravel.html')->entities($value);
}
function __($key, $replacements = array(), $language = null)
{
return IoC::container()->resolve('laravel.lang')->line($key, $replacements, $language);
}
\ No newline at end of file
......@@ -52,6 +52,7 @@ class Redirect extends Facade { public static $resolve = 'laravel.redirect'; }
class Request extends Facade { public static $resolve = 'laravel.request'; }
class Response extends Facade { public static $resolve = 'laravel.response'; }
class Session extends Facade { public static $resolve = 'laravel.session'; }
class URI extends Facade { public static $resolve = 'laravel.uri'; }
class URL extends Facade { public static $resolve = 'laravel.url'; }
class Validator extends Facade { public static $resolve = 'laravel.validator'; }
class View extends Facade { public static $resolve = 'laravel.view'; }
\ No newline at end of file
......@@ -79,7 +79,7 @@ class File {
*/
public function delete($path)
{
@unlink($path);
if ($this->exists($path)) @unlink($path);
}
/**
......
......@@ -81,7 +81,7 @@ class Form {
$attributes['accept-charset'] = $this->html->encoding;
}
$append = ($method == 'PUT' or $method == 'DELETE') ? $this->hidden('_REQUEST_METHOD', $method) : '';
$append = ($method == 'PUT' or $method == 'DELETE') ? $this->hidden(Request::spoofer, $method) : '';
return '<form'.$this->html->attributes($attributes).'>'.$append.PHP_EOL;
}
......
......@@ -7,14 +7,14 @@ class Loader {
*
* @var array
*/
public $paths;
protected $paths;
/**
* The class aliases defined for the application.
*
* @var array
*/
public $aliases;
protected $aliases;
/**
* Create a new class loader instance.
......@@ -37,8 +37,12 @@ class Loader {
*/
public function load($class)
{
// All Laravel core classes follow a namespace to directory convention. So, we will
// replace all of the namespace slashes with directory slashes.
$file = strtolower(str_replace('\\', '/', $class));
// First, we'll check to determine if an alias exists. If it does, we will define the
// alias and bail out. Aliases are defined for most developer used core classes.
if (array_key_exists($class, $this->aliases)) return class_alias($this->aliases[$class], $class);
foreach ($this->paths as $path)
......@@ -52,4 +56,57 @@ class Loader {
}
}
/**
* Register a class alias with the auto-loader.
*
* Note: Aliases are lazy-loaded, so the aliased class will not be included until it is needed.
*
* <code>
* // Register an alias for the "SwiftMailer\Transport" class
* Loader::alias('Transport', 'SwiftMailer\\Transport');
* </code>
*
* @param string $alias
* @param string $class
* @return void
*/
public function alias($alias, $class)
{
$this->aliases[$alias] = $class;
}
/**
* Register a path with the auto-loader.
*
* The registered path will be searched when auto-loading classes.
*
* <code>
* // Register a path to be searched by the auto-loader
* Loader::path('path/to/files');
* </code>
*
* @param string $path
* @return void
*/
public function path($path)
{
$this->paths[] = rtrim($path, '/').'/';
}
/**
* Remove an alias from the auto-loader's alias registrations.
*
* <code>
* // Remove the "Transport" alias from the registered aliases
* Loader::forget_alias('Transport');
* </code>
*
* @param string $alias
* @return void
*/
public function forget_alias($alias)
{
unset($this->aliases[$alias]);
}
}
\ No newline at end of file
......@@ -2,12 +2,19 @@
class Request {
/**
* The URI for the current request.
*
* @var string
*/
protected $uri;
/**
* The $_SERVER array for the request.
*
* @var array
*/
public $server;
protected $server;
/**
* The $_POST array for the request.
......@@ -17,40 +24,30 @@ class Request {
protected $post;
/**
* The base URL of the application.
* The route handling the current request.
*
* @var string
* @var Routing\Route
*/
protected $url;
public $route;
/**
* The request URI.
*
* After determining the URI once, this property will be set and returned
* on subsequent requests for the URI.
* The request data key that is used to indicate the spoofed request method.
*
* @var string
*/
protected $uri;
/**
* The route handling the current request.
*
* @var Routing\Route
*/
public $route;
const spoofer = '__spoofer';
/**
* Create a new request instance.
*
* @param string $uri
* @param array $server
* @param array $post
* @param string $url
* @return void
*/
public function __construct($server, $post, $url)
public function __construct($uri, $server, $post)
{
$this->url = $url;
$this->uri = $uri;
$this->post = $post;
$this->server = $server;
}
......@@ -69,32 +66,7 @@ class Request {
*/
public function uri()
{
if ( ! is_null($this->uri)) return $this->uri;
if (isset($this->server['PATH_INFO']))
{
$uri = $this->server['PATH_INFO'];
}
elseif (isset($this->server['REQUEST_URI']))
{
$uri = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH);
}
else
{
throw new \Exception('Unable to determine the request URI.');
}
if ($uri === false)
{
throw new \Exception('Malformed request URI. Request terminated.');
}
foreach (array(parse_url($this->url, PHP_URL_PATH), '/index.php') as $value)
{
$uri = (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
}
return $this->uri = (($uri = trim($uri, '/')) == '') ? '/' : $uri;
return $this->uri;
}
/**
......@@ -102,6 +74,14 @@ class Request {
*
* The format is determined by essentially taking the "extension" of the URI.
*
* <code>
* // Returns "html" for a request to "/user/profile"
* $format = Request::format();
*
* // Returns "json" for a request to "/user/profile.json"
* $format = Request::format();
* </code>
*
* @return string
*/
public function format()
......@@ -120,27 +100,62 @@ class Request {
*/
public function method()
{
return ($this->spoofed()) ? $this->post['_REQUEST_METHOD'] : $this->server['REQUEST_METHOD'];
return ($this->spoofed()) ? $this->post[Request::spoofer] : $this->server['REQUEST_METHOD'];
}
/**
* Get an item from the $_SERVER array.
*
* Like most array retrieval methods, a default value may be specified.
*
* <code>
* // Get an item from the $_SERVER array
* $value = Request::server('http_x_requested_for');
*
* // Get an item from the $_SERVER array or return a default value
* $value = Request::server('http_x_requested_for', '127.0.0.1');
* </code>
*
* @param string $key
* @param mixed $default
* @return string
*/
public function server($key = null, $default = null)
{
return Arr::get($this->server, strtoupper($key), $default);
}
/**
* Determine if the request method is being spoofed by a hidden Form element.
*
* Hidden elements are used to spoof PUT and DELETE requests since they are not supported by HTML forms.
* Hidden elements are used to spoof PUT and DELETE requests since they are not supported
* by HTML forms. If the request is being spoofed, Laravel considers the spoofed request
* method the actual request method throughout the framework.
*
* @return bool
*/
public function spoofed()
{
return is_array($this->post) and array_key_exists('REQUEST_METHOD', $this->post);
return is_array($this->post) and array_key_exists(Request::spoofer, $this->post);
}
/**
* Get the requestor's IP address.
*
* A default may be passed and will be returned in the event the IP can't be determined
*
* <code>
* // Get the requestor's IP address
* $ip = Request::ip();
*
* // Get the requestor's IP address or return a default value
* $ip = Request::ip('127.0.0.1');
* </code>
*
* @param mixed $default
* @return string
*/
public function ip()
public function ip($default = '0.0.0.0')
{
if (isset($this->server['HTTP_X_FORWARDED_FOR']))
{
......@@ -154,11 +169,16 @@ class Request {
{
return $this->server['REMOTE_ADDR'];
}
return ($default instanceof \Closure) ? call_user_func($default) : $default;
}
/**
* Get the HTTP protocol for the request.
*
* This method will return either "https" or "http", depending on whether HTTPS
* is being used for the current request.
*
* @return string
*/
public function protocol()
......@@ -167,17 +187,17 @@ class Request {
}
/**
* Determine if the request is using HTTPS.
* Determine if the current request is using HTTPS.
*
* @return bool
*/
public function secure()
{
return ($this->protocol() == 'https');
return $this->protocol() == 'https';
}
/**
* Determine if the request is an AJAX request.
* Determine if the current request is an AJAX request.
*
* @return bool
*/
......
......@@ -72,14 +72,6 @@ class Loader {
{
return require $path;
}
// Even if we didn't find a matching file for the segment, we still want to
// check for a "routes.php" file which could handle the root route and any
// routes that are impossible to handle in an explicitly named file.
if (file_exists($path = str_replace('.php', '/routes.php', $path)))
{
return require $path;
}
}
return array();
......@@ -99,7 +91,6 @@ class Loader {
$routes = array();
// First we will check for the base routes file in the application directory.
if (file_exists($path = $this->base.'routes'.EXT))
{
$routes = array_merge($routes, require $path);
......
......@@ -99,7 +99,10 @@ class Route {
// route is delegating the responsibility for handling the request to a controller.
elseif (is_array($this->callback))
{
$callback = Arr::first($this->callback, function($key, $value) {return $key == 'delegate' or $value instanceof Closure;});
$callback = Arr::first($this->callback, function($key, $value)
{
return $key == 'delegate' or $value instanceof Closure;
});
return ($callback instanceof Closure) ? call_user_func_array($callback, $this->parameters) : new Delegate($callback);
}
......@@ -121,7 +124,12 @@ class Route {
*/
public function filters($name)
{
return (is_array($this->callback) and isset($this->callback[$name])) ? explode(', ', $this->callback[$name]) : array();
if (is_array($this->callback) and isset($this->callback[$name]))
{
return explode(', ', $this->callback[$name]);
}
return array();
}
/**
......
......@@ -65,7 +65,10 @@ class Manager {
// are generated per session to protect against Cross-Site Request Forgery attacks on
// the application. It is up to the developer to take advantage of them using the token
// methods on the Form class and the "csrf" route filter.
if ( ! $payload->has('csrf_token')) $payload->put('csrf_token', Str::random(16));
if ( ! $payload->has('csrf_token'))
{
$payload->put('csrf_token', Str::random(16));
}
return $payload;
}
......
<?php namespace Laravel;
class URI {
/**
* The $_SERVER array for the current request.
*
* @var array
*/
protected $server;
/**
* The application URL as specified in the application configuration file.
*
* @var string
*/
protected $url;
/**
* The URI for the current request.
*
* This property will be set after the URI is detected for the first time.
*
* @var string
*/
protected $uri;
/**
* Create a new URI instance.
*
* @param array $server
* @param string $url
* @return void
*/
public function __construct($server, $url)
{
$this->url = $url;
$this->server = $server;
}
/**
* Get the URI for the current request.
*
* @return string
*/
public function get()
{
if ( ! is_null($this->uri)) return $this->uri;
if (($uri = $this->from_server()) === false)
{
throw new \Exception('Malformed request URI. Request terminated.');
}
return $this->uri = $this->format($this->clean($uri));
}
/**
* Get a given URI segment from the URI for the current request.
*
* <code>
* // Get the first segment from the request URI
* $first = Request::uri()->segment(1);
*
* // Get the second segment or return a default value if it doesn't exist
* $second = Request::uri()->segment(2, 'Taylor');
*
* // Get all of the segments for the request URI
* $segments = Request::uri()->segment();
* </code>
*
* @param int $segment
* @param mixed $default
* @return string
*/
public function segment($segment = null, $default = null)
{
$segments = Arr::without(explode('/', $this->detect()), array(''));
if ( ! is_null($segment)) $segment = $segment - 1;
return Arr::get($segments, $segment, $default);
}
/**
* Get the request URI from the $_SERVER array.
*
* @return string
*/
protected function from_server()
{
// If the PATH_INFO $_SERVER element is set, we will use since it contains
// the request URI formatted perfectly for Laravel's routing engine.
if (isset($this->server['PATH_INFO']))
{
return $this->server['PATH_INFO'];
}
// If the REQUEST_URI is set, we need to extract the URL path since this
// should return the URI formatted in a manner similar to PATH_INFO.
elseif (isset($this->server['REQUEST_URI']))
{
return parse_url($this->server['REQUEST_URI'], PHP_URL_PATH);
}
throw new \Exception('Unable to determine the request URI.');
}
/**
* Remove extraneous segments from the URI such as the URL and index page.
*
* These segments need to be removed since they will cause problems in the
* routing engine if they are present in the URI.
*
* @param string $uri
* @return string
*/
protected function clean($uri)
{
foreach (array(parse_url($this->url, PHP_URL_PATH), '/index.php') as $value)
{
$uri = (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
}
return $uri;
}
/**
* Format the URI.
*
* If the request URI is empty, a single forward slash will be returned.
*
* @param string $uri
* @return string
*/
protected 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