Commit fee56e0b authored by Taylor Otwell's avatar Taylor Otwell

Merge branch 'develop'

parents 6b77cc98 45cc0f17
local/*
\ No newline at end of file
...@@ -49,7 +49,7 @@ return array( ...@@ -49,7 +49,7 @@ return array(
'logger' => function($severity, $message) 'logger' => function($severity, $message)
{ {
System\File::append(APP_PATH.'storage/log.txt', date('Y-m-d H:i:s').' '.$severity.' - '.$message.PHP_EOL); System\File::append(STORAGE_PATH.'log.txt', date('Y-m-d H:i:s').' '.$severity.' - '.$message.PHP_EOL);
}, },
); );
\ No newline at end of file
Copyright (c) 2011 Taylor Otwell - taylorotwell@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\ No newline at end of file
...@@ -5,18 +5,32 @@ ...@@ -5,18 +5,32 @@
* @package Laravel * @package Laravel
* @version 1.3.0 * @version 1.3.0
* @author Taylor Otwell * @author Taylor Otwell
* @license MIT License
* @link http://laravel.com * @link http://laravel.com
*/ */
// -------------------------------------------------------------- // --------------------------------------------------------------
// Define the framework paths. // Define the core framework paths.
// -------------------------------------------------------------- // --------------------------------------------------------------
define('BASE_PATH', realpath('../').'/');
define('APP_PATH', realpath('../application').'/'); define('APP_PATH', realpath('../application').'/');
define('SYS_PATH', realpath('../system').'/'); define('SYS_PATH', realpath('../system').'/');
define('PUBLIC_PATH', realpath(__DIR__.'/')); define('PUBLIC_PATH', realpath(__DIR__.'/'));
define('BASE_PATH', realpath('../').'/');
// --------------------------------------------------------------
// Define various other framework paths.
// --------------------------------------------------------------
define('CACHE_PATH', APP_PATH.'storage/cache/');
define('CONFIG_PATH', APP_PATH.'config/');
define('DATABASE_PATH', APP_PATH.'storage/db/');
define('LANG_PATH', APP_PATH.'lang/');
define('LIBRARY_PATH', APP_PATH.'libraries/');
define('MODEL_PATH', APP_PATH.'models/');
define('PACKAGE_PATH', APP_PATH.'packages/'); define('PACKAGE_PATH', APP_PATH.'packages/');
define('ROUTE_PATH', APP_PATH.'routes/');
define('SESSION_PATH', APP_PATH.'storage/sessions/');
define('STORAGE_PATH', APP_PATH.'storage/');
define('SYS_VIEW_PATH', SYS_PATH.'views/');
define('VIEW_PATH', APP_PATH.'views/');
// -------------------------------------------------------------- // --------------------------------------------------------------
// Define the PHP file extension. // Define the PHP file extension.
...@@ -32,7 +46,25 @@ require SYS_PATH.'arr'.EXT; ...@@ -32,7 +46,25 @@ require SYS_PATH.'arr'.EXT;
// -------------------------------------------------------------- // --------------------------------------------------------------
// Register the auto-loader. // Register the auto-loader.
// -------------------------------------------------------------- // --------------------------------------------------------------
spl_autoload_register(require SYS_PATH.'loader'.EXT); spl_autoload_register(function($class)
{
$file = strtolower(str_replace('\\', '/', $class));
if (array_key_exists($class, $aliases = System\Config::get('aliases')))
{
return class_alias($aliases[$class], $class);
}
foreach (array(BASE_PATH, MODEL_PATH, LIBRARY_PATH) as $directory)
{
if (file_exists($path = $directory.$file.EXT))
{
require $path;
return;
}
}
});
// -------------------------------------------------------------- // --------------------------------------------------------------
// Set the error reporting and display levels. // Set the error reporting and display levels.
...@@ -91,9 +123,9 @@ $response = System\Route\Filter::call('before', array(), true); ...@@ -91,9 +123,9 @@ $response = System\Route\Filter::call('before', array(), true);
// ---------------------------------------------------------- // ----------------------------------------------------------
if (is_null($response)) if (is_null($response))
{ {
$route = System\Router::route(Request::method(), Request::uri()); $route = System\Router::route(System\Request::method(), System\Request::uri());
$response = (is_null($route)) ? System\Response::make(View::make('error/404'), 404) : $route->call(); $response = (is_null($route)) ? System\Response::make(System\View::make('error/404'), 404) : $route->call();
} }
else else
{ {
......
# Laravel - A Clean & Classy PHP Framework
## For complete documentation: http://laravel.com
\ No newline at end of file
Laravel - A Clean & Classy PHP Framework
For complete documentation: http://laravel.com
Laravel is a clean and classy framework for PHP web development. Freeing you
from spaghetti code, Laravel helps you create wonderful applications using
simple, expressive syntax. Development should be a creative experience that you
enjoy, not something that is painful.
Creator: Taylor Otwell <taylorotwell@gmail.com>
\ No newline at end of file
...@@ -22,12 +22,12 @@ class File implements \System\Cache\Driver { ...@@ -22,12 +22,12 @@ class File implements \System\Cache\Driver {
*/ */
public function get($key) public function get($key)
{ {
if ( ! file_exists(APP_PATH.'storage/cache/'.$key)) if ( ! file_exists(CACHE_PATH.$key))
{ {
return null; return null;
} }
$cache = file_get_contents(APP_PATH.'storage/cache/'.$key); $cache = file_get_contents(CACHE_PATH.$key);
if (time() >= substr($cache, 0, 10)) if (time() >= substr($cache, 0, 10))
{ {
...@@ -49,7 +49,7 @@ class File implements \System\Cache\Driver { ...@@ -49,7 +49,7 @@ class File implements \System\Cache\Driver {
*/ */
public function put($key, $value, $minutes) public function put($key, $value, $minutes)
{ {
file_put_contents(APP_PATH.'storage/cache/'.$key, (time() + ($minutes * 60)).serialize($value), LOCK_EX); file_put_contents(CACHE_PATH.$key, (time() + ($minutes * 60)).serialize($value), LOCK_EX);
} }
/** /**
...@@ -60,7 +60,7 @@ class File implements \System\Cache\Driver { ...@@ -60,7 +60,7 @@ class File implements \System\Cache\Driver {
*/ */
public function forget($key) public function forget($key)
{ {
@unlink(APP_PATH.'storage/cache/'.$key); @unlink(CACHE_PATH.$key);
} }
} }
\ No newline at end of file
...@@ -7,7 +7,7 @@ class Config { ...@@ -7,7 +7,7 @@ class Config {
* *
* @var array * @var array
*/ */
private static $items = array(); public static $items = array();
/** /**
* Determine if a configuration item or file exists. * Determine if a configuration item or file exists.
...@@ -95,14 +95,26 @@ class Config { ...@@ -95,14 +95,26 @@ class Config {
/** /**
* Load all of the configuration items from a file. * Load all of the configuration items from a file.
* *
* If it exists, the configuration file in the application/config directory will be loaded first.
* Any environment specific configuration files will be merged with the root file.
*
* @param string $file * @param string $file
* @return void * @return void
*/ */
public static function load($file) public static function load($file)
{ {
if ( ! array_key_exists($file, static::$items) and file_exists($path = APP_PATH.'config/'.$file.EXT)) if (array_key_exists($file, static::$items)) return;
$config = (file_exists($path = CONFIG_PATH.$file.EXT)) ? require $path : array();
if (isset($_SERVER['LARAVEL_ENV']) and file_exists($path = CONFIG_PATH.$_SERVER['LARAVEL_ENV'].'/'.$file.EXT))
{
$config = array_merge($config, require $path);
}
if (count($config) > 0)
{ {
static::$items[$file] = require $path; static::$items[$file] = $config;
} }
} }
......
...@@ -33,6 +33,19 @@ class DB { ...@@ -33,6 +33,19 @@ class DB {
return static::$connections[$connection]; return static::$connections[$connection];
} }
/**
* Execute a SQL query against the connection and return the first result.
*
* @param string $sql
* @param array $bindings
* @param string $connection
* @return object
*/
public static function first($sql, $bindings = array(), $connection = null)
{
return (count($results = static::query($sql, $bindings, $connection)) > 0) ? $results[0] : null;
}
/** /**
* Execute a SQL query against the connection. * Execute a SQL query against the connection.
* *
...@@ -46,7 +59,7 @@ class DB { ...@@ -46,7 +59,7 @@ class DB {
* @param string $sql * @param string $sql
* @param array $bindings * @param array $bindings
* @param string $connection * @param string $connection
* @return mixed * @return array
*/ */
public static function query($sql, $bindings = array(), $connection = null) public static function query($sql, $bindings = array(), $connection = null)
{ {
......
...@@ -54,7 +54,7 @@ class Connector { ...@@ -54,7 +54,7 @@ class Connector {
*/ */
private static function connect_to_sqlite($config) private static function connect_to_sqlite($config)
{ {
if (file_exists($path = APP_PATH.'storage/db/'.$config->database.'.sqlite')) if (file_exists($path = DATABASE_PATH.$config->database.'.sqlite'))
{ {
return new \PDO('sqlite:'.$path, null, null, static::$options); return new \PDO('sqlite:'.$path, null, null, static::$options);
} }
......
...@@ -202,7 +202,7 @@ abstract class Eloquent { ...@@ -202,7 +202,7 @@ abstract class Eloquent {
$current_page = \System\Paginator::page($total, $per_page); $current_page = \System\Paginator::page($total, $per_page);
return new \System\Paginator($this->for_page($current_page, $per_page)->get(), $total, $per_page); return \System\Paginator::make($this->for_page($current_page, $per_page)->get(), $total, $per_page);
} }
/** /**
......
...@@ -413,7 +413,6 @@ class Query { ...@@ -413,7 +413,6 @@ class Query {
*/ */
public function first($columns = array('*')) public function first($columns = array('*'))
{ {
return (count($results = $this->take(1)->get($columns)) > 0) ? $results[0] : null; return (count($results = $this->take(1)->get($columns)) > 0) ? $results[0] : null;
} }
...@@ -458,15 +457,29 @@ class Query { ...@@ -458,15 +457,29 @@ class Query {
* Get paginated query results. * Get paginated query results.
* *
* @param int $per_page * @param int $per_page
* @param array $columns
* @return Paginator * @return Paginator
*/ */
public function paginate($per_page) public function paginate($per_page, $columns = array('*'))
{ {
$select = $this->select;
$total = $this->count(); $total = $this->count();
// Every query clears the SELECT clause, so we store the contents of the clause
// before executing the count query and then put the contents back in afterwards.
if ( ! is_null($select))
{
$this->select = $select;
}
else
{
$this->select($columns);
}
$current_page = \System\Paginator::page($total, $per_page); $current_page = \System\Paginator::page($total, $per_page);
return new \System\Paginator($this->for_page($current_page, $per_page)->get(), $total, $per_page); return \System\Paginator::make($this->for_page($current_page, $per_page)->get(), $total, $per_page);
} }
/** /**
......
...@@ -35,6 +35,18 @@ class HTML { ...@@ -35,6 +35,18 @@ class HTML {
return '<link href="'.static::entities(URL::to_asset($url)).'" rel="stylesheet" type="text/css" media="'.$media.'">'.PHP_EOL; return '<link href="'.static::entities(URL::to_asset($url)).'" rel="stylesheet" type="text/css" media="'.$media.'">'.PHP_EOL;
} }
/**
* Generate a HTML span.
*
* @param string $value
* @param array $attributes
* @return string
*/
public static function span($value, $attributes = array())
{
return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
}
/** /**
* Generate a HTML link. * Generate a HTML link.
* *
......
...@@ -9,21 +9,21 @@ class Lang { ...@@ -9,21 +9,21 @@ class Lang {
* *
* @var array * @var array
*/ */
private static $lines = array(); public static $lines = array();
/** /**
* The key of the line that is being requested. * The key of the line that is being requested.
* *
* @var string * @var string
*/ */
private $key; public $key;
/** /**
* The place-holder replacements. * The place-holder replacements.
* *
* @var array * @var array
*/ */
private $replacements = array(); public $replacements = array();
/** /**
* Create a new Lang instance. * Create a new Lang instance.
...@@ -117,7 +117,9 @@ class Lang { ...@@ -117,7 +117,9 @@ class Lang {
*/ */
private function load($file, $language) private function load($file, $language)
{ {
if ( ! array_key_exists($language.$file, static::$lines) and file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT)) if (array_key_exists($language.$file, static::$lines)) return;
if (file_exists($path = LANG_PATH.$language.'/'.$file.EXT))
{ {
static::$lines[$language.$file] = require $path; static::$lines[$language.$file] = require $path;
} }
......
<?php
/**
* This function is registered on the auto-loader stack by the front controller.
*
* All namespace slashes will be replaced with directory slashes since all Laravel
* system classes are organized using a namespace to directory convention.
*/
return function($class) {
$file = strtolower(str_replace('\\', '/', $class));
if (array_key_exists($class, $aliases = System\Config::get('aliases')))
{
return class_alias($aliases[$class], $class);
}
if (file_exists($path = BASE_PATH.$file.EXT))
{
require $path;
}
elseif (file_exists($path = APP_PATH.'models/'.$file.EXT))
{
require $path;
}
elseif (file_exists($path = APP_PATH.'libraries/'.$file.EXT))
{
require $path;
}
elseif (file_exists($path = APP_PATH.$file.EXT))
{
require $path;
}
};
\ No newline at end of file
...@@ -31,7 +31,7 @@ class Paginator { ...@@ -31,7 +31,7 @@ class Paginator {
public $per_page; public $per_page;
/** /**
* The last page number. * The last page available for the result set.
* *
* @var int * @var int
*/ */
...@@ -44,34 +44,44 @@ class Paginator { ...@@ -44,34 +44,44 @@ class Paginator {
*/ */
public $language; public $language;
/**
* Indicates if HTTPS links should be generated.
*
* @var bool
*/
public $https = false;
/** /**
* Create a new Paginator instance. * Create a new Paginator instance.
* *
* @param array $results * @param array $results
* @param int $page
* @param int $total * @param int $total
* @param int $per_page * @param int $per_page
* @param int $last_page
* @return void * @return void
*/ */
public function __construct($results, $total, $per_page) public function __construct($results, $page, $total, $per_page, $last_page)
{ {
$this->page = static::page($total, $per_page); $this->last_page = $last_page;
$this->last_page = ceil($total / $per_page);
$this->per_page = $per_page; $this->per_page = $per_page;
$this->results = $results; $this->results = $results;
$this->total = $total; $this->total = $total;
$this->page = $page;
}
/**
* Create a new Paginator instance.
*
* @param array $results
* @param int $total
* @param int $per_page
* @return Paginator
*/
public static function make($results, $total, $per_page)
{
return new static($results, static::page($total, $per_page), $total, $per_page, ceil($total / $per_page));
} }
/** /**
* Get the current page from the request query string. * Get the current page from the request query string.
* *
* The page will be validated and adjusted if it is less than one or greater than the last page. * The page will be validated and adjusted if it is less than one or greater than the last page.
* For example, if the current page is not an integer or less than one, one will be returned.
* If the current page is greater than the last page, the last page will be returned.
* *
* @param int $total * @param int $total
* @param int $per_page * @param int $per_page
...@@ -83,10 +93,10 @@ class Paginator { ...@@ -83,10 +93,10 @@ class Paginator {
if (is_numeric($page) and $page > $last_page = ceil($total / $per_page)) if (is_numeric($page) and $page > $last_page = ceil($total / $per_page))
{ {
return $last_page; return ($last_page > 0) ? $last_page : 1;
} }
return (filter_var($page, FILTER_VALIDATE_INT) === false or $page < 1) ? 1 : $page; return ($page < 1 or filter_var($page, FILTER_VALIDATE_INT) === false) ? 1 : $page;
} }
/** /**
...@@ -110,8 +120,6 @@ class Paginator { ...@@ -110,8 +120,6 @@ class Paginator {
*/ */
private function numbers($adjacent = 3) private function numbers($adjacent = 3)
{ {
// "7" is added to the adjacent range to account for the seven constant elements
// in a slider: the first and last two links, the current page, and the two "..." strings.
return ($this->last_page < 7 + ($adjacent * 2)) ? $this->range(1, $this->last_page) : $this->slider($adjacent); return ($this->last_page < 7 + ($adjacent * 2)) ? $this->range(1, $this->last_page) : $this->slider($adjacent);
} }
...@@ -125,7 +133,7 @@ class Paginator { ...@@ -125,7 +133,7 @@ class Paginator {
{ {
if ($this->page <= $adjacent * 2) if ($this->page <= $adjacent * 2)
{ {
return $this->range(1, 4 + ($adjacent * 2)).$this->ending(); return $this->range(1, 2 + ($adjacent * 2)).$this->ending();
} }
elseif ($this->page >= $this->last_page - ($adjacent * 2)) elseif ($this->page >= $this->last_page - ($adjacent * 2))
{ {
...@@ -144,12 +152,7 @@ class Paginator { ...@@ -144,12 +152,7 @@ class Paginator {
{ {
$text = Lang::line('pagination.previous')->get($this->language); $text = Lang::line('pagination.previous')->get($this->language);
if ($this->page > 1) return ($this->page > 1) ? $this->link($this->page - 1, $text, 'prev_page').' ' : HTML::span($text, array('class' => 'disabled prev_page')).' ';
{
return HTML::link(Request::uri().'?page='.($this->page - 1), $text, array('class' => 'prev_page'), $this->https).' ';
}
return "<span class=\"disabled prev_page\">$text</span> ";
} }
/** /**
...@@ -161,12 +164,7 @@ class Paginator { ...@@ -161,12 +164,7 @@ class Paginator {
{ {
$text = Lang::line('pagination.next')->get($this->language); $text = Lang::line('pagination.next')->get($this->language);
if ($this->page < $this->last_page) return ($this->page < $this->last_page) ? $this->link($this->page + 1, $text, 'next_page') : HTML::span($text, array('class' => 'disabled next_page'));
{
return HTML::link(Request::uri().'?page='.($this->page + 1), $text, array('class' => 'next_page'), $this->https);
}
return "<span class=\"disabled next_page\">$text</span>";
} }
/** /**
...@@ -176,7 +174,7 @@ class Paginator { ...@@ -176,7 +174,7 @@ class Paginator {
*/ */
private function beginning() private function beginning()
{ {
return $this->range(1, 2).'<span class="dots">...</span> '; return $this->range(1, 2).'<span class="dots">...</span>';
} }
/** /**
...@@ -186,7 +184,7 @@ class Paginator { ...@@ -186,7 +184,7 @@ class Paginator {
*/ */
private function ending() private function ending()
{ {
return '<span class="dots">...</span> '.$this->range($this->last_page - 1, $this->last_page); return '<span class="dots">...</span>'.$this->range($this->last_page - 1, $this->last_page);
} }
/** /**
...@@ -204,32 +202,34 @@ class Paginator { ...@@ -204,32 +202,34 @@ class Paginator {
for ($i = $start; $i <= $end; $i++) for ($i = $start; $i <= $end; $i++)
{ {
$pages .= ($this->page == $i) ? "<span class=\"current\">$i</span> " : HTML::link(Request::uri().'?page='.$i, $i, array(), $this->https).' '; $pages .= ($this->page == $i) ? HTML::span($i, array('class' => 'current')).' ' : $this->link($i, $i, null).' ';
} }
return $pages; return $pages;
} }
/** /**
* Set the language that should be used when generating pagination links. * Create a HTML page link.
* *
* @param string $language * @param int $page
* @return Paginator * @param string $text
* @param string $attributes
* @return string
*/ */
public function lang($language) private function link($page, $text, $class)
{ {
$this->language = $language; return HTML::link(Request::uri().'?page='.$page, $text, array('class' => $class), Request::is_secure());
return $this;
} }
/** /**
* Force the pagination links to use HTTPS. * Set the language that should be used when generating page links.
* *
* @param string $language
* @return Paginator * @return Paginator
*/ */
public function secure() public function lang($language)
{ {
$this->https = true; $this->language = $language;
return $this; return $this;
} }
......
...@@ -101,6 +101,18 @@ class Response { ...@@ -101,6 +101,18 @@ class Response {
return new static($content, $status); return new static($content, $status);
} }
/**
* Factory for creating new error response instances.
*
* @param int $code
* @param array $data
* @return Response
*/
public static function error($code, $data = array())
{
return static::make(View::make('error/'.$code, $data), $code);
}
/** /**
* Take a value returned by a route and prepare a Response instance. * Take a value returned by a route and prepare a Response instance.
* *
......
...@@ -3,11 +3,27 @@ ...@@ -3,11 +3,27 @@
class Router { class Router {
/** /**
* All of the loaded routes. * All of the loaded routes keyed by route file.
* *
* @var array * @var array
*/ */
public static $routes; public static $routes = array();
/**
* Simulate a request to a given route. Useful for implementing HMVC.
*
* @param array|string $parameters
* @return Response
*/
public static function call($parameters)
{
$route = static::route('GET', (is_array($parameters)) ? implode('/', $parameters) : (string) $parameters);
if ( ! is_null($route))
{
return $route->call();
}
}
/** /**
* Search a set of routes for the route matching a method and URI. * Search a set of routes for the route matching a method and URI.
...@@ -18,22 +34,19 @@ class Router { ...@@ -18,22 +34,19 @@ class Router {
*/ */
public static function route($method, $uri) public static function route($method, $uri)
{ {
if (is_null(static::$routes)) $routes = static::load($uri);
{
static::$routes = static::load($uri);
}
// Put the request method and URI in route form. // Put the request method and URI in route form.
// Routes begin with the request method and a forward slash. // Routes begin with the request method and a forward slash.
$uri = $method.' /'.trim($uri, '/'); $uri = $method.' /'.trim($uri, '/');
// Is there an exact match for the request? // Is there an exact match for the request?
if (isset(static::$routes[$uri])) if (isset($routes[$uri]))
{ {
return Request::$route = new Route($uri, static::$routes[$uri]); return Request::$route = new Route($uri, $routes[$uri]);
} }
foreach (static::$routes as $keys => $callback) foreach ($routes as $keys => $callback)
{ {
// Only check routes that have multiple URIs or wildcards. // Only check routes that have multiple URIs or wildcards.
// Other routes would have been caught by the check for literal matches. // Other routes would have been caught by the check for literal matches.
...@@ -58,7 +71,7 @@ class Router { ...@@ -58,7 +71,7 @@ class Router {
*/ */
public static function load($uri) public static function load($uri)
{ {
$base = require APP_PATH.'routes'.EXT; $base = (isset(static::$routes[$path = APP_PATH.'routes'.EXT])) ? static::$routes[$path] : static::$routes[$path] = require $path;
return (is_dir(APP_PATH.'routes') and $uri !== '') ? array_merge(static::load_from_directory($uri), $base) : $base; return (is_dir(APP_PATH.'routes') and $uri !== '') ? array_merge(static::load_from_directory($uri), $base) : $base;
} }
...@@ -77,9 +90,13 @@ class Router { ...@@ -77,9 +90,13 @@ class Router {
// Iterate backwards through the URI looking for the deepest matching file. // Iterate backwards through the URI looking for the deepest matching file.
foreach (array_reverse($segments, true) as $key => $value) foreach (array_reverse($segments, true) as $key => $value)
{ {
if (file_exists($path = APP_PATH.'routes/'.implode('/', array_slice($segments, 0, $key + 1)).EXT)) if (isset(static::$routes[$path = ROUTE_PATH.implode('/', array_slice($segments, 0, $key + 1)).EXT]))
{
return static::$routes[$path];
}
elseif (file_exists($path))
{ {
return require $path; return static::$routes[$path] = require $path;
} }
} }
......
...@@ -10,7 +10,7 @@ class File implements \System\Session\Driver { ...@@ -10,7 +10,7 @@ class File implements \System\Session\Driver {
*/ */
public function load($id) public function load($id)
{ {
if (file_exists($path = APP_PATH.'storage/sessions/'.$id)) if (file_exists($path = SESSION_PATH.$id))
{ {
return unserialize(file_get_contents($path)); return unserialize(file_get_contents($path));
} }
...@@ -24,7 +24,7 @@ class File implements \System\Session\Driver { ...@@ -24,7 +24,7 @@ class File implements \System\Session\Driver {
*/ */
public function save($session) public function save($session)
{ {
file_put_contents(APP_PATH.'storage/sessions/'.$session['id'], serialize($session), LOCK_EX); file_put_contents(SESSION_PATH.$session['id'], serialize($session), LOCK_EX);
} }
/** /**
...@@ -35,7 +35,7 @@ class File implements \System\Session\Driver { ...@@ -35,7 +35,7 @@ class File implements \System\Session\Driver {
*/ */
public function delete($id) public function delete($id)
{ {
@unlink(APP_PATH.'storage/sessions/'.$id); @unlink(SESSION_PATH.$id);
} }
/** /**
...@@ -46,7 +46,7 @@ class File implements \System\Session\Driver { ...@@ -46,7 +46,7 @@ class File implements \System\Session\Driver {
*/ */
public function sweep($expiration) public function sweep($expiration)
{ {
foreach (glob(APP_PATH.'storage/sessions/*') as $file) foreach (glob(SESSION_PATH.'*') as $file)
{ {
if (filetype($file) == 'file' and filemtime($file) < $expiration) if (filetype($file) == 'file' and filemtime($file) < $expiration)
{ {
......
...@@ -5,6 +5,8 @@ class URL { ...@@ -5,6 +5,8 @@ class URL {
/** /**
* Generate an application URL. * Generate an application URL.
* *
* If the given URL is already well-formed, it will be returned unchanged.
*
* @param string $url * @param string $url
* @param bool $https * @param bool $https
* @param bool $asset * @param bool $asset
...@@ -12,26 +14,18 @@ class URL { ...@@ -12,26 +14,18 @@ class URL {
*/ */
public static function to($url = '', $https = false, $asset = false) public static function to($url = '', $https = false, $asset = false)
{ {
if (strpos($url, '://') !== false) if (filter_var($url, FILTER_VALIDATE_URL) !== false)
{ {
return $url; return $url;
} }
$base = Config::get('application.url'); $base = Config::get('application.url').'/'.Config::get('application.index');
// If the URL is being generated for a public asset such as an $base = ($asset) ? str_replace('/'.Config::get('application.index'), '', $base) : $base;
// image, we do not want to include "index.php" in the path.
if ( ! $asset)
{
$base .= '/'.Config::get('application.index');
}
if (strpos($base, 'http://') === 0 and $https) $base = ($https and strpos($base, 'http://') === 0) ? 'https://'.substr($base, 7) : $base;
{
$base = 'https://'.substr($base, 7);
}
return rtrim($base, '/').'/'.trim($url, '/'); return rtrim($base, '/').'/'.ltrim($url, '/');
} }
/** /**
...@@ -52,9 +46,9 @@ class URL { ...@@ -52,9 +46,9 @@ class URL {
* @param string $url * @param string $url
* @return string * @return string
*/ */
public static function to_asset($url = '') public static function to_asset($url)
{ {
return static::to($url, false, true); return static::to($url, Request::is_secure(), true);
} }
/** /**
......
...@@ -49,6 +49,25 @@ class View { ...@@ -49,6 +49,25 @@ class View {
return new static($view, $data); return new static($view, $data);
} }
/**
* Create a new named view instance.
*
* @param string $view
* @param array $data
* @return View
*/
public static function of($view, $data = array())
{
$views = Config::get('view.names');
if ( ! array_key_exists($view, $views))
{
throw new \Exception("Named view [$view] is not defined.");
}
return static::make($views[$view], $data);
}
/** /**
* Get the parsed content of the view. * Get the parsed content of the view.
* *
...@@ -82,20 +101,31 @@ class View { ...@@ -82,20 +101,31 @@ class View {
* *
* @return string * @return string
*/ */
private function find() protected function find()
{ {
if (file_exists($path = APP_PATH.'views/'.$this->view.EXT)) if (file_exists($path = VIEW_PATH.$this->view.EXT))
{ {
return $path; return $path;
} }
elseif (file_exists($path = SYS_PATH.'views/'.$this->view.EXT)) elseif (file_exists($path = SYS_VIEW_PATH.$this->view.EXT))
{ {
return $path; return $path;
} }
else
{
throw new \Exception("View [".$this->view."] doesn't exist."); throw new \Exception("View [".$this->view."] doesn't exist.");
} }
/**
* Add a view instance to the view data.
*
* @param string $key
* @param string $view
* @param array $data
* @return View
*/
public function partial($key, $view, $data = array())
{
return $this->bind($key, static::make($view, $data));
} }
/** /**
...@@ -118,14 +148,7 @@ class View { ...@@ -118,14 +148,7 @@ class View {
{ {
if (strpos($method, 'of_') === 0) if (strpos($method, 'of_') === 0)
{ {
$views = Config::get('view.names'); return static::of(substr($method, 3), Arr::get($parameters, 0, array()));
if ( ! array_key_exists($view = substr($method, 3), $views))
{
throw new \Exception("Named view [$view] is not defined.");
}
return static::make($views[$view], (isset($parameters[0]) and is_array($parameters[0])) ? $parameters[0] : array());
} }
} }
......
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
\ 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