From 9fc9f88a41600596d3a9e5328ae61044dd36eb2d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 18 Oct 2011 20:19:36 -0500 Subject: [PATCH] refactoring various pieces of the framework. --- application/filters.php | 7 +++-- laravel/bootstrap/constants.php | 44 ++++++++++++++++++++++++++++++ laravel/bootstrap/core.php | 32 ++-------------------- laravel/database/connection.php | 4 +-- laravel/form.php | 33 +++++++++++++++++----- laravel/html.php | 15 +++++----- laravel/language/en/pagination.php | 14 ++++++++++ laravel/language/en/validation.php | 32 ++++++++++++++++++++++ laravel/laravel.php | 4 ++- laravel/proxy.php | 21 -------------- laravel/request.php | 7 ++--- laravel/response.php | 29 ++++++++++++-------- laravel/routing/loader.php | 3 ++ laravel/security/crypter.php | 20 ++++++++------ laravel/session/manager.php | 8 +----- laravel/validation/validator.php | 4 ++- laravel/view.php | 26 ++++++++++-------- public/index.php | 4 ++- 18 files changed, 193 insertions(+), 114 deletions(-) create mode 100644 laravel/bootstrap/constants.php delete mode 100644 laravel/proxy.php diff --git a/application/filters.php b/application/filters.php index 01a70efc..2bf71db7 100644 --- a/application/filters.php +++ b/application/filters.php @@ -50,13 +50,16 @@ 'after' => function($response) { - // Do stuff after every request to your application. + if (Config::get('session.driver') !== '') + { + Session::flash(Input::old_input, Input::old()); + } }, 'auth' => function() { - if ( ! Auth::check()) return Redirect::to('login'); + if ( ! Auth::check()) return Redirect::to_login(); }, diff --git a/laravel/bootstrap/constants.php b/laravel/bootstrap/constants.php new file mode 100644 index 00000000..56710afb --- /dev/null +++ b/laravel/bootstrap/constants.php @@ -0,0 +1,44 @@ + $value) + { + if ( ! defined($key)) define($key, $value); + } +} + +$constants = array( + 'APP_PATH' => realpath($application).'/', + 'BASE_PATH' => realpath("$laravel/..").'/', + 'PACKAGE_PATH' => realpath($packages).'/', + 'PUBLIC_PATH' => realpath($public).'/', + 'STORAGE_PATH' => realpath($storage).'/', + 'SYS_PATH' => realpath($laravel).'/', +); + +constants($constants); + +$constants = array( + 'CACHE_PATH' => STORAGE_PATH.'cache/', + 'CONFIG_PATH' => APP_PATH.'config/', + 'CONTROLLER_PATH' => APP_PATH.'controllers/', + 'DATABASE_PATH' => STORAGE_PATH.'database/', + 'LANG_PATH' => APP_PATH.'language/', + 'LIBRARY_PATH' => APP_PATH.'libraries/', + 'MODEL_PATH' => APP_PATH.'models/', + '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/', +); + + +constants($constants); + +unset($constants); \ No newline at end of file diff --git a/laravel/bootstrap/core.php b/laravel/bootstrap/core.php index 02e94a5a..3d758081 100644 --- a/laravel/bootstrap/core.php +++ b/laravel/bootstrap/core.php @@ -1,43 +1,15 @@ $value) { if ($value instanceof Expression) unset($bindings[$key]); diff --git a/laravel/form.php b/laravel/form.php index ae9c4298..5f2b810b 100644 --- a/laravel/form.php +++ b/laravel/form.php @@ -43,9 +43,9 @@ class Form { */ public static function open($action = null, $method = 'POST', $attributes = array(), $https = false) { - $attributes['action'] = static::action($action, $https); - $attributes['method'] = static::method($method); + + $attributes['action'] = static::action($action, $https); if ( ! array_key_exists('accept-charset', $attributes)) { @@ -207,7 +207,9 @@ public static function input($type, $name, $value = null, $attributes = array()) $id = static::id($name, $attributes); - return ''.PHP_EOL; + $attributes = array_merge($attributes, compact('type', 'name', 'value', 'id')); + + return ''.PHP_EOL; } /** @@ -363,20 +365,37 @@ public static function textarea($name, $value = '', $attributes = array()) */ public static function select($name, $options = array(), $selected = null, $attributes = array()) { - $attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'name' => $name)); + $attributes['id'] = static::id($name, $attributes); + + $attributes['name'] = $name; $html = array(); foreach ($options as $value => $display) { - $option_attributes = array('value' => HTML::entities($value), 'selected' => ($value == $selected) ? 'selected' : null); - - $html[] = ''.HTML::entities($display).''; + $html[] = static::option($value, $display, $selected); } return ''.implode('', $html).''.PHP_EOL; } + /** + * Create a HTML select element option. + * + * @param string $value + * @param string $display + * @return string $selected + * @return string + */ + protected static function option($value, $display, $selected) + { + $selected = ($value === $selected) ? 'selected' : null; + + $attributes = array('value' => HTML::entities($value), 'selected' => $selected); + + return ''.HTML::entities($display).''; + } + /** * Create a HTML checkbox input element. * diff --git a/laravel/html.php b/laravel/html.php index 953a5397..489714ed 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -34,7 +34,7 @@ public static function script($url, $attributes = array()) { $url = static::entities(URL::to_asset($url)); - return ''.PHP_EOL; + return ''.PHP_EOL; } /** @@ -241,7 +241,7 @@ public static function image($url, $alt = '', $attributes = array()) */ public static function ol($list, $attributes = array()) { - return static::list_elements('ol', $list, $attributes); + return static::listing('ol', $list, $attributes); } /** @@ -253,7 +253,7 @@ public static function ol($list, $attributes = array()) */ public static function ul($list, $attributes = array()) { - return static::list_elements('ul', $list, $attributes); + return static::listing('ul', $list, $attributes); } /** @@ -264,13 +264,13 @@ public static function ul($list, $attributes = array()) * @param array $attributes * @return string */ - private static function list_elements($type, $list, $attributes = array()) + private static function listing($type, $list, $attributes = array()) { $html = ''; foreach ($list as $key => $value) { - $html .= (is_array($value)) ? static::list_elements($type, $value) : '
  • '.static::entities($value).'
  • '; + $html .= (is_array($value)) ? static::elements($type, $value) : '
  • '.static::entities($value).'
  • '; } return '<'.$type.static::attributes($attributes).'>'.$html.''; @@ -279,6 +279,9 @@ private static function list_elements($type, $list, $attributes = array()) /** * Build a list of HTML attributes from an array. * + * Numeric-keyed attributes will be assigned the same key and value to handle + * attributes such as "autofocus" and "required". + * * @param array $attributes * @return string */ @@ -288,8 +291,6 @@ public static function attributes($attributes) foreach ((array) $attributes as $key => $value) { - // Assume numeric-keyed attributes to have the same key and value. - // Example: required="required", autofocus="autofocus", etc. if (is_numeric($key)) $key = $value; if ( ! is_null($value)) diff --git a/laravel/language/en/pagination.php b/laravel/language/en/pagination.php index c4e503d6..db6f5fa4 100644 --- a/laravel/language/en/pagination.php +++ b/laravel/language/en/pagination.php @@ -2,6 +2,20 @@ return array( + /* + |-------------------------------------------------------------------------- + | Pagination Language Lines + |-------------------------------------------------------------------------- + | + | The following language lines are used by the paginator library to build + | the pagination links. They may be easily changed by the developer to + | anything they wish. + | + | The "status" line has two place-holders, :current and :last, for which + | the current and last page numbers are substituted, respectively. + | + */ + 'first' => 'First', 'previous' => '← Previous', 'status' => 'Page :current of :last', diff --git a/laravel/language/en/validation.php b/laravel/language/en/validation.php index 79de7c3f..edd77e71 100644 --- a/laravel/language/en/validation.php +++ b/laravel/language/en/validation.php @@ -2,6 +2,38 @@ return array( + /* + |-------------------------------------------------------------------------- + | Validation Attribute Language Lines + |-------------------------------------------------------------------------- + | + | The following language lines are used to swap attribute place-holders + | with something more reader friendly, such as "E-Mail Address" instead + | of "email". + | + | The Validator class will automatically search this array of lines when + | attempting to replace the :attribute place-holder in error messages. + | + */ + + 'attributes' => array(), + + /* + |-------------------------------------------------------------------------- + | Validation Language Lines + |-------------------------------------------------------------------------- + | + | The following language lines contain the default error messages used + | by the validator class. Some of the rules contain multiple versions, + | such as the size (max, min, between) rules. These versions are used + | for different input types such as strings and files. + | + | These language lines may be easily changed by the developer to provide + | custom error messages in their application. Error messages for custom + | validation rules may also be added to this file. + | + */ + "accepted" => "The :attribute must be accepted.", "active_url" => "The :attribute is not a valid URL.", "alpha" => "The :attribute may only contain letters.", diff --git a/laravel/laravel.php b/laravel/laravel.php index 8aa41098..86ef75d3 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -37,7 +37,9 @@ * Manually load some core classes that are used on every request * This allows to avoid using the loader for these classes. */ +require SYS_PATH.'input'.EXT; require SYS_PATH.'request'.EXT; +require SYS_PATH.'response'.EXT; require SYS_PATH.'routing/route'.EXT; require SYS_PATH.'routing/router'.EXT; require SYS_PATH.'routing/loader'.EXT; @@ -117,7 +119,7 @@ */ if (Config::$items['session']['driver'] !== '') { - Session\Manager::close(array(Input::old_input => Input::get())); + Session\Manager::close(); } /** diff --git a/laravel/proxy.php b/laravel/proxy.php deleted file mode 100644 index c81e9390..00000000 --- a/laravel/proxy.php +++ /dev/null @@ -1,21 +0,0 @@ -headers['Content-Type'])) - { - $this->header('Content-Type', 'text/html; charset=utf-8'); - } - - if ( ! headers_sent()) $this->send_headers(); + if ( ! headers_sent()) $this->headers(); echo $this->render(); } /** - * Send the response headers to the browser. + * Send all of the response headers to the browser. + * + * The develop may set any response headers they wish using the "header" method. + * All of the headers set by the developer will be automatically sent to the + * browser when the response is sent via the "send" method. There is no need + * to call this method before calling the "send" method. + * + * The protocol and status header will be set automatically, as well as the + * content-type and charset, unless those headers have been set explicitly. + * The content-type charset used will be the application encoding. * * @return void */ - public function send_headers() + public function headers() { - $protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; + if ( ! isset($this->headers['Content-Type'])) + { + $this->header('Content-Type', 'text/html; charset='.Config::$items['application']['encoding']); + } - header($protocol.' '.$this->status.' '.$this->statuses[$this->status]); + header(Request::protocol().' '.$this->status.' '.$this->statuses[$this->status]); foreach ($this->headers as $name => $value) { diff --git a/laravel/routing/loader.php b/laravel/routing/loader.php index 47c656a6..73240727 100644 --- a/laravel/routing/loader.php +++ b/laravel/routing/loader.php @@ -92,6 +92,9 @@ public function everything() $routes = array(); + // First, we'll grab the base routes from the application directory. + // Once we have these, we'll merge all of the nested routes in the + // routes directory into this array of routes. if (file_exists($path = $this->base.'routes'.EXT)) { $routes = array_merge($routes, require $path); diff --git a/laravel/security/crypter.php b/laravel/security/crypter.php index a85fd7d8..6312f900 100644 --- a/laravel/security/crypter.php +++ b/laravel/security/crypter.php @@ -72,25 +72,29 @@ public static function encrypt($value) */ public static function decrypt($value) { - // Since all encrypted strings generated by this class are base64 - // encoded, we will first attempt to base64 decode the string. - // If we can't do it, we'll bail out. if ( ! is_string($value = base64_decode($value, true))) { throw new \Exception('Decryption error. Input value is not valid base64 data.'); } - // Extract the input vector and the encrypted string from the value. - // These will be used by Mcrypt to properly decrypt the value. - $iv = substr($value, 0, static::iv_size()); - - $value = substr($value, static::iv_size()); + list($iv, $value) = static::parse($value); $key = Config::$items['application']['key']; return rtrim(mcrypt_decrypt(static::$cipher, $key, $value, static::$mode, $iv), "\0"); } + /** + * Parse an encrypted value into the input vector and the actual value. + * + * @param string $value + * @return array + */ + protected static function parse($value) + { + return array(substr($value, 0, static::iv_size()), substr($value, static::iv_size())); + } + /** * Get the input vector size for the cipher and mode. * diff --git a/laravel/session/manager.php b/laravel/session/manager.php index 5e9728da..30baf671 100644 --- a/laravel/session/manager.php +++ b/laravel/session/manager.php @@ -269,18 +269,12 @@ protected static function replace($search, $replace, $keys) /** * Close the session handling for the request. * - * @param array $flash * @return void */ - public static function close($flash = array()) + public static function close() { $config = Config::$items['session']; - foreach ($flash as $key => $value) - { - static::flash($key, $value); - } - static::$driver->save(static::age(), $config, static::$exists); static::$transporter->put(static::$session['id'], $config); diff --git a/laravel/validation/validator.php b/laravel/validation/validator.php index 821db832..008cee30 100644 --- a/laravel/validation/validator.php +++ b/laravel/validation/validator.php @@ -563,7 +563,9 @@ protected function replace($message, $attribute, $rule, $parameters) // Except for "between" every replacement should be the first parameter. $max = ($rule == 'between') ? $parameters[1] : $parameters[0]; - $message = str_replace(array(':size', ':min', ':max'), array($parameters[0], $parameters[0], $max), $message); + $replace = array($parameters[0], $parameters[0], $max); + + $message = str_replace(array(':size', ':min', ':max'), $replace, $message); } elseif (in_array($rule, $this->inclusion_rules)) { diff --git a/laravel/view.php b/laravel/view.php index 1a1fe429..55f15457 100644 --- a/laravel/view.php +++ b/laravel/view.php @@ -133,12 +133,16 @@ protected static function name($name) { 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'))) return $key; + if ($name === $value or (is_array($value) and $name === Arr::get($value, 'name'))) + { + return $key; + } } } @@ -170,10 +174,10 @@ public function render() { 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 these 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) @@ -184,9 +188,9 @@ public function render() 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 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. $view = (strpos($this->path, BLADE_EXT) !== false) ? $this->compile() : $this->path; try { include $view; } catch (Exception $e) { ob_get_clean(); throw $e; } diff --git a/public/index.php b/public/index.php index 06006852..c39e1cd0 100644 --- a/public/index.php +++ b/public/index.php @@ -13,6 +13,7 @@ | Tick... Tock... Tick... Tock |-------------------------------------------------------------------------- */ + define('START_TIME', microtime(true)); /* @@ -43,6 +44,7 @@ | 3... 2... 1... Lift-off! |-------------------------------------------------------------------------- */ + require $laravel.'/laravel.php'; -//echo number_format((microtime(true) - START_TIME) * 1000, 2); \ No newline at end of file +echo number_format((microtime(true) - START_TIME) * 1000, 2); \ No newline at end of file