From af36cb3d5abae2f0dbace148e65f87c0cad532b8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 20 Oct 2011 21:44:18 -0500 Subject: [PATCH] various refactoring and tweaks. --- application/config/cache.php | 4 +- application/filters.php | 10 ++- application/language/en/.gitignore | 0 .../language/en/pagination.php | 0 .../language/en/validation.php | 0 laravel/bootstrap/constants.php | 1 - laravel/bootstrap/errors.php | 25 +++++-- laravel/cache/drivers/driver.php | 2 +- laravel/cache/manager.php | 4 +- laravel/config/container.php | 2 +- laravel/database/connection.php | 68 +++++++++---------- laravel/database/grammars/grammar.php | 11 +-- laravel/html.php | 9 ++- laravel/lang.php | 5 +- laravel/routing/controller.php | 6 +- laravel/routing/route.php | 31 +++++---- laravel/routing/router.php | 9 ++- laravel/security/auth.php | 11 +-- laravel/security/crypter.php | 12 ++-- laravel/session/manager.php | 9 +-- laravel/validation/validator.php | 10 +-- laravel/view.php | 21 +++--- 22 files changed, 140 insertions(+), 110 deletions(-) delete mode 100644 application/language/en/.gitignore rename {laravel => application}/language/en/pagination.php (100%) rename {laravel => application}/language/en/validation.php (100%) diff --git a/application/config/cache.php b/application/config/cache.php index 7c6bf32ea..c04359887 100644 --- a/application/config/cache.php +++ b/application/config/cache.php @@ -45,8 +45,10 @@ return array( | */ - 'servers' => array( + 'memcached' => array( + array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100), + ), ); \ No newline at end of file diff --git a/application/filters.php b/application/filters.php index 2bf71db72..7e308455c 100644 --- a/application/filters.php +++ b/application/filters.php @@ -59,13 +59,19 @@ return array( 'auth' => function() { - if ( ! Auth::check()) return Redirect::to_login(); + if ( ! Auth::check()) + { + return Redirect::to_login(); + } }, 'csrf' => function() { - if (Input::get('csrf_token') !== Form::raw_token()) return Response::error('500'); + if (Input::get('csrf_token') !== Form::raw_token()) + { + return Response::error('500'); + } }, ); \ No newline at end of file diff --git a/application/language/en/.gitignore b/application/language/en/.gitignore deleted file mode 100644 index e69de29bb..000000000 diff --git a/laravel/language/en/pagination.php b/application/language/en/pagination.php similarity index 100% rename from laravel/language/en/pagination.php rename to application/language/en/pagination.php diff --git a/laravel/language/en/validation.php b/application/language/en/validation.php similarity index 100% rename from laravel/language/en/validation.php rename to application/language/en/validation.php diff --git a/laravel/bootstrap/constants.php b/laravel/bootstrap/constants.php index 4b331d3a4..e2364d7ef 100644 --- a/laravel/bootstrap/constants.php +++ b/laravel/bootstrap/constants.php @@ -49,7 +49,6 @@ $constants = array( 'ROUTE_PATH' => APP_PATH.'routes/', 'SESSION_PATH' => STORAGE_PATH.'sessions/', 'SYS_CONFIG_PATH' => SYS_PATH.'config/', - 'SYS_LANG_PATH' => SYS_PATH.'language/', 'SYS_VIEW_PATH' => SYS_PATH.'views/', 'VIEW_PATH' => APP_PATH.'views/', ); diff --git a/laravel/bootstrap/errors.php b/laravel/bootstrap/errors.php index 877175e32..f61b406df 100644 --- a/laravel/bootstrap/errors.php +++ b/laravel/bootstrap/errors.php @@ -6,8 +6,12 @@ * error handler to create a more readable message. */ $message = function($e) -{ - $file = str_replace(array(APP_PATH, SYS_PATH), array('APP_PATH/', 'SYS_PATH/'), $e->getFile()); +{ + $search = array(APP_PATH, SYS_PATH); + + $replace = array('APP_PATH/', 'SYS_PATH/'); + + $file = str_replace($search, $replace, $e->getFile()); return rtrim($e->getMessage(), '.').' in '.$file.' on line '.$e->getLine().'.'; }; @@ -35,14 +39,23 @@ $severity = function($e) E_STRICT => 'Runtime Notice', ); - return (array_key_exists($e->getCode(), $levels)) ? $levels[$e->getCode()] : $e->getCode(); + if (array_key_exists($e->getCode(), $levels)) + { + $level = $levels[$e->getCode()]; + } + else + { + $level = $e->getCode(); + } + + return $level; }; /** * Create the exception handler function. All of the error handlers - * registered with PHP call this closure to keep the code D.R.Y. - * Each of the formatting closures defined above will be passed - * into the handler for convenient use. + * registered by the framework call this closure to avoid duplicate + * code. Each of the formatting closures defined above will be + * passed into the handler for convenient use. */ $handler = function($e) use ($message, $severity) { diff --git a/laravel/cache/drivers/driver.php b/laravel/cache/drivers/driver.php index 6476ded4a..dad7c77a5 100644 --- a/laravel/cache/drivers/driver.php +++ b/laravel/cache/drivers/driver.php @@ -61,7 +61,7 @@ abstract class Driver { * cache, store the default value in the cache and return it. * * - * // Get an item from the cache, or cache a value for 15 minutes if it doesn't exist + * // Get an item from the cache, or cache a value for 15 minutes * $name = Cache::remember('name', 'Taylor', 15); * * // Use a closure for deferred execution diff --git a/laravel/cache/manager.php b/laravel/cache/manager.php index 0e09632a0..1c88a8fc8 100644 --- a/laravel/cache/manager.php +++ b/laravel/cache/manager.php @@ -12,8 +12,8 @@ class Manager { /** * Get a cache driver instance. * - * If no driver name is specified, the default cache driver will be - * returned as defined in the cache configuration file. + * If no driver name is specified, the default cache driver will + * be returned as defined in the cache configuration file. * * * // Get the default cache driver instance diff --git a/laravel/config/container.php b/laravel/config/container.php index df4bcff5d..e4de06d0c 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -101,7 +101,7 @@ return array( { $memcache = new \Memcache; - foreach (Config::get('cache.servers') as $server) + foreach (Config::get('cache.memcached') as $server) { $memcache->addServer($server['host'], $server['port'], true, $server['weight']); } diff --git a/laravel/database/connection.php b/laravel/database/connection.php index c15660851..59bd9dd72 100644 --- a/laravel/database/connection.php +++ b/laravel/database/connection.php @@ -43,6 +43,36 @@ class Connection { $this->config = $config; } + /** + * Begin a fluent query against a table. + * + * @param string $table + * @return Query + */ + public function table($table) + { + return new Query($this, $this->grammar(), $table); + } + + /** + * Create a new query grammar for the connection. + * + * @return Grammars\Grammar + */ + protected function grammar() + { + if (isset($this->grammar)) return $this->grammar; + + switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver()) + { + case 'mysql': + return $this->grammar = new Grammars\MySQL; + + default: + return $this->grammar = new Grammars\Grammar; + } + } + /** * Execute a SQL query against the connection and return a single column result. * @@ -109,18 +139,18 @@ class Connection { */ public function query($sql, $bindings = array()) { - // First we need to remove all expressions from the bindings - // since they will be placed into the query as raw strings. + // Remove expressions from the bindings since they injected into + // the query as raw strings and are not bound parameters. foreach ($bindings as $key => $value) { if ($value instanceof Expression) unset($bindings[$key]); } - $sql = $this->transform($sql, $bindings); + $sql = $this->transform(trim($sql), $bindings); $this->queries[] = compact('sql', 'bindings'); - return $this->execute($this->pdo->prepare(trim($sql)), $bindings); + return $this->execute($this->pdo->prepare($sql), $bindings); } /** @@ -178,36 +208,6 @@ class Connection { return $statement->rowCount(); } - /** - * Begin a fluent query against a table. - * - * @param string $table - * @return Query - */ - public function table($table) - { - return new Query($this, $this->grammar(), $table); - } - - /** - * Create a new query grammar for the connection. - * - * @return Grammars\Grammar - */ - protected function grammar() - { - if (isset($this->grammar)) return $this->grammar; - - switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver()) - { - case 'mysql': - return $this->grammar = new Grammars\MySQL; - - default: - return $this->grammar = new Grammars\Grammar; - } - } - /** * Get the driver name for the database connection. * diff --git a/laravel/database/grammars/grammar.php b/laravel/database/grammars/grammar.php index 8dfbbfbf4..64dcbde44 100644 --- a/laravel/database/grammars/grammar.php +++ b/laravel/database/grammars/grammar.php @@ -47,8 +47,6 @@ class Grammar { { $sql = array(); - // Iterate through each query component, calling the compiler for that - // component and passing the query instance into the compiler. foreach ($this->components as $component) { if ( ! is_null($query->$component)) @@ -111,9 +109,6 @@ class Grammar { */ protected function joins(Query $query) { - // Since creating a JOIN clause using string concatenation is a little - // cumbersome, we will create a format we can pass to "sprintf" to - // make things cleaner. $format = '%s JOIN %s ON %s %s %s'; foreach ($query->joins as $join) @@ -281,9 +276,9 @@ class Grammar { // every insert to the table. $columns = $this->columnize(array_keys(reset($values))); - // Build the list of parameter place-holders for the array of values bound - // to the query. Each insert statement should have the same number of bound - // parameters, so we can just use the first array of values. + // Build the list of parameter place-holders of values bound to the query. + // Each insert should have the same number of bound paramters, so we can + // just use the first array of values. $parameters = $this->parameterize(reset($values)); $parameters = implode(', ', array_fill(0, count($values), '('.$parameters.')')); diff --git a/laravel/html.php b/laravel/html.php index 489714ed4..dc7e5a6f8 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -270,7 +270,14 @@ class HTML { foreach ($list as $key => $value) { - $html .= (is_array($value)) ? static::elements($type, $value) : '
  • '.static::entities($value).'
  • '; + if (is_array($value)) + { + $html .= static::elements($type, $value); + } + else + { + $html .= '
  • '.static::entities($value).'
  • '; + } } return '<'.$type.static::attributes($attributes).'>'.$html.''; diff --git a/laravel/lang.php b/laravel/lang.php index f475a2b66..cd45bf05b 100644 --- a/laravel/lang.php +++ b/laravel/lang.php @@ -37,7 +37,7 @@ class Lang { * * @var array */ - protected $paths = array(SYS_LANG_PATH, LANG_PATH); + protected $paths = array(LANG_PATH); /** * Create a new Lang instance. @@ -164,9 +164,6 @@ class Lang { $language = array(); - // Language files cascade. Typically, the system language array is - // loaded first, followed by the application array. This allows the - // convenient overriding of the system language files. foreach ($this->paths as $directory) { if (file_exists($path = $directory.$this->language.'/'.$file.EXT)) diff --git a/laravel/routing/controller.php b/laravel/routing/controller.php index 95aa5d45a..fd41c0cfe 100644 --- a/laravel/routing/controller.php +++ b/laravel/routing/controller.php @@ -60,13 +60,11 @@ abstract class Controller { } // The after filter and the framework expects all responses to - // be instances of the Response class. If the route did not + // be instances of the Response class. If the method did not // return an instsance of Response, we will make on now. if ( ! $response instanceof Response) $response = new Response($response); - $filters = array_merge($controller->filters('after'), array('after')); - - Filter::run($filters, array($response)); + Filter::run($controller->filters('after'), array($response)); return $response; } diff --git a/laravel/routing/route.php b/laravel/routing/route.php index 9aacb5802..47c106b5f 100644 --- a/laravel/routing/route.php +++ b/laravel/routing/route.php @@ -93,6 +93,12 @@ class Route { // Since "before" filters can halt the request cycle, we will return // any response from the before filters. Allowing filters to halt the // request cycle makes tasks like authorization convenient. + // + // The route is responsible for running the global filters, and any + // filters defined on the route itself. Since all incoming requests + // come through a route (either defined or ad-hoc), it makes sense + // to let the route handle the global filters. If the route uses + // a controller, the controller will only call its own filters. $before = array_merge(array('before'), $this->filters('before')); if ( ! is_null($response = Filter::run($before, array(), true))) @@ -104,21 +110,22 @@ class Route { { if ($response instanceof Delegate) { - return Controller::call($response->destination, $this->parameters); + $response = Controller::call($response->destination, $this->parameters); } - else + + // The after filter and the framework expects all responses to + // be instances of the Response class. If the route did not + // return an instsance of Response, we will make on now. + if ( ! $response instanceof Response) { - // The after filter and the framework expects all responses to - // be instances of the Response class. If the route did not - // return an instsance of Response, we will make on now. - if ( ! $response instanceof Response) $response = new Response($response); - - $filters = array_merge($this->filters('after'), array('after')); - - Filter::run($filters, array($response)); - - return $response; + $response = new Response($response); } + + $filters = array_merge($this->filters('after'), array('after')); + + Filter::run($filters, array($response)); + + return $response; } return Response::error('404'); diff --git a/laravel/routing/router.php b/laravel/routing/router.php index 32cf367e6..5b65944bf 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -153,7 +153,10 @@ class Router { foreach (explode(', ', $keys) as $key) { // Append the provided formats to the route as an optional regular expression. - if ( ! is_null($formats = $this->provides($callback))) $key .= '(\.('.implode('|', $formats).'))?'; + if ( ! is_null($formats = $this->provides($callback))) + { + $key .= '(\.('.implode('|', $formats).'))?'; + } if (preg_match('#^'.$this->wildcards($key).'$#', $destination)) { @@ -216,7 +219,9 @@ class Router { { foreach (array_reverse($segments, true) as $key => $value) { - if (file_exists($path = $this->controllers.implode('/', array_slice($segments, 0, $key + 1)).EXT)) + $controller = implode('/', array_slice($segments, 0, $key + 1)).EXT; + + if (file_exists($path = $this->controllers.$controller)) { return $key + 1; } diff --git a/laravel/security/auth.php b/laravel/security/auth.php index 08d0faca7..77c3ad79e 100644 --- a/laravel/security/auth.php +++ b/laravel/security/auth.php @@ -1,6 +1,5 @@ * // Flash an item to the session @@ -236,9 +236,6 @@ class Manager { { static::$session['last_activity'] = time(); - // To age the data, we will forget all of the old keys and then - // rewrite the newly flashed items to have old keys, which will - // be available for the next request. foreach (static::$session['data'] as $key => $value) { if (strpos($key, ':old:') === 0) static::forget($key); diff --git a/laravel/validation/validator.php b/laravel/validation/validator.php index 008cee300..b622f26de 100644 --- a/laravel/validation/validator.php +++ b/laravel/validation/validator.php @@ -281,7 +281,7 @@ class Validator { */ protected function validate_size($attribute, $value, $parameters) { - return $this->get_size($attribute, $value) == $parameters[0]; + return $this->size($attribute, $value) == $parameters[0]; } /** @@ -294,7 +294,7 @@ class Validator { */ protected function validate_between($attribute, $value, $parameters) { - return $this->get_size($attribute, $value) >= $parameters[0] and $this->get_size($attribute, $value) <= $parameters[1]; + return $this->size($attribute, $value) >= $parameters[0] and $this->size($attribute, $value) <= $parameters[1]; } /** @@ -307,7 +307,7 @@ class Validator { */ protected function validate_min($attribute, $value, $parameters) { - return $this->get_size($attribute, $value) >= $parameters[0]; + return $this->size($attribute, $value) >= $parameters[0]; } /** @@ -320,7 +320,7 @@ class Validator { */ protected function validate_max($attribute, $value, $parameters) { - return $this->get_size($attribute, $value) <= $parameters[0]; + return $this->size($attribute, $value) <= $parameters[0]; } /** @@ -335,7 +335,7 @@ class Validator { * @param mixed $value * @return mixed */ - protected function get_size($attribute, $value) + protected function size($attribute, $value) { if (is_numeric($value) and $this->has_rule($attribute, $this->numeric_rules)) { diff --git a/laravel/view.php b/laravel/view.php index 55f15457a..4ec7cdf77 100644 --- a/laravel/view.php +++ b/laravel/view.php @@ -133,10 +133,9 @@ class View { { if (is_null(static::$composers)) static::$composers = require APP_PATH.'composers'.EXT; - // The view's name may specified in several different ways in the - // composers file. The composer may simple have a string value, - // which is the name. Or, it may an array value in which a - // "name" key exists. + // The view's name may specified in several different ways in the composers + // file. The composer may simple have a string value, which is the name. + // Or, it may an array value in which a "name" key exists. foreach (static::$composers as $key => $value) { if ($name === $value or (is_array($value) and $name === Arr::get($value, 'name'))) @@ -174,10 +173,10 @@ class View { { static::compose($this); - // All nested views and responses are evaluated before the - // main view. This allows the assets used by these views to - // be added to the asset container before the main view is - // evaluated and dumps the links to the assets. + // All nested views and responses are evaluated before the main view. + // This allows the assets used by the nested views to be added to the + // asset container before the main view is evaluated and dumps the + // links to the assets. foreach ($this->data as &$data) { if ($data instanceof View or $data instanceof Response) @@ -188,9 +187,9 @@ class View { ob_start() and extract($this->data, EXTR_SKIP); - // If the view is a "Blade" view, we need to check the view for - // modifications and get the path to the compiled view file. - // Otherwise, we'll just use the regular path to the view. + // If the view is Bladed, we need to check the view for modifications + // and get the path to the compiled view file. Otherwise, we'll just + // use the regular path to the view. $view = (strpos($this->path, BLADE_EXT) !== false) ? $this->compile() : $this->path; try { include $view; } catch (Exception $e) { ob_get_clean(); throw $e; }