more refactoring and dependency injection.
This commit is contained in:
parent
0c2834fed1
commit
7eef380d8a
|
@ -39,7 +39,12 @@
|
|||
|
|
||||
*/
|
||||
|
||||
'home.index' => array('name' => 'home', function($view)
|
||||
'global' => function($view, $laravel)
|
||||
{
|
||||
//
|
||||
},
|
||||
|
||||
'home.index' => array('name' => 'home', function($view, $laravel)
|
||||
{
|
||||
//
|
||||
}),
|
||||
|
|
|
@ -20,33 +20,33 @@
|
|||
|
||||
'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',
|
||||
|
||||
);
|
|
@ -1,8 +1,5 @@
|
|||
<?php
|
||||
|
||||
use Laravel\Application;
|
||||
use Laravel\Response;
|
||||
|
||||
return array(
|
||||
|
||||
/*
|
||||
|
@ -45,13 +42,13 @@
|
|||
|
|
||||
*/
|
||||
|
||||
'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 @@
|
|||
|
|
||||
| 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 @@
|
|||
// --------------------------------------------------------------
|
||||
// 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 @@
|
|||
IoC::$container = $container;
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// Load the auto-loader.
|
||||
// Register the auto-loader on the stack.
|
||||
// --------------------------------------------------------------
|
||||
spl_autoload_register(array($container->loader, 'load'));
|
|
@ -1,14 +1,7 @@
|
|||
<?php namespace Laravel\Cache;
|
||||
|
||||
use Laravel\Facade;
|
||||
use Laravel\Container;
|
||||
|
||||
class Manager_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'cache';
|
||||
|
||||
}
|
||||
|
||||
class Manager {
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
class Config_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'config';
|
||||
|
||||
}
|
||||
|
||||
class Config {
|
||||
|
||||
/**
|
||||
|
|
|
@ -158,7 +158,19 @@
|
|||
|
||||
'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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return new View_Composer(require APP_PATH.'composers'.EXT);
|
||||
return new View_Composer($container, require APP_PATH.'composers'.EXT);
|
||||
}),
|
||||
|
||||
/*
|
||||
|
|
|
@ -31,4 +31,12 @@ public function __call($method, $parameters)
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,11 +1,5 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
class Cookie_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'cookie';
|
||||
|
||||
}
|
||||
|
||||
class Cookie {
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,13 +1,5 @@
|
|||
<?php namespace Laravel\Database;
|
||||
|
||||
use Laravel\Facade;
|
||||
|
||||
class Manager_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'database';
|
||||
|
||||
}
|
||||
|
||||
class Manager {
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
class Download_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'download';
|
||||
|
||||
}
|
||||
|
||||
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-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),
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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'; }
|
|
@ -1,11 +1,5 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
class File_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'file';
|
||||
|
||||
}
|
||||
|
||||
class File {
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
class Form_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'form';
|
||||
|
||||
}
|
||||
|
||||
class Form {
|
||||
|
||||
/**
|
||||
|
@ -73,7 +67,7 @@ public function open($action = null, $method = 'POST', $attributes = array(), $h
|
|||
|
||||
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) : '';
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
<?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.
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
class Input_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'input';
|
||||
|
||||
}
|
||||
|
||||
class Input {
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
<?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 @@ public function __construct($key, $replacements, $language, $paths)
|
|||
$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.
|
||||
*
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
class Loader_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'loader';
|
||||
|
||||
}
|
||||
|
||||
class Loader {
|
||||
|
||||
/**
|
||||
|
@ -44,7 +38,7 @@ public function __construct($aliases, $paths)
|
|||
*/
|
||||
public function load($class)
|
||||
{
|
||||
$file = strtolower(str_replace(array('\\', '_Facade'), array('/', ''), $class));
|
||||
$file = strtolower(str_replace('\\', '/', $class));
|
||||
|
||||
if (array_key_exists($class, $this->aliases))
|
||||
{
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
class Package_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'package';
|
||||
|
||||
}
|
||||
|
||||
class Package {
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
<?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
|
||||
*/
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
class Request_Facade extends Facade {
|
||||
|
||||
public static $resolve = '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>
|
||||
* // Determine if the route handling the request is named "profile"
|
||||
* $profile = Request::active()->route_is('profile');
|
||||
* </code>
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
* @return Route
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* <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));
|
||||
}
|
||||
}
|
||||
public function route() { return $this->route; }
|
||||
|
||||
}
|
|
@ -1,11 +1,5 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
class Response_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'response';
|
||||
|
||||
}
|
||||
|
||||
class Response_Factory {
|
||||
|
||||
/**
|
||||
|
@ -230,4 +224,12 @@ public function status($status)
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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 @@ public function call(Route $route)
|
|||
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($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.
|
||||
* Otherwise, we will return the first callable array value.
|
||||
* 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 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
|
||||
* @param Route $route
|
||||
* @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
|
||||
// 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;
|
||||
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 @@ protected function finish(Route $route, $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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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';
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
<?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;
|
||||
|
||||
// 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;
|
||||
}
|
||||
$this->uris = $this->parse($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the "before" filters defined for the route.
|
||||
* Call the route closure.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function before()
|
||||
{
|
||||
return $this->filters('before');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the "after" filters defined for the route.
|
||||
* 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.
|
||||
*
|
||||
* @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
|
||||
* @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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)); }
|
||||
}
|
||||
|
||||
}
|
|
@ -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()
|
||||
{
|
||||
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 @@ protected function route_to_controller()
|
|||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,8 @@
|
|||
<?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 {
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,14 +1,5 @@
|
|||
<?php namespace Laravel\Security;
|
||||
|
||||
use Laravel\IoC;
|
||||
use Laravel\Facade;
|
||||
|
||||
class Crypter_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'crypter';
|
||||
|
||||
}
|
||||
|
||||
class Crypter {
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,13 +1,5 @@
|
|||
<?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 @@ public function __call($method, $parameters)
|
|||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
|
|
|
@ -1,14 +1,7 @@
|
|||
<?php namespace Laravel\Session;
|
||||
|
||||
use Laravel\Facade;
|
||||
use Laravel\Container;
|
||||
|
||||
class Manager_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'session';
|
||||
|
||||
}
|
||||
|
||||
class Manager {
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
class URL_Facade extends Facade {
|
||||
|
||||
public static $resolve = 'url';
|
||||
|
||||
}
|
||||
|
||||
class URL {
|
||||
|
||||
/**
|
||||
|
@ -76,8 +70,9 @@ public function to_asset($url, $https = null)
|
|||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
157
laravel/view.php
157
laravel/view.php
|
@ -1,70 +1,5 @@
|
|||
<?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 @@ public function __construct(View_Composer $composer, $path)
|
|||
*/
|
||||
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)))
|
||||
{
|
||||
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 @@ 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
|
||||
* 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 @@ 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.
|
||||
*
|
||||
|
|
|
@ -12,7 +12,6 @@ public function testGetMethodReturnsItemsFromArray($array)
|
|||
$this->assertEquals(Arr::get($array, 'names.uncle'), $array['names']['uncle']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider getArray
|
||||
*/
|
||||
|
@ -23,7 +22,6 @@ public function testGetMethodReturnsDefaultWhenItemDoesntExist($array)
|
|||
$this->assertEquals(Arr::get($array, 'names.aunt', function() {return 'Tammy';}), 'Tammy');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider getArray
|
||||
*/
|
||||
|
@ -39,7 +37,6 @@ public function testSetMethodSetsItemsInArray($array)
|
|||
|
||||
}
|
||||
|
||||
|
||||
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 @@ public function testHasMethodReturnsTrueWhenItemExists($mock, $mocker)
|
|||
$this->assertTrue($mock->has('something'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider getGetMocker
|
||||
*/
|
||||
|
@ -30,7 +27,6 @@ public function testHasMethodReturnsFalseWhenItemDoesntExist($mock, $mocker)
|
|||
$this->assertFalse($mock->has('something'));
|
||||
}
|
||||
|
||||
|
||||
public function getGetMocker()
|
||||
{
|
||||
$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')));
|
||||
}
|
||||
|
||||
|
||||
public function testConfigClassCanRetrieveItems()
|
||||
{
|
||||
$config = IoC::container()->config;
|
||||
|
@ -47,7 +42,6 @@ public function testConfigClassCanRetrieveItems()
|
|||
$this->assertEquals($config->get('application.url'), 'http://localhost');
|
||||
}
|
||||
|
||||
|
||||
public function testGetMethodReturnsDefaultWhenItemDoesntExist()
|
||||
{
|
||||
$config = IoC::container()->config;
|
||||
|
@ -57,7 +51,6 @@ public function testGetMethodReturnsDefaultWhenItemDoesntExist()
|
|||
$this->assertEquals($config->get('config.item', function() {return 'test';}), 'test');
|
||||
}
|
||||
|
||||
|
||||
public function testConfigClassCanSetItems()
|
||||
{
|
||||
$config = IoC::container()->config;
|
||||
|
|
|
@ -1,12 +1,4 @@
|
|||
<?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
|
||||
*/
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue