more refactoring and dependency injection.

This commit is contained in:
Taylor Otwell 2011-09-06 22:04:52 -05:00
parent 0c2834fed1
commit 7eef380d8a
39 changed files with 560 additions and 501 deletions

View File

@ -39,7 +39,12 @@
| |
*/ */
'home.index' => array('name' => 'home', function($view) 'global' => function($view, $laravel)
{
//
},
'home.index' => array('name' => 'home', function($view, $laravel)
{ {
// //
}), }),

View File

@ -20,33 +20,33 @@
'Arr' => 'Laravel\\Arr', 'Arr' => 'Laravel\\Arr',
'Asset' => 'Laravel\\Asset', 'Asset' => 'Laravel\\Asset',
'Auth' => 'Laravel\\Security\\Authenticator_Facade', 'Auth' => 'Laravel\\Facades\\Auth',
'Benchmark' => 'Laravel\\Benchmark', 'Benchmark' => 'Laravel\\Benchmark',
'Cache' => 'Laravel\\Cache\\Manager_Facade', 'Cache' => 'Laravel\\Facades\\Cache',
'Config' => 'Laravel\\Config_Facade', 'Config' => 'Laravel\\Facades\\Config',
'Controller' => 'Laravel\\Controller', 'Controller' => 'Laravel\\Controller',
'Cookie' => 'Laravel\\Cookie_Facade', 'Cookie' => 'Laravel\\Facades\\Cookie',
'Crypter' => 'Laravel\\Security\\Crypter_Facade', 'Crypter' => 'Laravel\\Facades\\Crypter',
'DB' => 'Laravel\\Database\\Manager_Facade', 'DB' => 'Laravel\\Facades\\DB',
'Download' => 'Laravel\\Download_Facade', 'Download' => 'Laravel\\Facades\\Download',
'Eloquent' => 'Laravel\\Database\\Eloquent\\Model', 'Eloquent' => 'Laravel\\Database\\Eloquent\\Model',
'File' => 'Laravel\\File_Facade', 'File' => 'Laravel\\Facades\\File',
'Form' => 'Laravel\\Form_Facade', 'Form' => 'Laravel\\Facades\\Form',
'Hasher' => 'Laravel\\Security\\Hashing\\Hasher_Facade', 'Hasher' => 'Laravel\\Facades\\Hasher',
'HTML' => 'Laravel\\HTML_Facade', 'HTML' => 'Laravel\\Facades\\HTML',
'Inflector' => 'Laravel\\Inflector', 'Inflector' => 'Laravel\\Inflector',
'Input' => 'Laravel\\Input_Facade', 'Input' => 'Laravel\\Facades\\Input',
'IoC' => 'Laravel\\IoC', 'IoC' => 'Laravel\\IoC',
'Lang' => 'Laravel\\Lang_Facade', 'Lang' => 'Laravel\\Lang',
'Loader' => 'Laravel\\Loader_Facade', 'Loader' => 'Laravel\\Facades\\Loader',
'Package' => 'Laravel\\Package_Facade', 'Package' => 'Laravel\\Facades\\Package',
'URL' => 'Laravel\\URL_Facade', 'URL' => 'Laravel\\Facades\\URL',
'Redirect' => 'Laravel\\Redirect_Facade', 'Redirect' => 'Laravel\\Facades\\Redirect',
'Request' => 'Laravel\\Request_Facade', 'Request' => 'Laravel\\Facades\\Request',
'Response' => 'Laravel\\Response_Facade', 'Response' => 'Laravel\\Response',
'Session' => 'Laravel\\Session\\Manager_Facade', 'Session' => 'Laravel\\Facades\\Session',
'Str' => 'Laravel\\Str', 'Str' => 'Laravel\\Str',
'Validator' => 'Laravel\\Validation\\Validator_Facade', 'Validator' => 'Laravel\\Facades\\Validator',
'View' => 'Laravel\\View_Facade', 'View' => 'Laravel\\View',
); );

View File

