Commit b6ab0b08 authored by Taylor Otwell's avatar Taylor Otwell

moving some laravel classes around, switching alias to reflect changes. added...

moving some laravel classes around, switching alias to reflect changes. added some factories. removed system ioc container config file.
parent b625ebdc
......@@ -112,24 +112,26 @@ return array(
*/
'aliases' => array(
'Arr' => 'Laravel\\Arr',
'Asset' => 'Laravel\\Asset',
'Auth' => 'Laravel\\Security\\Auth',
'Auth' => 'Laravel\\Auth',
'Benchmark' => 'Laravel\\Benchmark',
'Cache' => 'Laravel\\Cache\\Manager',
'Config' => 'Laravel\\Config',
'Controller' => 'Laravel\\Routing\\Controller',
'Cookie' => 'Laravel\\Cookie',
'Crypter' => 'Laravel\\Security\\Crypter',
'Crypter' => 'Laravel\\Crypter',
'DB' => 'Laravel\\Database\\Manager',
'Eloquent' => 'Laravel\\Database\\Eloquent\\Model',
'File' => 'Laravel\\File',
'Form' => 'Laravel\\Form',
'Hash' => 'Laravel\\Security\\Hash',
'Hash' => 'Laravel\\Hash',
'HTML' => 'Laravel\\HTML',
'Inflector' => 'Laravel\\Inflector',
'Input' => 'Laravel\\Input',
'IoC' => 'Laravel\\IoC',
'Lang' => 'Laravel\\Lang',
'Memcached' => 'Laravel\\Memcached',
'Paginator' => 'Laravel\\Paginator',
'URL' => 'Laravel\\URL',
'Redirect' => 'Laravel\\Redirect',
......@@ -139,7 +141,7 @@ return array(
'Section' => 'Laravel\\Section',
'Session' => 'Laravel\\Facades\\Session',
'Str' => 'Laravel\\Str',
'Validator' => 'Laravel\\Validation\\Validator',
'Validator' => 'Laravel\\Validator',
'View' => 'Laravel\\View',
),
......
......@@ -75,6 +75,42 @@ class Arr {
$array[array_shift($keys)] = $value;
}
/**
* Remove an array item from a given array.
*
* The same "dot" syntax used by the "get" method may be used here.
*
* <code>
* // Remove the $array['user']['name'] item from the array
* Arr::forget($array, 'user.name');
* </code>
*
* @param array $array
* @param string $key
* @param mixed $value
* @return void
*/
public static function forget(&$array, $key)
{
if (is_null($key)) return;
$keys = explode('.', $key);
while (count($keys) > 1)
{
$key = array_shift($keys);
if ( ! isset($array[$key]) or ! is_array($array[$key]))
{
return;
}
$array =& $array[$key];
}
unset($array[array_shift($keys)]);
}
/**
* Return the first element in an array which passes a given truth test.
*
......
<?php namespace Laravel\Security;
use Laravel\IoC;
use Laravel\Str;
use Laravel\Config;
use Laravel\Cookie;
use Laravel\Session;
<?php namespace Laravel;
class Auth {
......
......@@ -49,7 +49,6 @@ unset($application, $public, $laravel, $environment);
require SYS_PATH.'arr'.EXT;
require SYS_PATH.'config'.EXT;
require SYS_PATH.'facades'.EXT;
require SYS_PATH.'container'.EXT;
require SYS_PATH.'autoloader'.EXT;
/**
......@@ -61,13 +60,6 @@ Config::load('application');
Config::load('session');
Config::load('error');
/**
* Bootstrap the application inversion of control container. The IoC
* container is responsible for resolving classes, and helps keep the
* framework flexible.
*/
IoC::bootstrap();
/**
* Register the Autoloader's "load" method on the auto-loader stack.
* This method provides the lazy-loading of all class files, as well
......
<?php namespace Laravel\Cache\Drivers;
use Laravel\Config;
class Factory {
/**
* Create a new cache driver instance.
*
* @param string $driver
* @return Driver
*/
public static function make($driver)
{
switch ($driver)
{
case 'apc':
return new APC(Config::get('cache.key'));
case 'file':
return new File(CACHE_PATH);
case 'memcached':
return new Memcached(\Laravel\Memcached::instance(), Config::get('cache.key'));
case 'redis':
return new Redis(\Laravel\Redis::db());
default:
throw new \Exception("Cache driver {$driver} is not supported.");
}
}
}
\ No newline at end of file
<?php namespace Laravel\Cache; use Laravel\IoC, Laravel\Config;
<?php namespace Laravel\Cache;
use Laravel\Redis;
use Laravel\Config;
use Laravel\Memcached;
class Manager {
......@@ -37,7 +41,7 @@ class Manager {
throw new \Exception("Cache driver [$driver] is not supported.");
}
return static::$drivers[$driver] = IoC::core("cache.{$driver}");
return static::$drivers[$driver] = Drivers\Factory::make($driver);
}
return static::$drivers[$driver];
......
<?php namespace Laravel;
return array(
'laravel.routing.router' => array('singleton' => true, 'resolver' => function($c)
{
return new Routing\Router($c->core('routing.loader'), CONTROLLER_PATH);
}),
'laravel.routing.loader' => array('singleton' => true, 'resolver' => function($c)
{
return new Routing\Loader(APP_PATH, ROUTE_PATH);
}),
'laravel.routing.caller' => array('resolver' => function($c)
{
return new Routing\Caller($c, require APP_PATH.'filters'.EXT, CONTROLLER_PATH);
}),
'laravel.database.connectors.sqlite' => array('resolver' => function($c)
{
return new Database\Connectors\SQLite(DATABASE_PATH);
}),
'laravel.database.connectors.mysql' => array('resolver' => function($c)
{
return new Database\Connectors\MySQL;
}),
'laravel.database.connectors.pgsql' => array('resolver' => function($c)
{
return new Database\Connectors\Postgres;
}),
'laravel.cache.apc' => array('resolver' => function($c)
{
return new Cache\Drivers\APC(Config::get('cache.key'));
}),
'laravel.cache.file' => array('resolver' => function($c)
{
return new Cache\Drivers\File(CACHE_PATH);
}),
'laravel.cache.redis' => array('resolver' => function()
{
return new Cache\Drivers\Redis(Redis::db());
}),
'laravel.cache.memcached' => array('resolver' => function($c)
{
return new Cache\Drivers\Memcached($c->core('cache.memcache.connection'), Config::get('cache.key'));
}),
'laravel.cache.memcache.connection' => array('singleton' => true, 'resolver' => function($c)
{
$memcache = new \Memcache;
foreach (Config::get('cache.memcached') as $server)
{
$memcache->addServer($server['host'], $server['port'], true, $server['weight']);
}
if ($memcache->getVersion() === false)
{
throw new \Exception('Could not establish memcached connection. Please verify your memcached configuration.');
}
return $memcache;
}),
'laravel.session.apc' => array('resolver' => function($c)
{
return new Session\Drivers\APC($c->core('cache.apc'));
}),
'laravel.session.cookie' => array('resolver' => function($c)
{
return new Session\Drivers\Cookie;
}),
'laravel.session.database' => array('resolver' => function($c)
{
return new Session\Drivers\Database(Database\Manager::connection());
}),
'laravel.session.file' => array('resolver' => function($c)
{
return new Session\Drivers\File(SESSION_PATH);
}),
'laravel.session.redis' => array('resolver' => function($c)
{
return new Session\Drivers\Redis($c->core('cache.redis'));
}),
'laravel.session.memcached' => array('resolver' => function($c)
{
return new Session\Drivers\Memcached($c->core('cache.memcached'));
}),
);
\ No newline at end of file
<?php namespace Laravel\Security; use Laravel\Config;
<?php namespace Laravel;
if (trim(Config::$items['application']['key']) === '')
{
......
<?php namespace Laravel\Database\Connectors;
class Factory {
/**
* Create a new database connector instance.
*
* @param string $driver
* @return Connector
*/
public static function make($driver)
{
switch ($driver)
{
case 'sqlite':
return new SQLite(DATABASE_PATH);
case 'mysql':
return new MySQL;
case 'pgsql':
return new Postgres;
default:
throw new \Exception("Database driver [$driver] is not supported.");
}
}
}
\ No newline at end of file
......@@ -32,7 +32,7 @@ class Manager {
{
if (is_null($connection)) $connection = Config::get('database.default');
if ( ! array_key_exists($connection, static::$connections))
if ( ! isset(static::$connections[$connection]))
{
$config = Config::get("database.connections.{$connection}");
......@@ -65,7 +65,7 @@ class Manager {
return call_user_func($config['connector'], $config);
}
return IoC::core("database.connectors.{$config['driver']}")->connect($config);
return Connectors\Factory::make($config['driver'])->connect($config);
}
/**
......
<?php namespace Laravel\Security; use Laravel\Str;
<?php namespace Laravel;
class Hash {
......
......@@ -76,6 +76,23 @@ class Input {
}
}
/**
* Flush the old input from the session.
*
* On a successful form submission, the application may redirect to another
* form. If this is the case, it may be necessary to flush the old input
* so that the new form does not have the previous form's data.
*
* @return void
*/
public static function flush()
{
if (Config::$items['session']['driver'] !== '')
{
IoC::core('session')->flash(Input::old_input, array());
}
}
/**
* Determine if the old input data contains an item.
*
......
......@@ -3,100 +3,30 @@
class IoC {
/**
* The active container instance.
*
* @var Container
*/
public static $container;
/**
* Bootstrap the global IoC instance.
*
* @return void
*/
public static function bootstrap()
{
Config::load('container');
static::$container = new Container(Config::$items['container']);
}
/**
* Get the active container instance.
*
* @return Container
*/
public static function container()
{
return static::$container;
}
/**
* Resolve a core Laravel class from the container.
*
* <code>
* // Resolve the "laravel.router" class from the container
* $input = IoC::core('router');
*
* // Equivalent resolution using the "resolve" method
* $input = IoC::resolve('laravel.router');
*
* // Pass an array of parameters to the resolver
* $input = IoC::core('router', array('test'));
* </code>
*
* @param string $name
* @param array $parameters
* @return mixed
*/
public static function core($name, $parameters = array())
{
return static::$container->core($name, $parameters);
}
/**
* Magic Method for calling methods on the active container instance.
*
* <code>
* // Call the "resolve" method on the active container
* $instance = IoC::resolve('laravel.routing.router');
*
* // Call the "instance" method on the active container
* IoC::instance('mailer', new Mailer);
* </code>
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array(static::$container, $method), $parameters);
}
}
class Container {
/**
* The resolved singleton instances.
* The registered dependencies.
*
* @var array
*/
public $singletons = array();
protected static $registry = array();
/**
* The registered dependencies.
* The resolved singleton instances.
*
* @var array
*/
protected $registry = array();
protected static $singletons = array();
/**
* Create a new IoC container instance.
* Bootstrap the application IoC container.
*
* @param array $registry
* @return void
*/
public function __construct($registry = array())
public static function bootstrap($registry = array())
{
$this->registry = $registry;
Config::load('container');
static::$registry = Config::$items['container'];
}
/**
......@@ -114,9 +44,9 @@ class Container {
* @param Closure $resolver
* @return void
*/
public function register($name, $resolver, $singleton = false)
public static function register($name, $resolver, $singleton = false)
{
$this->registry[$name] = array('resolver' => $resolver, 'singleton' => $singleton);
static::$registry[$name] = array('resolver' => $resolver, 'singleton' => $singleton);
}
/**
......@@ -125,9 +55,9 @@ class Container {
* @param string $name
* @return bool
*/
public function registered($name)
public static function registered($name)
{
return array_key_exists($name, $this->registry);
return array_key_exists($name, static::$registry);
}
/**
......@@ -140,9 +70,9 @@ class Container {
* @param Closure $resolver
* @return void
*/
public function singleton($name, $resolver)
public static function singleton($name, $resolver)
{
$this->register($name, $resolver, true);
static::register($name, $resolver, true);
}
/**
......@@ -160,9 +90,9 @@ class Container {
* @param mixed $instance
* @return void
*/
public function instance($name, $instance)
public static function instance($name, $instance)
{
$this->singletons[$name] = $instance;
static::$singletons[$name] = $instance;
}
/**
......@@ -183,9 +113,9 @@ class Container {
* @param array $parameters
* @return mixed
*/
public function core($name, $parameters = array())
public static function core($name, $parameters = array())
{
return $this->resolve("laravel.{$name}", $parameters);
return static::resolve("laravel.{$name}", $parameters);
}
/**
......@@ -203,23 +133,28 @@ class Container {
* @param array $parameters
* @return mixed
*/
public function resolve($name, $parameters = array())
public static function resolve($name, $parameters = array())
{
if (array_key_exists($name, static::$singletons))
{
if (array_key_exists($name, $this->singletons)) return $this->singletons[$name];
return static::$singletons[$name];
}
if ( ! $this->registered($name))
if ( ! static::registered($name))
{
throw new \Exception("Error resolving [$name]. No resolver has been registered in the container.");
}
$object = call_user_func($this->registry[$name]['resolver'], $this, $parameters);
$object = call_user_func(static::$registry[$name]['resolver'], $parameters);
if (isset($this->registry[$name]['singleton']) and $this->registry[$name]['singleton'])
if (isset(static::$registry[$name]['singleton']) and static::$registry[$name]['singleton'])
{
return $this->singletons[$name] = $object;
return static::$singletons[$name] = $object;
}
return $object;
}
}
IoC::bootstrap();
\ No newline at end of file
......@@ -99,11 +99,16 @@ if ( ! Config::$items['error']['detail'])
*/
if (Config::$items['session']['driver'] !== '')
{
$driver = IoC::core('session.'.Config::$items['session']['driver']);
require SYS_PATH.'ioc'.EXT;
require SYS_PATH.'session/payload'.EXT;
require SYS_PATH.'session/drivers/driver'.EXT;
require SYS_PATH.'session/drivers/factory'.EXT;
$id = Cookie::get(Config::$items['session']['cookie']);
IoC::instance('laravel.session', new Session\Manager($driver, $id));
$driver = Session\Drivers\Factory::make(Config::$items['session']['driver']);
IoC::instance('laravel.session', new Session\Payload($driver, $id));
}
/**
......@@ -168,7 +173,11 @@ Routing\Filter::register(require APP_PATH.'filters'.EXT);
list($uri, $method) = array(Request::uri(), Request::method());
Request::$route = IoC::core('routing.router')->route($method, $uri);
$loader = new Routing\Loader(APP_PATH, ROUTE_PATH);
$router = new Routing\Router($loader, CONTROLLER_PATH);
Request::$route = $router->route($method, $uri);
if ( ! is_null(Request::$route))
{
......
<?php namespace Laravel;
class Memcached {
/**
* The Memcached connection instance.
*
* @var Memcache
*/
protected static $instance;
/**
* Get the Memcached connection instance.
*
* This connection will be managed as a singleton instance so that only
* one connection to the Memcached severs will be established.
*
* @return Memcache
*/
public static function instance()
{
if (is_null(static::$instance))
{
static::$instance = static::connect(Config::get('cache.memcached'));
}
return static::$instance;
}
/**
* Create a new Memcached connection instance.
*
* The configuration array passed to this method should be an array of
* server hosts / ports, like those defined in the cache configuration
* file.
*
* <code>
* // Create a new localhost Memcached connection instance.
* $memcache = Memcached::connect(array('host' => '127.0.0.1', 'port' => 11211));
* </code>
*
* @param array $servers
* @return Memcache
*/
public static function connect($servers)
{
$memcache = new \Memcache;
foreach ($servers as $server)
{
$memcache->addServer($server['host'], $server['port'], true, $server['weight']);
}
if ($memcache->getVersion() === false)
{
throw new \Exception('Could not establish memcached connection. Please verify your configuration.');
}
return $memcache;
}
}
\ No newline at end of file
<?php namespace Laravel\Validation;
<?php namespace Laravel;
class Messages {
......
......@@ -38,7 +38,7 @@ class Section {
*/
public static function start($section, $content = '')
{
if ($content !== '')
if ($content == '')
{
ob_start();
......
<?php namespace Laravel\Session\Drivers;
use Laravel\Security\Crypter;
use Laravel\Crypter;
class Cookie implements Driver {
......
<?php namespace Laravel\Session\Drivers;
use Laravel\Cache\Manager as Cache;
class Factory {
/**
* Create a new session driver instance.
*
* @param string $driver
* @return Driver
*/
public static function make($driver)
{
switch ($driver)
{
case 'apc':
return new APC(Cache::driver('apc'));
case 'cookie':
return new Cookie;
case 'database':
return new Database(\Laravel\Database\Manager::connection());
case 'file':
return new File(SESSION_PATH);
case 'memcached':
return new Memcached(Cache::driver('memcached'));
case 'redis':
return new Redis(Cache::driver('redis'));
default:
throw new \Exception("Session driver [$driver] is not supported.");
}
}
}
\ No newline at end of file
<?php namespace Laravel\Session;
use Closure;
use Laravel\Arr;
use Laravel\Str;
use Laravel\Config;
use Laravel\Cookie;
......@@ -12,7 +13,7 @@ if (Config::$items['application']['key'] === '')
throw new \Exception("An application key is required to use sessions.");
}
class Manager {
class Payload {
/**
* The session array that is stored by the driver.
......@@ -42,19 +43,26 @@ class Manager {
$this->session = $driver->load($id);
}
// If the session doesn't exist or is invalid, we will create a new session
// array and mark the session as being non-existent. Some drivers, such as
// the database driver, need to know whether the session exists in storage
// so they can know whether to "insert" or "update" the session.
if (is_null($this->session) or $this->invalid())
{
$this->exists = false;
$this->session = array('id' => Str::random(40), 'data' => array());
$this->session = array('id' => Str::random(40), 'data' => array(
':new:' => array(),
':old:' => array(),
));
}
// A CSRF token is stored in every session. The token is used by the Form
// class and the "csrf" filter to protect the application from cross-site
// request forgery attacks. The token is simply a long, random string
// which should be posted with each request.
if ( ! $this->has('csrf_token'))
{
// A CSRF token is stored in every session. The token is used by the
// Form class and the "csrf" filter to protect the application from
// cross-site request forgery attacks. The token is simply a long,
// random string which should be posted with each request.
$this->put('csrf_token', Str::random(40));
}
}
......@@ -113,12 +121,17 @@ class Manager {
*/
public function get($key, $default = null)
{
foreach (array($key, ':old:'.$key, ':new:'.$key) as $possibility)
if (isset($this->session['data'][$key]))
{
if (array_key_exists($possibility, $this->session['data']))
return $this->session['data'][$key];
}
elseif (isset($this->session['data'][':new:'][$key]))
{
return $this->session['data'][$possibility];
return $this->session['data'][':new:'][$key];
}
elseif (isset($this->session['data'][':old:'][$key]))
{
return $this->session['data'][':old:'][$key];
}
return ($default instanceof Closure) ? call_user_func($default) : $default;
......@@ -133,13 +146,14 @@ class Manager {
*/
public function put($key, $value)
{
$this->session['data'][$key] = $value;
Arr::set($this->session['data'], $key, $value);
}
/**
* Write an item to the session flash data.
*
* Flash data only exists for the next request to the application.
* Flash data only exists for the next request to the application, and is
* useful for storing temporary data such as error or status messages.
*
* @param string $key
* @param mixed $value
......@@ -147,27 +161,17 @@ class Manager {
*/
public function flash($key, $value)
{
$this->put(':new:'.$key, $value);
Arr::set($this->session['data'][':new:'], $key, $value);
}
/**
* Keep all of the session flash data from expiring at the end of the request.
* Keep the session flash data from expiring at the end of the request.
*
* @return void
*/
public function reflash()
{
$flash = array();
foreach ($this->session['data'] as $key => $value)
{
if (strpos($key, ':old:') === 0)
{
$flash[] = str_replace(':old:', '', $key);
}
}
$this->keep($flash);
$this->session['data'][':new:'] += $this->session['data'][':old:'];
}
/**
......@@ -188,11 +192,11 @@ class Manager {
* Remove an item from the session data.
*
* @param string $key
* @return Driver
* @return void
*/
public function forget($key)
{
unset($this->session['data'][$key]);
Arr::forget($this->session['data'], $key);
}
/**
......@@ -250,7 +254,9 @@ class Manager {
// need to determine if garbage collection should be run for the request.
// Since garbage collection can be expensive, the probability of it
// occuring is controlled by the "sweepage" configuration option.
if ($driver instanceof Sweeper and (mt_rand(1, $config['sweepage'][1]) <= $config['sweepage'][0]))
$sweepage = $config['sweepage'];
if ($driver instanceof Sweeper and (mt_rand(1, $sweepage[1]) <= $sweepage[0]))
{
$driver->sweep(time() - ($config['lifetime'] * 60));
}
......@@ -267,21 +273,9 @@ class Manager {
*/
protected function age()
{
foreach ($this->session['data'] as $key => $value)
{
if (strpos($key, ':old:') === 0)
{
$this->forget($key);
}
}
// Now that all of the "old" keys have been removed from the session data,
// we can re-address all of the newly flashed keys to have old addresses.
// The array_combine method uses the first array for keys, and the second
// array for values to construct a single array from both.
$keys = str_replace(':new:', ':old:', array_keys($this->session['data']));
$this->session['data'][':old:'] = $this->session['data'][':new:'];
$this->session['data'] = array_combine($keys, array_values($this->session['data']));
$this->session['data'][':new:'] = array();
}
/**
......
<?php namespace Laravel\Validation;
<?php namespace Laravel;
use Closure;
use Laravel\Arr;
use Laravel\Str;
use Laravel\File;
use Laravel\Lang;
use Laravel\Input;
use Laravel\Database\Manager as DB;
class Validator {
......
......@@ -324,6 +324,16 @@ class View {
unset($this->data[$key]);
}
/**
* Get the evaluated string content of the view.
*
* @return string
*/
public function __toString()
{
return $this->render();
}
/**
* Magic Method for handling the dynamic creation of named views.
*
......
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $severity; ?></title>
<style>
<?php echo file_get_contents(PUBLIC_PATH.'css/laravel.css'); ?>
</style>
<style>
pre {
word-wrap: break-word;
}
</style>
</head>
<body>
<div id="main">
<img src="http://laravel.com/img/splash/error.png" class="marker">
<h1><?php echo $severity; ?></h1>
<h3>Error Message:</h3>
<pre><?php echo $message; ?></pre>
<h3>Stack Trace:</h3>
<?php
$search = array(APP_PATH, SYS_PATH);
$replace = array('APP_PATH/', 'SYS_PATH/');
$trace = str_replace($search, $replace, $exception->getTraceAsString());
?>
<pre style="word-wrap: break-word;"><?php echo $trace; ?></pre>
</div>
</body>
</html>
\ 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