From d4c5114792972038e55d8509e9277572aec8c475 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 25 Jan 2012 16:07:16 -0600 Subject: [PATCH] code quality and backreference support in controllers. --- laravel/database/grammar.php | 6 +++++- laravel/database/schema.php | 5 ++--- laravel/routing/controller.php | 31 ++++++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/laravel/database/grammar.php b/laravel/database/grammar.php index 5bc8621b..a69faa7f 100644 --- a/laravel/database/grammar.php +++ b/laravel/database/grammar.php @@ -30,7 +30,11 @@ public function wrap($value) { $segments = explode(' ', $value); - return $this->wrap($segments[0]).' AS '.$this->wrap($segments[2]); + return sprintf( + '%s AS %s', + $this->wrap($segments[0]), + $this->wrap($segments[2]) + ); } // Since columns may be prefixed with their corresponding table diff --git a/laravel/database/schema.php b/laravel/database/schema.php index 15767f28..6897f7be 100644 --- a/laravel/database/schema.php +++ b/laravel/database/schema.php @@ -75,9 +75,8 @@ protected static function implications($table) } // For some extra syntax sugar, we'll check for any implicit - // indexes on the table. The developer may specify the index - // type on the fluent column declaration. Here we'll find - // any implicit indexes and add the commands. + // indexes on the table since the developer may specify the + // index type on the fluent column declaration. foreach ($table->columns as $column) { foreach (array('primary', 'unique', 'fulltext', 'index') as $key) diff --git a/laravel/routing/controller.php b/laravel/routing/controller.php index c32f4edd..2d5b0a03 100644 --- a/laravel/routing/controller.php +++ b/laravel/routing/controller.php @@ -57,16 +57,41 @@ public static function call($destination, $parameters = array()) list($controller, $method) = explode('@', $destination); + list($method, $parameters) = static::backreference($method, $parameters); + $controller = static::resolve($bundle, $controller); - // If the controller could not be resolved, we're out of options and will - // return the 404 error response. Of course, if we found the controller, - // we can go ahead and execute the requested method on the instance. + // If the controller could not be resolved, we're out of options and + // will return the 404 error response. If we found the controller, + // we can execute the requested method on the instance. if (is_null($controller)) return Response::error('404'); return $controller->execute($method, $parameters); } + /** + * Replace all back-references on the given method. + * + * @param string $method + * @param array $parameters + * @return array + */ + protected static function backreference($method, $parameters) + { + // Controller delegates may use back-references to the action parameters, + // which allows the developer to setup more flexible rouets to their + // controllers with less code. We will replace the back-references + // with their corresponding parameter value. + foreach ($parameters as $key => $value) + { + $method = str_replace('(:'.($key + 1).')', $value, $method, $count); + + if ($count > 0) unset($parameters[$key]); + } + + return array(str_replace('$1', 'index', $method), $parameters); + } + /** * Resolve a bundle and controller name to a controller instance. *