Commit 7eef380d authored by Taylor Otwell's avatar Taylor Otwell

more refactoring and dependency injection.

parent 0c2834fe
......@@ -39,7 +39,12 @@ return array(
|
*/
'home.index' => array('name' => 'home', function($view)
'global' => function($view, $laravel)
{
//
},
'home.index' => array('name' => 'home', function($view, $laravel)
{
//
}),
......
......@@ -20,33 +20,33 @@ return array(
'Arr' => 'Laravel\\Arr',
'Asset' => 'Laravel\\Asset',
'Auth' => 'Laravel\\Security\\Authenticator_Facade',
'Auth' => 'Laravel\\Facades\\Auth',
'Benchmark' => 'Laravel\\Benchmark',
'Cache' => 'Laravel\\Cache\\Manager_Facade',
'Config' => 'Laravel\\Config_Facade',
'Cache' => 'Laravel\\Facades\\Cache',
'Config' => 'Laravel\\Facades\\Config',
'Controller' => 'Laravel\\Controller',
'Cookie' => 'Laravel\\Cookie_Facade',
'Crypter' => 'Laravel\\Security\\Crypter_Facade',
'DB' => 'Laravel\\Database\\Manager_Facade',
'Download' => 'Laravel\\Download_Facade',
'Cookie' => 'Laravel\\Facades\\Cookie',
'Crypter' => 'Laravel\\Facades\\Crypter',
'DB' => 'Laravel\\Facades\\DB',
'Download' => 'Laravel\\Facades\\Download',
'Eloquent' => 'Laravel\\Database\\Eloquent\\Model',
'File' => 'Laravel\\File_Facade',
'Form' => 'Laravel\\Form_Facade',
'Hasher' => 'Laravel\\Security\\Hashing\\Hasher_Facade',
'HTML' => 'Laravel\\HTML_Facade',
'File' => 'Laravel\\Facades\\File',
'Form' => 'Laravel\\Facades\\Form',
'Hasher' => 'Laravel\\Facades\\Hasher',
'HTML' => 'Laravel\\Facades\\HTML',
'Inflector' => 'Laravel\\Inflector',
'Input' => 'Laravel\\Input_Facade',
'Input' => 'Laravel\\Facades\\Input',
'IoC' => 'Laravel\\IoC',
'Lang' => 'Laravel\\Lang_Facade',
'Loader' => 'Laravel\\Loader_Facade',
'Package' => 'Laravel\\Package_Facade',
'URL' => 'Laravel\\URL_Facade',
'Redirect' => 'Laravel\\Redirect_Facade',
'Request' => 'Laravel\\Request_Facade',
'Response' => 'Laravel\\Response_Facade',
'Session' => 'Laravel\\Session\\Manager_Facade',
'Lang' => 'Laravel\\Lang',
'Loader' => 'Laravel\\Facades\\Loader',
'Package' => 'Laravel\\Facades\\Package',
'URL' => 'Laravel\\Facades\\URL',
'Redirect' => 'Laravel\\Facades\\Redirect',
'Request' => 'Laravel\\Facades\\Request',
'Response' => 'Laravel\\Response',
'Session' => 'Laravel\\Facades\\Session',
'Str' => 'Laravel\\Str',
'Validator' => 'Laravel\\Validation\\Validator_Facade',
'View' => 'Laravel\\View_Facade',
'Validator' => 'Laravel\\Facades\\Validator',
'View' => 'Laravel\\View',
);
\ No newline at end of file
<?php
use Laravel\Application;
use Laravel\Response;
return array(
/*
......@@ -45,13 +42,13 @@ return array(
|
*/
'before' => function()
'before' => function($laravel)
{
// Do stuff before every request to your application.
},
'after' => function(Response $response)
'after' => function($laravel, $response)
{
// Do stuff after every request to your application.
},
......
......@@ -16,28 +16,28 @@ return array(
|
| 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!';
| }
|
| You can even respond to more than one URI:
|
| 'GET /hello, GET /world' => function()
| 'GET /hello, GET /world' => function($laravel)
| {
| return 'Hello World!';
| }
|
| 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.";
| }
|
*/
'GET /' => function()
'GET /' => function($laravel)
{
return View::make('home.index');
},
......
......@@ -35,7 +35,7 @@ define('VIEW_PATH', APP_PATH.'views/');
// --------------------------------------------------------------
// 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.'config'.EXT;
require SYS_PATH.'arr'.EXT;
......@@ -62,6 +62,6 @@ $container = new Container($dependencies);
IoC::$container = $container;
// --------------------------------------------------------------
// Load the auto-loader.
// Register the auto-loader on the stack.
// --------------------------------------------------------------
spl_autoload_register(array($container->loader, 'load'));
\ No newline at end of file
<?php namespace Laravel\Cache;
use Laravel\Facade;
use Laravel\Container;
class Manager_Facade extends Facade {
public static $resolve = 'cache';
}
class Manager {
/**
......
<?php namespace Laravel;
class Config_Facade extends Facade {
public static $resolve = 'config';
}
class Config {
/**
......
......@@ -158,7 +158,19 @@ return array(
'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 @@ return array(
'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 @@ return array(
'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);
}),
/*
......
......@@ -31,4 +31,12 @@ abstract class Controller {
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;
}
}
\ No newline at end of file
<?php namespace Laravel;
class Cookie_Facade extends Facade {
public static $resolve = 'cookie';
}
class Cookie {
/**
......
<?php namespace Laravel\Database;
use Laravel\Facade;
class Manager_Facade extends Facade {
public static $resolve = 'database';
}
class Manager {
/**
......
<?php namespace Laravel;
class Download_Facade extends Facade {
public static $resolve = 'download';
}
class Download extends Response {
/**
......@@ -43,7 +37,7 @@ class Download extends Response {
'Content-Type' => $this->mime($this->file->extension($path)),
'Content-Disposition' => 'attachment; filename="'.$name.'"',
'Content-Transfer-Encoding' => 'binary',
'Expires' = => 0,
'Expires' => 0,
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Pragma' => 'public',
'Content-Length' => $this->file-size($path),
......
<?php namespace Laravel;
<?php namespace Laravel\Facades;
use Laravel\IoC;
abstract class Facade {
......@@ -16,4 +18,24 @@ abstract class Facade {
return call_user_func_array(array(IoC::container()->resolve('laravel.'.static::$resolve), $method), $parameters);
}
}
\ No newline at end of file
}
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'; }
\ No newline at end of file
<?php namespace Laravel;
class File_Facade extends Facade {
public static $resolve = 'file';
}
class File {
/**
......
<?php namespace Laravel;
class Form_Facade extends Facade {
public static $resolve = 'form';
}
class Form {
/**
......@@ -73,7 +67,7 @@ class Form {
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) : '';
......
<?php namespace Laravel;
class HTML_Facade extends Facade {
public static $resolve = 'html';
}
class HTML {
/**
......@@ -13,7 +7,7 @@ class HTML {
*
* @var string
*/
private $encoding;
public $encoding;
/**
* The URL generator instance.
......
<?php namespace Laravel;
class Input_Facade extends Facade {
public static $resolve = 'input';
}
class Input {
/**
......
<?php namespace Laravel;
class Lang_Facade extends Facade {
public static $resolve = 'lang';
}
class Lang_Factory {
/**
......@@ -25,6 +19,9 @@ class Lang_Factory {
/**
* 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 array $paths
* @return void
......@@ -105,6 +102,18 @@ class Lang {
$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.
*
......
<?php namespace Laravel;
class Loader_Facade extends Facade {
public static $resolve = 'loader';
}
class Loader {
/**
......@@ -44,7 +38,7 @@ class Loader {
*/
public function load($class)
{
$file = strtolower(str_replace(array('\\', '_Facade'), array('/', ''), $class));
$file = strtolower(str_replace('\\', '/', $class));
if (array_key_exists($class, $this->aliases))
{
......
<?php namespace Laravel;
class Package_Facade extends Facade {
public static $resolve = 'package';
}
class Package {
/**
......
<?php namespace Laravel;
class Redirect_Facade extends Facade {
public static $resolve = 'redirect';
}
class Redirect extends Response {
/**
......@@ -18,7 +12,6 @@ class Redirect extends Response {
/**
* Create a new redirect generator instance.
*
* @param Session\Driver $session
* @param URL $url
* @return void
*/
......
<?php namespace Laravel;
class Request_Facade extends Facade {
public static $resolve = 'request';
}
class Request {
/**
......@@ -192,37 +186,10 @@ class Request {
}
/**
* Determine if the route handling the request has a given name.
*
* <code>
* // 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)
{
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.
* Get the route handling the current request.
*
* <code>
* // Determine if the route handling the request is named "profile"
* $profile = Request::active()->route_is_profile();
* </code>
* @return Route
*/
public function __call($method, $parameters)
{
if (strpos($method, 'route_is_') === 0)
{
return $this->route_is(substr($method, 9));
}
}
public function route() { return $this->route; }
}
\ No newline at end of file
<?php namespace Laravel;
class Response_Facade extends Facade {
public static $resolve = 'response';
}
class Response_Factory {
/**
......@@ -230,4 +224,12 @@ class Response {
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);
}
}
\ No newline at end of file
......@@ -14,23 +14,32 @@ class Caller {
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.
*
* @param Container $container
* @param string $controller_path
* @param Filterer $filterer
* @param Delegator $delegator
* @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->controller_path = $controller_path;
$this->delegator = $delegator;
}
/**
......@@ -46,116 +55,41 @@ class Caller {
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
// 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)))
if ( ! is_null($response = $this->before($route)))
{
return $this->finish($route, $response);
}
$closure = ( ! $route->callback instanceof Closure) ? $this->find_route_closure($route) : $route->callback;
if ( ! is_null($closure)) return $this->handle_closure($route, $closure);
return $this->finish($route, $this->container->resolve('laravel.response')->error('404'));
}
/**
* Extract the route closure from the route.
*
* If a "do" index is specified on the callback, that is the handler.
* Otherwise, we will return the first callable array value.
*
* @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
*/
protected function handle_closure(Route $route, Closure $closure)
{
$response = call_user_func_array($closure, $route->parameters);
// If the route closure returns an array, we assume that they are returning a
// 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))
if ( ! is_null($response = $route->call($this->container)))
{
throw new \Exception("Controller [$controller] does not exist.");
}
require $path;
if (is_array($response)) $response = $this->delegator->delegate($route, $response);
$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();
return $this->finish($route, $response);
}
// 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;
return $this->finish($route, $this->container->response->error('404'));
}
/**
* Resolve a controller name to a controller instance.
* Run the "before" filters for the route.
*
* If a before filter returns a value, that value will be considered the response to the
* request and the route function / controller will not be used to handle the request.
*
* @param string $controller
* @return Controller
* @param Route $route
* @return mixed
*/
protected function resolve_controller($controller)
protected function before(Route $route)
{
if ($this->container->registered('controllers.'.$controller)) return $this->container->resolve('controllers.'.$controller);
$controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
$before = array_merge(array('before'), $route->filters('before'));
return new $controller;
return $this->filterer->filter($before, array($this->container), true);
}
/**
* Finish the route handling for the request.
*
* The route response will be converted to a Response instance and the "after" filters
* defined for the route will be executed.
* The route response will be converted to a Response instance and the "after" filters will be run.
*
* @param Route $route
* @param mixed $response
......@@ -165,33 +99,9 @@ class Caller {
{
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;
}
/**
* 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;
}
}
}
\ No newline at end of file
<?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';
}
}
\ No newline at end of file
<?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;
}
}
}
\ No newline at end of file
<?php namespace Laravel\Routing;
use Closure;
use Laravel\Container;
class Route {
/**
......@@ -30,13 +33,6 @@ class Route {
*/
public $parameters;
/**
* The route filters for the application.
*
* @param array $filters
*/
public $filters = array();
/**
* Create a new Route instance.
*
......@@ -45,51 +41,120 @@ class Route {
* @param array $parameters
* @return void
*/
public function __construct($key, $callback, $parameters)
public function __construct($key, $callback, $parameters = array())
{
$this->key = $key;
$this->callback = $callback;
$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);
/**
* Call the route closure.
*
* 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.
*
* @param Container $container
* @return mixed
*/
public function call(Container $container)
{
if (is_null($closure = $this->find_closure())) return;
$this->uris[] = ($segment !== '/') ? trim($segment, '/') : $segment;
}
return call_user_func_array($closure, array_merge($this->parameters, array($container)));
}
/**
* Get all of the "before" filters defined for the route.
* Extract the route closure from the route.
*
* @return array
* @return Closure|null
*/
public function before()
protected function find_closure()
{
return $this->filters('before');
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 all of the "after" filters defined for the route.
* Get an array of filter names defined for a route.
*
* @param string $name
* @return array
*/
public function after()
public function filters($name)
{
return $this->filters('after');
return (is_array($this->callback) and isset($this->callback[$name])) ? explode(', ', $this->callback[$name]) : array();
}
/**
* Get an array of filters defined for the route.
* 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
*/
private function filters($name)
protected function parse($key)
{
return (is_array($this->callback) and isset($this->callback[$name])) ? explode(', ', $this->callback[$name]) : array();
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)); }
}
}
\ No newline at end of file
......@@ -111,7 +111,7 @@ class Router {
}
}
return $this->route_to_controller();
return $this->request->route = $this->route_to_controller();
}
/**
......@@ -123,6 +123,8 @@ class Router {
*/
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(), '/'));
if ( ! is_null($key = $this->controller_key($segments)))
......@@ -147,7 +149,7 @@ class Router {
// were they to code the controller delegation manually.
$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);
}
}
......
<?php namespace Laravel\Security;
use Laravel\IoC;
use Laravel\Facade;
use Laravel\Session\Driver;
class Authenticator_Facade extends Facade {
public static $resolve = 'auth';
}
class Authenticator {
/**
......
<?php namespace Laravel\Security;
use Laravel\IoC;
use Laravel\Facade;
class Crypter_Facade extends Facade {
public static $resolve = 'crypter';
}
class Crypter {
/**
......
<?php namespace Laravel\Security\Hashing;
use Laravel\Facade;
class Hasher_Facade extends Facade {
public static $resolve = 'hasher';
}
class Hasher {
/**
......@@ -15,31 +7,21 @@ class Hasher {
*
* @var Hash\Engine
*/
public $engine;
protected $engine;
/**
* Create a new Hasher instance.
*
* If no hashing engine is provided, the BCrypt engine will be used.
*
* @param Engine $engine
* @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.
*
* <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)
{
......@@ -48,11 +30,6 @@ class Hasher {
/**
* 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)
{
......
<?php namespace Laravel\Session;
use Laravel\Facade;
use Laravel\Container;
class Manager_Facade extends Facade {
public static $resolve = 'session';
}
class Manager {
/**
......
<?php namespace Laravel;
class URL_Facade extends Facade {
public static $resolve = 'url';
}
class URL {
/**
......@@ -76,8 +70,9 @@ class URL {
/**
* 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.
* The values of this array will be used to fill the wildcard segments of the route URI.
* For routes that have wildcard parameters, an array may be passed as the second
* 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.
*
......
......@@ -2,12 +2,40 @@
use Laravel\IoC;
use Laravel\Str;
use Laravel\Facade;
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.
*
* @param Lang $lang
* @param Lang_Factory $lang
* @param array $attributes
* @param array $rules
* @param array $messages
* @return void
*/
public function __construct(Lang_Factory $lang)
public function __construct(Lang_Factory $lang, $attributes, $rules, $messages = array())
{
$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 $rules
* @param array $messages
* @return Validator
*/
public function of($attributes, $rules, $messages = array())
public static function make($attributes, $rules, $messages = array())
{
foreach ($rules as $key => &$rule)
{
$rule = (is_string($rule)) ? explode('|', $rule) : $rule;
}
$this->attributes = $attributes;
$this->messages = $messages;
$this->rules = $rules;
return $this;
return IoC::container()->resolve('laravel.validator')->make($attributes, $rules, $messages);
}
/**
......
<?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
* access through the application instance from a route or controller, and is managed
......@@ -108,7 +43,7 @@ class View_Factory {
*/
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 @@ class View_Factory {
{
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.");
......@@ -152,6 +87,76 @@ class View_Factory {
}
/**
* 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
* class for working with individual views. It provides methods for binding data to
......@@ -197,14 +202,14 @@ class View {
/**
* Create a new view instance.
*
* @param View_Factory $factory
* @param View_Composer $composer
* @param string $view
* @param array $data
* @param string $path
* @param View_Composer $composer
* @param View_Factory $factory
* @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->data = $data;
......@@ -218,6 +223,18 @@ class View {
}
}
/**
* 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.
*
......
......@@ -12,7 +12,6 @@ class ArrTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(Arr::get($array, 'names.uncle'), $array['names']['uncle']);
}
/**
* @dataProvider getArray
*/
......@@ -23,7 +22,6 @@ class ArrTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(Arr::get($array, 'names.aunt', function() {return 'Tammy';}), 'Tammy');
}
/**
* @dataProvider getArray
*/
......@@ -39,7 +37,6 @@ class ArrTest extends PHPUnit_Framework_TestCase {
}
public function getArray()
{
return array(array(
......
......@@ -2,13 +2,11 @@
class ConfigTest extends PHPUnit_Framework_TestCase {
public function setUp()
{
IoC::container()->singletons = array();
}
/**
* @dataProvider getGetMocker
*/
......@@ -19,7 +17,6 @@ class ConfigTest extends PHPUnit_Framework_TestCase {
$this->assertTrue($mock->has('something'));
}
/**
* @dataProvider getGetMocker
*/
......@@ -30,7 +27,6 @@ class ConfigTest extends PHPUnit_Framework_TestCase {
$this->assertFalse($mock->has('something'));
}
public function getGetMocker()
{
$mock = $this->getMock('Laravel\\Config', array('get'), array(null));
......@@ -38,7 +34,6 @@ class ConfigTest extends PHPUnit_Framework_TestCase {
return array(array($mock, $mock->expects($this->any())->method('get')));
}
public function testConfigClassCanRetrieveItems()
{
$config = IoC::container()->config;
......@@ -47,7 +42,6 @@ class ConfigTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($config->get('application.url'), 'http://localhost');
}
public function testGetMethodReturnsDefaultWhenItemDoesntExist()
{
$config = IoC::container()->config;
......@@ -57,7 +51,6 @@ class ConfigTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($config->get('config.item', function() {return 'test';}), 'test');
}
public function testConfigClassCanSetItems()
{
$config = IoC::container()->config;
......
<?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
*/
/*
|--------------------------------------------------------------------------
......
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