@ -1,8 +1,5 @@
<?php <?php
use Laravel\Application;
use Laravel\Response;
return array( return array(
/* /*
@ -45,13 +42,13 @@
| |
*/ */
'before' => function() 'before' => function($laravel)
{ {
// Do stuff before every request to your application. // Do stuff before every request to your application.
}, },
'after' => function(Response $response) 'after' => function($laravel, $response)
{ {
// Do stuff after every request to your application. // Do stuff after every request to your application.
}, },

View File

@ -16,28 +16,28 @@
| |
| Here is how to respond to a simple GET request to http://example.com/hello: | Here is how to respond to a simple GET request to http://example.com/hello:
| |
| 'GET /hello' => function() | 'GET /hello' => function($laravel)
| { | {
| return 'Hello World!'; | return 'Hello World!';
| } | }
| |
| You can even respond to more than one URI: | You can even respond to more than one URI:
| |
| 'GET /hello, GET /world' => function() | 'GET /hello, GET /world' => function($laravel)
| { | {
| return 'Hello World!'; | return 'Hello World!';
| } | }
| |
| It's easy to allow URI wildcards using the (:num) or (:any) place-holders: | It's easy to allow URI wildcards using the (:num) or (:any) place-holders:
| |
| 'GET /hello/(:any)' => function($name) | 'GET /hello/(:any)' => function($laravel, $name)
| { | {
| return "Welcome, $name."; | return "Welcome, $name.";
| } | }
| |
*/ */
'GET /' => function() 'GET /' => function($laravel)
{ {
return View::make('home.index'); return View::make('home.index');
}, },

View File

@ -35,7 +35,7 @@
// -------------------------------------------------------------- // --------------------------------------------------------------
// Load the configuration manager and its dependencies. // Load the configuration manager and its dependencies.
// -------------------------------------------------------------- // --------------------------------------------------------------
require SYS_PATH.'facade'.EXT; require SYS_PATH.'facades'.EXT;
require SYS_PATH.'loader'.EXT; require SYS_PATH.'loader'.EXT;
require SYS_PATH.'config'.EXT; require SYS_PATH.'config'.EXT;
require SYS_PATH.'arr'.EXT; require SYS_PATH.'arr'.EXT;
@ -62,6 +62,6 @@
IoC::$container = $container; IoC::$container = $container;
// -------------------------------------------------------------- // --------------------------------------------------------------
// Load the auto-loader. // Register the auto-loader on the stack.
// -------------------------------------------------------------- // --------------------------------------------------------------
spl_autoload_register(array($container->loader, 'load')); spl_autoload_register(array($container->loader, 'load'));

View File

@ -1,14 +1,7 @@
<?php namespace Laravel\Cache; <?php namespace Laravel\Cache;
use Laravel\Facade;
use Laravel\Container; use Laravel\Container;
class Manager_Facade extends Facade {
public static $resolve = 'cache';
}
class Manager { class Manager {
/** /**

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class Config_Facade extends Facade {
public static $resolve = 'config';
}
class Config { class Config {
/** /**

View File

@ -158,7 +158,19 @@
'laravel.routing.caller' => array('resolver' => function($container) 'laravel.routing.caller' => array('resolver' => function($container)
{ {
return new Routing\Caller($container, CONTROLLER_PATH); return new Routing\Caller($container, $container->resolve('laravel.routing.filterer'), $container->resolve('laravel.routing.delegator'));
}),
'laravel.routing.filterer' => array('resolver' => function($container)
{
return new Routing\Filterer(require APP_PATH.'filters'.EXT);
}),
'laravel.routing.delegator' => array('resolver' => function($container)
{
return new Routing\Delegator($container, CONTROLLER_PATH);
}), }),
@ -188,7 +200,7 @@
'laravel.validator' => array('resolver' => function($container) 'laravel.validator' => array('resolver' => function($container)
{ {
return new Validation\Validator($container->resolve('laravel.lang')); return new Validation\Validator_Factory($container->resolve('laravel.lang'));
}), }),
@ -202,7 +214,7 @@
'laravel.view.composer' => array('resolver' => function($container) 'laravel.view.composer' => array('resolver' => function($container)
{ {
return new View_Composer(require APP_PATH.'composers'.EXT); return new View_Composer($container, require APP_PATH.'composers'.EXT);
}), }),
/* /*

View File

@ -31,4 +31,12 @@ public function __call($method, $parameters)
return $this->container->resolve('laravel.response')->error('404'); return $this->container->resolve('laravel.response')->error('404');
} }
/**
* Magic Method for retrieving items out of the IoC container.
*/
public function __get($key)
{
return $this->container->$key;
}
} }

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class Cookie_Facade extends Facade {
public static $resolve = 'cookie';
}
class Cookie { class Cookie {
/** /**

View File

@ -1,13 +1,5 @@
<?php namespace Laravel\Database; <?php namespace Laravel\Database;
use Laravel\Facade;
class Manager_Facade extends Facade {
public static $resolve = 'database';
}
class Manager { class Manager {
/** /**

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class Download_Facade extends Facade {
public static $resolve = 'download';
}
class Download extends Response { class Download extends Response {
/** /**
@ -43,7 +37,7 @@ public function of($path, $name = null, $headers = array())
'Content-Type' => $this->mime($this->file->extension($path)), 'Content-Type' => $this->mime($this->file->extension($path)),
'Content-Disposition' => 'attachment; filename="'.$name.'"', 'Content-Disposition' => 'attachment; filename="'.$name.'"',
'Content-Transfer-Encoding' => 'binary', 'Content-Transfer-Encoding' => 'binary',
'Expires' = => 0, 'Expires' => 0,
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Pragma' => 'public', 'Pragma' => 'public',
'Content-Length' => $this->file-size($path), 'Content-Length' => $this->file-size($path),

View File

@ -1,19 +0,0 @@
<?php namespace Laravel;
abstract class Facade {
/**
* Magic Method for passing methods to a class registered in the IoC container.
* This provides a convenient method of accessing functions on classes that
* could not otherwise be accessed staticly.
*
* Facades allow Laravel to still have a high level of dependency injection
* and testability while still accomodating the common desire to conveniently
* use classes via static methods.
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array(IoC::container()->resolve('laravel.'.static::$resolve), $method), $parameters);
}
}

41
laravel/facades.php Normal file
View File

@ -0,0 +1,41 @@
<?php namespace Laravel\Facades;
use Laravel\IoC;
abstract class Facade {
/**
* Magic Method for passing methods to a class registered in the IoC container.
* This provides a convenient method of accessing functions on classes that
* could not otherwise be accessed staticly.
*
* Facades allow Laravel to still have a high level of dependency injection
* and testability while still accomodating the common desire to conveniently
* use classes via static methods.
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array(IoC::container()->resolve('laravel.'.static::$resolve), $method), $parameters);
}
}
class Auth extends Facade { public static $resolve = 'auth'; }
class Cache extends Facade { public static $resolve = 'cache'; }
class Config extends Facade { public static $resolve = 'config'; }
class Cookie extends Facade { public static $resolve = 'cookie'; }
class Crypter extends Facade { public static $resolve = 'crypter'; }
class DB extends Facade { public static $resolve = 'database'; }
class Download extends Facade { public static $resolve = 'download'; }
class File extends Facade { public static $resolve = 'file'; }
class Form extends Facade { public static $resolve = 'form'; }
class Hasher extends Facade { public static $resolve = 'hasher'; }
class HTML extends Facade { public static $resolve = 'html'; }
class Input extends Facade { public static $resolve = 'input'; }
class Loader extends Facade { public static $resolve = 'loader'; }
class Package extends Facade { public static $resolve = 'package'; }
class Redirect extends Facade { public static $resolve = 'redirect'; }
class Request extends Facade { public static $resolve = 'request'; }
class Session extends Facade { public static $resolve = 'session'; }
class URL extends Facade { public static $resolve = 'url'; }
class Validator extends Facade { public static $resolve = 'validator'; }

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class File_Facade extends Facade {
public static $resolve = 'file';
}
class File { class File {
/** /**

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class Form_Facade extends Facade {
public static $resolve = 'form';
}
class Form { class Form {
/** /**
@ -73,7 +67,7 @@ public function open($action = null, $method = 'POST', $attributes = array(), $h
if ( ! array_key_exists('accept-charset', $attributes)) if ( ! array_key_exists('accept-charset', $attributes))
{ {
$attributes['accept-charset'] = Config::get('application.encoding'); $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_METHOD', $method) : '';

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class HTML_Facade extends Facade {
public static $resolve = 'html';
}
class HTML { class HTML {
/** /**
@ -13,7 +7,7 @@ class HTML {
* *
* @var string * @var string
*/ */
private $encoding; public $encoding;
/** /**
* The URL generator instance. * The URL generator instance.

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class Input_Facade extends Facade {
public static $resolve = 'input';
}
class Input { class Input {
/** /**

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class Lang_Facade extends Facade {
public static $resolve = 'lang';
}
class Lang_Factory { class Lang_Factory {
/** /**
@ -25,6 +19,9 @@ class Lang_Factory {
/** /**
* Create a new language factory instance. * Create a new language factory instance.
* *
* Note: The entire configuration manager is used in case the default language
* is changed during the course of a request to the application.
*
* @param Config $config * @param Config $config
* @param array $paths * @param array $paths
* @return void * @return void
@ -105,6 +102,18 @@ public function __construct($key, $replacements, $language, $paths)
$this->replacements = $replacements; $this->replacements = $replacements;
} }
/**
* Create a new Lang instance.
*
* @param string $key
* @param array $replacements
* @return Lang
*/
public static function line($key, $replacements = array())
{
return IoC::container()->resolve('laravel.lang')->line($key, $replacements);
}
/** /**
* Get the language line. * Get the language line.
* *

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class Loader_Facade extends Facade {
public static $resolve = 'loader';
}
class Loader { class Loader {
/** /**
@ -44,7 +38,7 @@ public function __construct($aliases, $paths)
*/ */
public function load($class) public function load($class)
{ {
$file = strtolower(str_replace(array('\\', '_Facade'), array('/', ''), $class)); $file = strtolower(str_replace('\\', '/', $class));
if (array_key_exists($class, $this->aliases)) if (array_key_exists($class, $this->aliases))
{ {

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class Package_Facade extends Facade {
public static $resolve = 'package';
}
class Package { class Package {
/** /**

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class Redirect_Facade extends Facade {
public static $resolve = 'redirect';
}
class Redirect extends Response { class Redirect extends Response {
/** /**
@ -18,7 +12,6 @@ class Redirect extends Response {
/** /**
* Create a new redirect generator instance. * Create a new redirect generator instance.
* *
* @param Session\Driver $session
* @param URL $url * @param URL $url
* @return void * @return void
*/ */

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class Request_Facade extends Facade {
public static $resolve = 'request';
}
class Request { class Request {
/** /**
@ -192,37 +186,10 @@ public function ajax()
} }
/** /**
* Determine if the route handling the request has a given name. * Get the route handling the current request.
* *
* <code> * @return Route
* // Determine if the route handling the request is named "profile"
* $profile = Request::active()->route_is('profile');
* </code>
*
* @param string $name
* @return bool
*/ */
public function route_is($name) public function route() { return $this->route; }
{
if (is_null($this->route) or ! is_array($this->route->callback) or ! isset($this->route->callback['name'])) return false;
return $this->route->callback['name'] === $name;
}
/**
* Magic Method to handle dynamic method calls to determine the route handling the request.
*
* <code>
* // Determine if the route handling the request is named "profile"
* $profile = Request::active()->route_is_profile();
* </code>
*/
public function __call($method, $parameters)
{
if (strpos($method, 'route_is_') === 0)
{
return $this->route_is(substr($method, 9));
}
}
} }

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class Response_Facade extends Facade {
public static $resolve = 'response';
}
class Response_Factory { class Response_Factory {
/** /**
@ -230,4 +224,12 @@ public function status($status)
return $this; return $this;
} }
/**
* Magic Method for calling methods on the response factory instance.
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array(IoC::container()->resolve('laravel.response'), $method), $parameters);
}
} }

View File

@ -14,23 +14,32 @@ class Caller {
protected $container; protected $container;
/** /**
* The path to the application controllers. * The route filterer instance.
* *
* @var string * @var Filterer
*/ */
protected $controller_path; protected $filterer;
/**
* The route delegator instance.
*
* @var Delegator
*/
protected $delegator;
/** /**
* Create a new route caller instance. * Create a new route caller instance.
* *
* @param Container $container * @param Container $container
* @param string $controller_path * @param Filterer $filterer
* @param Delegator $delegator
* @return void * @return void
*/ */
public function __construct(Container $container, $controller_path) public function __construct(Container $container, Filterer $filterer, Delegator $delegator)
{ {
$this->filterer = $filterer;
$this->container = $container; $this->container = $container;
$this->controller_path = $controller_path; $this->delegator = $delegator;
} }
/** /**
@ -46,116 +55,41 @@ public function call(Route $route)
throw new \Exception('Invalid route defined for URI ['.$route->key.']'); throw new \Exception('Invalid route defined for URI ['.$route->key.']');
} }
// Run the "before" filters for the route. If a before filter returns a value, that value if ( ! is_null($response = $this->before($route)))
// will be considered the response to the request and the route function / controller will
// not be used to handle the request.
$before = array_merge($route->before(), array('before'));
if ( ! is_null($response = $this->filter($route, $before, array(), true)))
{ {
return $this->finish($route, $response); return $this->finish($route, $response);
} }
$closure = ( ! $route->callback instanceof Closure) ? $this->find_route_closure($route) : $route->callback; if ( ! is_null($response = $route->call($this->container)))
{
if (is_array($response)) $response = $this->delegator->delegate($route, $response);
if ( ! is_null($closure)) return $this->handle_closure($route, $closure); return $this->finish($route, $response);
}
return $this->finish($route, $this->container->resolve('laravel.response')->error('404')); return $this->finish($route, $this->container->response->error('404'));
} }
/** /**
* Extract the route closure from the route. * Run the "before" filters for the route.
* *
* If a "do" index is specified on the callback, that is the handler. * If a before filter returns a value, that value will be considered the response to the
* Otherwise, we will return the first callable array value. * request and the route function / controller will not be used to handle the request.
* *
* @param Route $route * @param Route $route
* @return Closure
*/
protected function find_route_closure(Route $route)
{
if (isset($route->callback['do'])) return $route->callback['do'];
foreach ($route->callback as $value) { if ($value instanceof Closure) return $value; }
}
/**
* Handle a route closure.
*
* @param Route $route
* @param Closure $closure
* @return mixed * @return mixed
*/ */
protected function handle_closure(Route $route, Closure $closure) protected function before(Route $route)
{ {
$response = call_user_func_array($closure, $route->parameters); $before = array_merge(array('before'), $route->filters('before'));
// If the route closure returns an array, we assume that they are returning a return $this->filterer->filter($before, array($this->container), true);
// reference to a controller and method and will use the given controller method
// to handle the request to the application.
if (is_array($response))
{
$response = $this->delegate($route, $response[0], $response[1], $route->parameters);
}
return $this->finish($route, $response);
}
/**
* Handle the delegation of a route to a controller method.
*
* @param Route $route
* @param string $controller
* @param string $method
* @param array $parameters
* @return Response
*/
protected function delegate(Route $route, $controller, $method, $parameters)
{
if ( ! file_exists($path = $this->controller_path.strtolower(str_replace('.', '/', $controller)).EXT))
{
throw new \Exception("Controller [$controller] does not exist.");
}
require $path;
$controller = $this->resolve_controller($controller);
if ($method == 'before' or strncmp($method, '_', 1) === 0)
{
$response = $this->container->resolve('laravel.response')->error('404');
}
else
{
$response = $controller->before();
}
// Again, as was the case with route closures, if the controller "before" method returns
// a response, it will be considered the response to the request and the controller method
// will not be used to handle the request to the application.
return (is_null($response)) ? call_user_func_array(array($controller, $method), $parameters) : $response;
}
/**
* Resolve a controller name to a controller instance.
*
* @param string $controller
* @return Controller
*/
protected function resolve_controller($controller)
{
if ($this->container->registered('controllers.'.$controller)) return $this->container->resolve('controllers.'.$controller);
$controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
return new $controller;
} }
/** /**
* Finish the route handling for the request. * Finish the route handling for the request.
* *
* The route response will be converted to a Response instance and the "after" filters * The route response will be converted to a Response instance and the "after" filters will be run.
* defined for the route will be executed.
* *
* @param Route $route * @param Route $route
* @param mixed $response * @param mixed $response
@ -165,33 +99,9 @@ protected function finish(Route $route, $response)
{ {
if ( ! $response instanceof Response) $response = new Response($response); if ( ! $response instanceof Response) $response = new Response($response);
$this->filter($route, array_merge($route->after(), array('after')), array($response)); $this->filterer->filter(array_merge($route->filters('after'), array('after')), array($this->container, $response));
return $response; return $response;
} }
/**
* Call a filter or set of filters.
*
* @param Route $route
* @param array $filters
* @param array $parameters
* @param bool $override
* @return mixed
*/
protected function filter(Route $route, $filters, $parameters = array(), $override = false)
{
foreach ((array) $filters as $filter)
{
if ( ! isset($route->filters[$filter])) continue;
$response = call_user_func_array($route->filters[$filter], $parameters);
// "Before" filters may override the request cycle. For example, an authentication
// filter may redirect a user to a login view if they are not logged in. Because of
// this, we will return the first filter response if overriding is enabled.
if ( ! is_null($response) and $override) return $response;
}
}
} }

View File

@ -0,0 +1,117 @@
<?php namespace Laravel\Routing;
use Laravel\Container;
class Delegator {
/**
* The IoC container instance.
*
* @var Container
*/
protected $container;
/**
* The path to the application controllers.
*
* @var string
*/
protected $path;
/**
* Create a new route delegator instance.
*
* @param Container $container
* @param string $path
* @return void
*/
public function __construct(Container $container, $path)
{
$this->path = $path;
$this->container = $container;
}
/**
* Handle the delegation of a route to a controller method.
*
* @param Route $route
* @param array $delegate
* @return mixed
*/
public function delegate(Route $route, $delegate)
{
list($controller, $method) = array($delegate[0], $delegate[1]);
$controller = $this->resolve($controller);
// If the controller doesn't exist or the request is to an invalid method, we will
// return the 404 error response. The "before" method and any method beginning with
// an underscore are not publicly available.
if (is_null($controller) or ($method == 'before' or strncmp($method, '_', 1) === 0))
{
return $this->container->response->error('404');
}
$controller->container = $this->container;
// Again, as was the case with route closures, if the controller "before" method returns
// a response, it will be considered the response to the request and the controller method
// will not be used to handle the request to the application.
$response = $controller->before();
return (is_null($response)) ? call_user_func_array(array($controller, $method), $route->parameters) : $response;
}
/**
* Resolve a controller name to a controller instance.
*
* @param string $controller
* @return Controller
*/
protected function resolve($controller)
{
if ( ! $this->load($controller)) return;
if ($this->container->registered('controllers.'.$controller))
{
return $this->container->resolve('controllers.'.$controller);
}
$controller = $this->format($controller);
return new $controller;
}
/**
* Load the file for a given controller.
*
* @param string $controller
* @return bool
*/
protected function load($controller)
{
if (file_exists($path = $this->path.strtolower(str_replace('.', '/', $controller)).EXT))
{
require $path;
return true;
}
return false;
}
/**
* Format a controller name to its class name.
*
* All controllers are suffixed with "_Controller" to avoid namespacing. It gives the developer
* a more convenient environment since the controller sits in the global namespace.
*
* @param string $controller
* @return string
*/
protected function format($controller)
{
return str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
}
}

View File

@ -0,0 +1,46 @@
<?php namespace Laravel\Routing;
class Filterer {
/**
* All of the route filters for the application.
*
* @var array
*/
protected $filters = array();
/**
* Create a new route filterer instance.
*
* @param array $filters
* @return void
*/
public function __construct($filters)
{
$this->filters = $filters;
}
/**
* Call a filter or set of filters.
*
* @param array $filters
* @param array $parameters
* @param bool $override
* @return mixed
*/
public function filter($filters, $parameters = array(), $override = false)
{
foreach ((array) $filters as $filter)
{
if ( ! isset($this->filters[$filter])) continue;
$response = call_user_func_array($this->filters[$filter], $parameters);
// "Before" filters may override the request cycle. For example, an authentication
// filter may redirect a user to a login view if they are not logged in. Because of
// this, we will return the first filter response if overriding is enabled.
if ( ! is_null($response) and $override) return $response;
}
}
}

View File

@ -1,5 +1,8 @@
<?php namespace Laravel\Routing; <?php namespace Laravel\Routing;
use Closure;
use Laravel\Container;
class Route { class Route {
/** /**
@ -30,13 +33,6 @@ class Route {
*/ */
public $parameters; public $parameters;
/**
* The route filters for the application.
*
* @param array $filters
*/
public $filters = array();
/** /**
* Create a new Route instance. * Create a new Route instance.
* *
@ -45,51 +41,120 @@ class Route {
* @param array $parameters * @param array $parameters
* @return void * @return void
*/ */
public function __construct($key, $callback, $parameters) public function __construct($key, $callback, $parameters = array())
{ {
$this->key = $key; $this->key = $key;
$this->callback = $callback; $this->callback = $callback;
$this->parameters = $parameters; $this->parameters = $parameters;
$this->uris = $this->parse($key);
// Extract each URI handled by the URI. These will be used to find the route by
// URI when requested. The leading slash will be removed for convenience.
foreach (explode(', ', $key) as $segment)
{
$segment = substr($segment, strpos($segment, ' ') + 1);
$this->uris[] = ($segment !== '/') ? trim($segment, '/') : $segment;
}
} }
/** /**
* Get all of the "before" filters defined for the route. * Call the route closure.
* *
* @return array * If no closure is defined for the route, null will be returned. The IoC container instance will be
*/ * passed to the route closure so it has access to all of the framework components.
public function before()
{
return $this->filters('before');
}
/**
* Get all of the "after" filters defined for the route.
* *
* @return array * @param Container $container
* @return mixed
*/ */
public function after() public function call(Container $container)
{ {
return $this->filters('after'); if (is_null($closure = $this->find_closure())) return;
return call_user_func_array($closure, array_merge($this->parameters, array($container)));
} }
/** /**
* Get an array of filters defined for the route. * Extract the route closure from the route.
*
* @return Closure|null
*/
protected function find_closure()
{
if ($this->callback instanceof Closure) return $this->callback;
if (isset($this->callback['do'])) return $this->callback['do'];
foreach ($this->callback as $value) { if ($value instanceof Closure) return $value; }
}
/**
* Get an array of filter names defined for a route.
* *
* @param string $name * @param string $name
* @return array * @return array
*/ */
private function filters($name) public function filters($name)
{ {
return (is_array($this->callback) and isset($this->callback[$name])) ? explode(', ', $this->callback[$name]) : array(); return (is_array($this->callback) and isset($this->callback[$name])) ? explode(', ', $this->callback[$name]) : array();
} }
/**
* Determine if the route handling has a given name.
*
* @param string $name
* @return bool
*/
public function is($name)
{
if ( ! is_array($this->callback) or ! isset($this->callback['name'])) return false;
return $this->callback['name'] === $name;
}
/**
* Determine if the route handles a given URI.
*
* @param string $uri
* @return bool
*/
public function handles($uri)
{
return in_array($uri, $this->uris);
}
/**
* Parse the route key and return an array of URIs the route responds to.
*
* @param string $key
* @return array
*/
protected function parse($key)
{
if (strpos($key, ', ') === false) return array($this->extract($key));
foreach (explode(', ', $key) as $segment)
{
$uris[] = $this->extract($segment);
}
return $uris;
}
/**
* Extract the URI from a route destination.
*
* Route destinations include the request method the route responds to, so this method
* will only remove it from the string. Unless the URI is root, the forward slash will
* be removed to make searching the URIs more convenient.
*
* @param string $segment
* @return string
*/
protected function extract($segment)
{
$segment = substr($segment, strpos($segment, ' ') + 1);
return ($segment !== '/') ? trim($segment, '/') : $segment;
}
/**
* Magic Method to handle dynamic method calls to determine the name of the route.
*/
public function __call($method, $parameters)
{
if (strpos($method, 'is_') === 0) { return $this->is(substr($method, 3)); }
}
} }

View File

@ -111,7 +111,7 @@ public function route()
} }
} }
return $this->route_to_controller(); return $this->request->route = $this->route_to_controller();
} }
/** /**
@ -123,6 +123,8 @@ public function route()
*/ */
protected function route_to_controller() protected function route_to_controller()
{ {
if ($this->request->uri() === '/') return new Route($this->request->method().' /', function() { return array('home', 'index'); });
$segments = explode('/', trim($this->request->uri(), '/')); $segments = explode('/', trim($this->request->uri(), '/'));
if ( ! is_null($key = $this->controller_key($segments))) if ( ! is_null($key = $this->controller_key($segments)))
@ -147,7 +149,7 @@ protected function route_to_controller()
// were they to code the controller delegation manually. // were they to code the controller delegation manually.
$callback = function() use ($controller, $method) { return array($controller, $method); }; $callback = function() use ($controller, $method) { return array($controller, $method); };
return new Route($controller, $callback, $segments); return new Route($this->request->method().' /'.$this->request->uri(), $callback, $segments);
} }
} }

View File

@ -1,15 +1,8 @@
<?php namespace Laravel\Security; <?php namespace Laravel\Security;
use Laravel\IoC; use Laravel\IoC;
use Laravel\Facade;
use Laravel\Session\Driver; use Laravel\Session\Driver;
class Authenticator_Facade extends Facade {
public static $resolve = 'auth';
}
class Authenticator { class Authenticator {
/** /**

View File

@ -1,14 +1,5 @@
<?php namespace Laravel\Security; <?php namespace Laravel\Security;
use Laravel\IoC;
use Laravel\Facade;
class Crypter_Facade extends Facade {
public static $resolve = 'crypter';
}
class Crypter { class Crypter {
/** /**

View File

@ -1,13 +1,5 @@
<?php namespace Laravel\Security\Hashing; <?php namespace Laravel\Security\Hashing;
use Laravel\Facade;
class Hasher_Facade extends Facade {
public static $resolve = 'hasher';
}
class Hasher { class Hasher {
/** /**
@ -15,31 +7,21 @@ class Hasher {
* *
* @var Hash\Engine * @var Hash\Engine
*/ */
public $engine; protected $engine;
/** /**
* Create a new Hasher instance. * Create a new Hasher instance.
* *
* If no hashing engine is provided, the BCrypt engine will be used.
*
* @param Engine $engine * @param Engine $engine
* @return void * @return void
*/ */
public function __construct(Engine $engine = null) public function __construct(Engine $engine)
{ {
$this->engine = (is_null($engine)) ? new BCrypt(10, false) : $engine; $this->engine = $engine
} }
/** /**
* Magic Method for delegating method calls to the hashing engine. * Magic Method for delegating method calls to the hashing engine.
*
* <code>
* // Use the hashing engine to has a value
* $hash = Hasher::make()->hash('password');
*
* // Equivalent method using the engine property
* $hash = Hasher::make()->engine->hash('password');
* </code>
*/ */
public function __call($method, $parameters) public function __call($method, $parameters)
{ {
@ -48,11 +30,6 @@ public function __call($method, $parameters)
/** /**
* Magic Method for performing methods on the default hashing engine. * Magic Method for performing methods on the default hashing engine.
*
* <code>
* // Hash a value using the default hashing engine
* $hash = Hasher::hash('password');
* </code>
*/ */
public static function __callStatic($method, $parameters) public static function __callStatic($method, $parameters)
{ {

View File

@ -1,14 +1,7 @@
<?php namespace Laravel\Session; <?php namespace Laravel\Session;
use Laravel\Facade;
use Laravel\Container; use Laravel\Container;
class Manager_Facade extends Facade {
public static $resolve = 'session';
}
class Manager { class Manager {
/** /**

View File

@ -1,11 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class URL_Facade extends Facade {
public static $resolve = 'url';
}
class URL { class URL {
/** /**
@ -76,8 +70,9 @@ public function to_asset($url, $https = null)
/** /**
* Generate a URL from a route name. * Generate a URL from a route name.
* *
* For routes that have wildcard parameters, an array may be passed as the second parameter to the method. * For routes that have wildcard parameters, an array may be passed as the second
* The values of this array will be used to fill the wildcard segments of the route URI. * parameter to the method. The values of this array will be used to fill the
* wildcard segments of the route URI.
* *
* Optional parameters will be convereted to spaces if no parameter values are specified. * Optional parameters will be convereted to spaces if no parameter values are specified.
* *

View File

@ -2,12 +2,40 @@
use Laravel\IoC; use Laravel\IoC;
use Laravel\Str; use Laravel\Str;
use Laravel\Facade;
use Laravel\Lang_Factory; use Laravel\Lang_Factory;
class Validator_Facade extends Facade { class Validator_Factory {
public static $resolve = 'validator'; /**
* The language factory instance.
*
* @var Lang_Factory
*/
protected $lang;
/**
* Create a new validator factory instance.
*
* @param Lang_Factory $lang
* @return void
*/
public function __construct(Lang_Factory $lang)
{
$this->lang = $lang;
}
/**
* Create a new validator instance.
*
* @param array $attributes
* @param array $rules
* @param array $messages
* @return Validator
*/
public function make($attributes, $rules, $messages = array())
{
return new Validator($this->lang, $attributes, $rules, $messages);
}
} }
@ -72,34 +100,31 @@ class Validator {
/** /**
* Create a new validator instance. * Create a new validator instance.
* *
* @param Lang $lang * @param Lang_Factory $lang
* @param array $attributes
* @param array $rules
* @param array $messages
* @return void * @return void
*/ */
public function __construct(Lang_Factory $lang) public function __construct(Lang_Factory $lang, $attributes, $rules, $messages = array())
{ {
$this->lang = $lang; $this->lang = $lang;
$this->rules = $rules;
$this->messages = $messages;
$this->attributes = $attributes;
} }
/** /**
* Set the attributes, rules, and messages for the validator. * Create a new validator instance.
* *
* @param array $attributes * @param array $attributes
* @param array $rules * @param array $rules
* @param array $messages * @param array $messages
* @return Validator * @return Validator
*/ */
public function of($attributes, $rules, $messages = array()) public static function make($attributes, $rules, $messages = array())
{ {
foreach ($rules as $key => &$rule) return IoC::container()->resolve('laravel.validator')->make($attributes, $rules, $messages);
{
$rule = (is_string($rule)) ? explode('|', $rule) : $rule;
}
$this->attributes = $attributes;
$this->messages = $messages;
$this->rules = $rules;
return $this;
} }
/** /**

View File

@ -1,70 +1,5 @@
<?php namespace Laravel; <?php namespace Laravel;
class View_Facade extends Facade {
public static $resolve = 'view';
}
/**
* The view composer class is responsible for calling the composer on a view and
* searching through the view composers for a given view name. It is injected
* into the View_Factory and View instances themselves, and is managed as a singleton
* by the application IoC container.
*/
class View_Composer {
/**
* The view composers.
*
* @var array
*/
protected $composers;
/**
* Create a new view composer instance.
*
* @param array $composers
* @return void
*/
public function __construct($composers)
{
$this->composers = $composers;
}
/**
* Find the key for a view by name.
*
* @param string $name
* @return string
*/
public function name($name)
{
foreach ($this->composers as $key => $value)
{
if ($name === $value or (isset($value['name']) and $name === $value['name'])) { return $key; }
}
}
/**
* Call the composer for the view instance.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
if (isset($this->composers[$view->view]))
{
foreach ((array) $this->composers[$view->view] as $key => $value)
{
if ($value instanceof \Closure) return call_user_func($value, $view);
}
}
}
}
/** /**
* The view factory class is responsible for the instantiation of Views. It is typically * The view factory class is responsible for the instantiation of Views. It is typically
* access through the application instance from a route or controller, and is managed * access through the application instance from a route or controller, and is managed
@ -108,7 +43,7 @@ public function __construct(View_Composer $composer, $path)
*/ */
public function make($view, $data = array()) public function make($view, $data = array())
{ {
return new View($view, $data, $this->path($view), $this->composer, $this); return new View($this, $this->composer, $view, $data, $this->path($view));
} }
/** /**
@ -122,7 +57,7 @@ protected function of($name, $data = array())
{ {
if ( ! is_null($view = $this->composer->name($name))) if ( ! is_null($view = $this->composer->name($name)))
{ {
return new View($view, $data, $this->path($view), $this->composer, $this); return $this->make($view, $data);
} }
throw new \Exception("Named view [$name] is not defined."); throw new \Exception("Named view [$name] is not defined.");
@ -152,6 +87,76 @@ public function __call($method, $parameters)
} }
/**
* The view composer class is responsible for calling the composer on a view and
* searching through the view composers for a given view name. It is injected
* into the View_Factory and View instances themselves, and is managed as a singleton
* by the application IoC container.
*/
class View_Composer {
/**
* The IoC container instance.
*
* @var Container
*/
protected $container;
/**
* The view composers.
*
* @var array
*/
protected $composers;
/**
* Create a new view composer instance.
*
* @param Container $container
* @param array $composers
* @return void
*/
public function __construct(Container $container, $composers)
{
$this->container = $container;
$this->composers = $composers;
}
/**
* Find the key for a view by name.
*
* @param string $name
* @return string
*/
public function name($name)
{
foreach ($this->composers as $key => $value)
{
if ($name === $value or (isset($value['name']) and $name === $value['name'])) { return $key; }
}
}
/**
* Call the composer for the view instance.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
if (isset($this->composers['global'])) call_user_func($this->composers['global'], $view, $this->container);
if (isset($this->composers[$view->view]))
{
foreach ((array) $this->composers[$view->view] as $key => $value)
{
if ($value instanceof \Closure) return call_user_func($value, $view, $this->container);
}
}
}
}
/** /**
* The view class is returned by the View Factory "make" method, and is the primary * The view class is returned by the View Factory "make" method, and is the primary
* class for working with individual views. It provides methods for binding data to * class for working with individual views. It provides methods for binding data to
@ -197,14 +202,14 @@ class View {
/** /**
* Create a new view instance. * Create a new view instance.
* *
* @param View_Factory $factory
* @param View_Composer $composer
* @param string $view * @param string $view
* @param array $data * @param array $data
* @param string $path * @param string $path
* @param View_Composer $composer
* @param View_Factory $factory
* @return void * @return void
*/ */
public function __construct($view, $data, $path, View_Composer $composer, View_Factory $factory) public function __construct(View_Factory $factory, View_Composer $composer, $view, $data, $path)
{ {
$this->view = $view; $this->view = $view;
$this->data = $data; $this->data = $data;
@ -218,6 +223,18 @@ public function __construct($view, $data, $path, View_Composer $composer, View_F
} }
} }
/**
* Create a new view instance.
*
* @param string $view
* @param array $data
* @return View
*/
public static function make($view, $data = array())
{
return IoC::container()->resolve('laravel.view')->make($view, $data);
}
/** /**
* Get the evaluated string content of the view. * Get the evaluated string content of the view.
* *

View File

@ -12,7 +12,6 @@ public function testGetMethodReturnsItemsFromArray($array)
$this->assertEquals(Arr::get($array, 'names.uncle'), $array['names']['uncle']); $this->assertEquals(Arr::get($array, 'names.uncle'), $array['names']['uncle']);
} }
/** /**
* @dataProvider getArray * @dataProvider getArray
*/ */
@ -23,7 +22,6 @@ public function testGetMethodReturnsDefaultWhenItemDoesntExist($array)
$this->assertEquals(Arr::get($array, 'names.aunt', function() {return 'Tammy';}), 'Tammy'); $this->assertEquals(Arr::get($array, 'names.aunt', function() {return 'Tammy';}), 'Tammy');
} }
/** /**
* @dataProvider getArray * @dataProvider getArray
*/ */
@ -39,7 +37,6 @@ public function testSetMethodSetsItemsInArray($array)
} }
public function getArray() public function getArray()
{ {
return array(array( return array(array(

View File

@ -2,13 +2,11 @@
class ConfigTest extends PHPUnit_Framework_TestCase { class ConfigTest extends PHPUnit_Framework_TestCase {
public function setUp() public function setUp()
{ {
IoC::container()->singletons = array(); IoC::container()->singletons = array();
} }
/** /**
* @dataProvider getGetMocker * @dataProvider getGetMocker
*/ */
@ -19,7 +17,6 @@ public function testHasMethodReturnsTrueWhenItemExists($mock, $mocker)
$this->assertTrue($mock->has('something')); $this->assertTrue($mock->has('something'));
} }
/** /**
* @dataProvider getGetMocker * @dataProvider getGetMocker
*/ */
@ -30,7 +27,6 @@ public function testHasMethodReturnsFalseWhenItemDoesntExist($mock, $mocker)
$this->assertFalse($mock->has('something')); $this->assertFalse($mock->has('something'));
} }
public function getGetMocker() public function getGetMocker()
{ {
$mock = $this->getMock('Laravel\\Config', array('get'), array(null)); $mock = $this->getMock('Laravel\\Config', array('get'), array(null));
@ -38,7 +34,6 @@ public function getGetMocker()
return array(array($mock, $mock->expects($this->any())->method('get'))); return array(array($mock, $mock->expects($this->any())->method('get')));
} }
public function testConfigClassCanRetrieveItems() public function testConfigClassCanRetrieveItems()
{ {
$config = IoC::container()->config; $config = IoC::container()->config;
@ -47,7 +42,6 @@ public function testConfigClassCanRetrieveItems()
$this->assertEquals($config->get('application.url'), 'http://localhost'); $this->assertEquals($config->get('application.url'), 'http://localhost');
} }
public function testGetMethodReturnsDefaultWhenItemDoesntExist() public function testGetMethodReturnsDefaultWhenItemDoesntExist()
{ {
$config = IoC::container()->config; $config = IoC::container()->config;
@ -57,7 +51,6 @@ public function testGetMethodReturnsDefaultWhenItemDoesntExist()
$this->assertEquals($config->get('config.item', function() {return 'test';}), 'test'); $this->assertEquals($config->get('config.item', function() {return 'test';}), 'test');
} }
public function testConfigClassCanSetItems() public function testConfigClassCanSetItems()
{ {
$config = IoC::container()->config; $config = IoC::container()->config;

View File

@ -1,12 +1,4 @@
<?php <?php
/**
* Laravel - A clean and classy framework for PHP web development.
*
* @package Laravel
* @version 2.0.0
* @author Taylor Otwell <taylorotwell@gmail.com>
* @link http://laravel.com
*/
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------