diff --git a/application/composers.php b/application/composers.php index 71a2428b..5e70068f 100644 --- a/application/composers.php +++ b/application/composers.php @@ -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) { // }), diff --git a/application/config/aliases.php b/application/config/aliases.php index 55e9d324..47b8a07d 100644 --- a/application/config/aliases.php +++ b/application/config/aliases.php @@ -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 diff --git a/application/filters.php b/application/filters.php index 8e06de17..c4cd6a40 100644 --- a/application/filters.php +++ b/application/filters.php @@ -1,8 +1,5 @@ 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. }, diff --git a/application/routes.php b/application/routes.php index 94efd50d..f21aa5ce 100644 --- a/application/routes.php +++ b/application/routes.php @@ -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'); }, diff --git a/laravel/bootstrap.php b/laravel/bootstrap.php index a6ff2e82..8cd44a90 100644 --- a/laravel/bootstrap.php +++ b/laravel/bootstrap.php @@ -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 diff --git a/laravel/cache/manager.php b/laravel/cache/manager.php index fdfa9753..a70cc3ed 100644 --- a/laravel/cache/manager.php +++ b/laravel/cache/manager.php @@ -1,14 +1,7 @@ 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); }), /* diff --git a/laravel/controller.php b/laravel/controller.php index 8cc534a4..c5659791 100644 --- a/laravel/controller.php +++ b/laravel/controller.php @@ -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 diff --git a/laravel/cookie.php b/laravel/cookie.php index c82df951..3b7a3be0 100644 --- a/laravel/cookie.php +++ b/laravel/cookie.php @@ -1,11 +1,5 @@ $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), diff --git a/laravel/facade.php b/laravel/facade.php deleted file mode 100644 index d3836172..00000000 --- a/laravel/facade.php +++ /dev/null @@ -1,19 +0,0 @@ -resolve('laravel.'.static::$resolve), $method), $parameters); - } - -} \ No newline at end of file diff --git a/laravel/facades.php b/laravel/facades.php new file mode 100644 index 00000000..96155f53 --- /dev/null +++ b/laravel/facades.php @@ -0,0 +1,41 @@ +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'; } \ No newline at end of file diff --git a/laravel/file.php b/laravel/file.php index 3bc1f1eb..2f39e81a 100644 --- a/laravel/file.php +++ b/laravel/file.php @@ -1,11 +1,5 @@ html->encoding; } $append = ($method == 'PUT' or $method == 'DELETE') ? $this->hidden('REQUEST_METHOD', $method) : ''; diff --git a/laravel/html.php b/laravel/html.php index a3dfbacb..dd77bfcf 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -1,11 +1,5 @@ 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. * diff --git a/laravel/loader.php b/laravel/loader.php index 24362c2e..9a0dd253 100644 --- a/laravel/loader.php +++ b/laravel/loader.php @@ -1,11 +1,5 @@ aliases)) { diff --git a/laravel/package.php b/laravel/package.php index cf247ecf..9839b2f6 100644 --- a/laravel/package.php +++ b/laravel/package.php @@ -1,11 +1,5 @@ - * // Determine if the route handling the request is named "profile" - * $profile = Request::active()->route_is('profile'); - * - * - * @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. - * - * - * // Determine if the route handling the request is named "profile" - * $profile = Request::active()->route_is_profile(); - * - */ - 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 diff --git a/laravel/response.php b/laravel/response.php index fd2da86d..2b10d5d4 100644 --- a/laravel/response.php +++ b/laravel/response.php @@ -1,11 +1,5 @@ resolve('laravel.response'), $method), $parameters); + } + } \ No newline at end of file diff --git a/laravel/routing/caller.php b/laravel/routing/caller.php index 44c43cd0..ca8b8003 100644 --- a/laravel/routing/caller.php +++ b/laravel/routing/caller.php @@ -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($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 @@ 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 diff --git a/laravel/routing/delegator.php b/laravel/routing/delegator.php new file mode 100644 index 00000000..2f31870f --- /dev/null +++ b/laravel/routing/delegator.php @@ -0,0 +1,117 @@ +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 diff --git a/laravel/routing/filterer.php b/laravel/routing/filterer.php new file mode 100644 index 00000000..9d00a3fe --- /dev/null +++ b/laravel/routing/filterer.php @@ -0,0 +1,46 @@ +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 diff --git a/laravel/routing/route.php b/laravel/routing/route.php index 5080858a..7332f8ac 100644 --- a/laravel/routing/route.php +++ b/laravel/routing/route.php @@ -1,5 +1,8 @@ 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)); } + } + } \ No newline at end of file diff --git a/laravel/routing/router.php b/laravel/routing/router.php index f8a66ab8..e66bbc5e 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -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); } } diff --git a/laravel/security/authenticator.php b/laravel/security/authenticator.php index 67c685c3..a0dea601 100644 --- a/laravel/security/authenticator.php +++ b/laravel/security/authenticator.php @@ -1,15 +1,8 @@ engine = (is_null($engine)) ? new BCrypt(10, false) : $engine; + $this->engine = $engine } /** * Magic Method for delegating method calls to the hashing engine. - * - * - * // 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'); - * */ public function __call($method, $parameters) { @@ -48,11 +30,6 @@ class Hasher { /** * Magic Method for performing methods on the default hashing engine. - * - * - * // Hash a value using the default hashing engine - * $hash = Hasher::hash('password'); - * */ public static function __callStatic($method, $parameters) { diff --git a/laravel/session/manager.php b/laravel/session/manager.php index cb0ba4db..9c190b5b 100644 --- a/laravel/session/manager.php +++ b/laravel/session/manager.php @@ -1,14 +1,7 @@ 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); } /** diff --git a/laravel/view.php b/laravel/view.php index 59c66d72..01caf1b7 100644 --- a/laravel/view.php +++ b/laravel/view.php @@ -1,70 +1,5 @@ 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. * diff --git a/tests/ArrTest.php b/tests/ArrTest.php index bbc85a92..3e7cfe66 100644 --- a/tests/ArrTest.php +++ b/tests/ArrTest.php @@ -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( diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 8d9ab918..de1abafa 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -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; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 76001e09..888560fe 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,12 +1,4 @@ - * @link http://laravel.com - */ /* |--------------------------------------------------------------------------