From 77dc8d2014f990f9350bf2baad31a684e4cbb257 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 16 Sep 2011 19:59:20 -0500 Subject: [PATCH] refactoring various things. --- laravel/arr.php | 47 +++++++++++--- laravel/asset.php | 42 ++++++++++--- laravel/bootstrap.php | 11 +++- laravel/config.php | 118 +++++++++++------------------------ laravel/config/container.php | 2 + laravel/database/manager.php | 9 +-- laravel/form.php | 2 +- laravel/request.php | 21 ++++--- laravel/response.php | 5 +- public/index.php | 2 + 10 files changed, 139 insertions(+), 120 deletions(-) diff --git a/laravel/arr.php b/laravel/arr.php index 35c7afc0..ca88753c 100644 --- a/laravel/arr.php +++ b/laravel/arr.php @@ -1,15 +1,21 @@ + * // Get the value of $array['user']['name'] + * $value = Arr::get($array, 'user.name'); + * + * // Get a value from the array, but return a default if it doesn't exist + * $value = Arr::get($array, 'user.name', 'Taylor'); + * * * @param array $array * @param string $key @@ -24,7 +30,7 @@ class Arr { { if ( ! is_array($array) or ! array_key_exists($segment, $array)) { - return ($default instanceof Closure) ? call_user_func($default) : $default; + return ($default instanceof \Closure) ? call_user_func($default) : $default; } $array = $array[$segment]; @@ -36,9 +42,16 @@ class Arr { /** * Set an array item to a given value. * - * This method is primarly helpful for setting the value in an array with - * a variable depth, such as configuration arrays. Like the Arr::get - * method, JavaScript "dot" syntax is supported. + * This method supports accessing arrays through JavaScript "dot" style syntax + * for conveniently digging deep into nested arrays. + * + * + * // Set the $array['user']['name'] value in the array + * Arr::set($array, 'user.name', 'Taylor'); + * + * // Set the $array['db']['driver']['name'] value in the array + * Arr::set($array, 'db.driver.name', 'SQLite'); + * * * @param array $array * @param string $key @@ -69,6 +82,20 @@ class Arr { /** * Return the first element in an array which passes a given truth test. * + * The truth test is passed as a closure, and simply returns true or false. + * The array key and value will be passed to the closure on each iteration. + * + * Like the "get" method, a default value may be specified, and will be + * returned if no matching array elements are found by the method. + * + * + * // Get the first string from an array with a length of 3 + * $value = Arr::first($array, function($k, $v) {return strlen($v) == 3;}); + * + * // Return a default value if no matching array elements are found + * $value = Arr::first($array, function($k, $v) {return;}, 'Default'); + * + * * @param array $array * @param Closure $callback * @return mixed @@ -80,7 +107,7 @@ class Arr { if (call_user_func($callback, $key, $value)) return $value; } - return ($default instanceof Closure) ? call_user_func($default) : $default; + return ($default instanceof \Closure) ? call_user_func($default) : $default; } } \ No newline at end of file diff --git a/laravel/asset.php b/laravel/asset.php index 0a4ee68e..1bb2cc98 100644 --- a/laravel/asset.php +++ b/laravel/asset.php @@ -35,10 +35,10 @@ class Asset { * expressive code and a clean API. * * - * // Get the default asset container + * // Get an instance of the default asset container * $container = Asset::container(); * - * // Get the "footer" asset container + * // Get an instance of the "footer" container * $container = Asset::container('footer'); * * @@ -58,12 +58,9 @@ class Asset { /** * Magic Method for calling methods on the default Asset container. * - * This provides a convenient API, allowing the developer to skip the - * "container" method when using the default container. - * * - * // Add a JavaScript file to the default container - * Asset::script('jquery', 'js/jquery.js'); + * // Call the "add" method on the default asset container + * Asset::add('jquery', 'js/jquery.js'); * * // Get all of the styles from the default container * echo Asset::styles(); @@ -125,10 +122,13 @@ class Asset_Container { * * * // Add an asset to the container - * Asset::add('jquery', 'js/jquery.js'); + * Asset::container()->add('style', 'style.css'); * - * // Add an asset that has dependencies - * Asset::add('jquery', 'js/jquery.js', array('jquery-ui')); + * // Add an asset to the container with attributes + * Asset::container()->add('style', 'style.css', array(), array('media' => 'print')); + * + * // Add an asset to the container with dependencies + * Asset::container()->add('jquery', 'jquery.js', array('jquery-ui')); * * * @param string $name @@ -147,6 +147,17 @@ class Asset_Container { /** * Add a CSS file to the registered assets. * + * + * // Add a CSS file to the registered assets + * Asset::container()->style('common', 'common.css'); + * + * // Add a CSS file with dependencies to the registered assets + * Asset::container()->style('common', 'common.css', array('reset')); + * + * // Add a CSS file with attributes to the registered assets + * Asset::container()->style('common', 'common.css', array(), array('media' => 'print')); + * + * * @param string $name * @param string $source * @param array $dependencies @@ -168,6 +179,17 @@ class Asset_Container { /** * Add a JavaScript file to the registered assets. * + * + * // Add a CSS file to the registered assets + * Asset::container()->script('jquery', 'jquery.js'); + * + * // Add a CSS file with dependencies to the registered assets + * Asset::container()->script('jquery', 'jquery.js', array('jquery-ui')); + * + * // Add a CSS file with attributes to the registered assets + * Asset::container()->script('loader', 'loader.js', array(), array('defer')); + * + * * @param string $name * @param string $source * @param array $dependencies diff --git a/laravel/bootstrap.php b/laravel/bootstrap.php index 5ad080f8..cb7df0d7 100644 --- a/laravel/bootstrap.php +++ b/laravel/bootstrap.php @@ -49,7 +49,9 @@ if (file_exists($path = CONFIG_PATH.'container'.EXT)) $dependencies = array_merge($dependencies, require $path); } -if (isset($_SERVER['LARAVEL_ENV']) and file_exists($path = CONFIG_PATH.$_SERVER['LARAVEL_ENV'].'/container'.EXT)) +$env = (isset($_SERVER['LARAVEL_ENV'])) ? $_SERVER['LARAVEL_ENV'] : null; + +if ( ! is_null($env) and file_exists($path = CONFIG_PATH.$env.'/container'.EXT)) { $dependencies = array_merge($dependencies, require $path); } @@ -61,4 +63,9 @@ IoC::$container = $container; // -------------------------------------------------------------- // Register the auto-loader on the auto-loader stack. // -------------------------------------------------------------- -spl_autoload_register(array($container->resolve('laravel.loader'), 'load')); \ No newline at end of file +spl_autoload_register(array($container->resolve('laravel.loader'), 'load')); + +// -------------------------------------------------------------- +// Set the application environment configuration option. +// -------------------------------------------------------------- +$container->resolve('laravel.config')->set('application.env', $env); \ No newline at end of file diff --git a/laravel/config.php b/laravel/config.php index 2cd2a4b6..00ce2873 100644 --- a/laravel/config.php +++ b/laravel/config.php @@ -5,33 +5,32 @@ class Config { /** * All of the loaded configuration items. * - * The configuration arrays are keyed by their owning file name. - * * @var array */ - protected $items = array(); - - /** - * The paths to the configuration files. - * - * @var array - */ - protected $paths = array(); + protected $config = array(); /** * Create a new configuration manager instance. * - * @param array $paths + * @param array $config * @return void */ - public function __construct($paths) + public function __construct($config) { - $this->paths = $paths; + $this->config = $config; } /** * Determine if a configuration item or file exists. * + * + * // Determine if the "options" configuration file exists + * $options = Config::has('options'); + * + * // Determine if a specific configuration item exists + * $timezone = Config::has('application.timezone'); + * + * * @param string $key * @return bool */ @@ -43,18 +42,23 @@ class Config { /** * Get a configuration item. * - * If the name of a configuration file is passed without specifying an item, the - * entire configuration array will be returned. + * Configuration items are stored in the application/config directory, and provide + * general configuration options for a wide range of Laravel facilities. + * + * The arrays may be accessed using JavaScript style "dot" notation to drill deep + * intot he configuration files. For example, asking for "database.connectors.sqlite" + * would return the connector closure for SQLite stored in the database configuration + * file. If no specific item is specfied, the entire configuration array is returned. + * + * Like most Laravel "get" functions, a default value may be provided, and it will + * be returned if the requested file or item doesn't exist. * * - * // Get the "timezone" option from the "application" file + * // Get the "timezone" option from the application config file * $timezone = Config::get('application.timezone'); * - * // Get the SQLite connection configuration from the "database" file - * $sqlite = Config::get('database.connections.sqlite'); - * - * // Get a configuration option and return "Fred" if it doesn't exist - * $option = Config::get('config.option', 'Fred'); + * // Get an option, but return a default value if it doesn't exist + * $value = Config::get('some.option', 'Default'); * * * @param string $key @@ -63,30 +67,25 @@ class Config { */ public function get($key, $default = null) { - list($file, $key) = $this->parse($key); - - if ( ! $this->load($file)) - { - return ($default instanceof \Closure) ? call_user_func($default) : $default; - } - - if (is_null($key)) return $this->items[$file]; - - return Arr::get($this->items[$file], $key, $default); + return Arr::get($this->items, $key, $default); } /** * Set a configuration item. * - * If a specific configuration item is not specified, the entire configuration - * array will be replaced with the given value. + * Configuration items are stored in the application/config directory, and provide + * general configuration options for a wide range of Laravel facilities. + * + * Like the "get" method, this method uses JavaScript style "dot" notation to access + * and manipulate the arrays in the configuration files. Also, like the "get" method, + * if no specific item is specified, the entire configuration array will be set. * * - * // Set the "timezone" option in the "application" file + * // Set the "timezone" option in the "application" array * Config::set('application.timezone', 'America/Chicago'); * - * // Set the entire "session" array to an empty array - * Config::set('session', array()); + * // Set the entire "session" configuration array + * Config::set('session', $array); * * * @param string $key @@ -95,52 +94,7 @@ class Config { */ public function set($key, $value) { - list($file, $key) = $this->parse($key); - - $this->load($file); - - (is_null($key)) ? Arr::set($this->items, $file, $value) : Arr::set($this->items[$file], $key, $value); - } - - /** - * Parse a configuration key and return its file and key segments. - * - * Configuration keys follow a {file}.{key} convention. - * - * @param string $key - * @return array - */ - protected function parse($key) - { - $segments = explode('.', $key); - - $key = (count($segments) > 1) ? implode('.', array_slice($segments, 1)) : null; - - return array($segments[0], $key); - } - - /** - * Load all of the configuration items from a module configuration file. - * - * If the configuration file has already been loaded, it will not be loaded again. - * - * @param string $file - * @return bool - */ - protected function load($file) - { - if (isset($this->items[$file])) return true; - - $config = array(); - - foreach ($this->paths as $directory) - { - $config = (file_exists($path = $directory.$file.EXT)) ? array_merge($config, require $path) : $config; - } - - if (count($config) > 0) $this->items[$file] = $config; - - return isset($this->items[$file]); + Arr::set($this->items, $key, $value); } } \ No newline at end of file diff --git a/laravel/config/container.php b/laravel/config/container.php index ef347996..b38024f9 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -108,6 +108,8 @@ return array( ($request->spoofed()) ? $input = $_POST : parse_str(file_get_contents('php://input'), $input); } + unset($input['_REQUEST_METHOD_']); + return new Input($container->resolve('laravel.file'), $container->resolve('laravel.cookie'), $input, $_FILES); }), diff --git a/laravel/database/manager.php b/laravel/database/manager.php index 33d0022c..0c29e294 100644 --- a/laravel/database/manager.php +++ b/laravel/database/manager.php @@ -21,10 +21,9 @@ class Manager { } /** - * Get a database connection. + * Get a database connection. * - * If no database name is specified, the default connection will be returned as - * defined in the database configuration file. + * If no database name is specified, the default connection will be returned. * * Note: Database connections are managed as singletons. * @@ -46,7 +45,9 @@ class Manager { // This provides the developer the maximum amount of freedom in establishing their // database connections, and allows the framework to remain agonstic to ugly database // specific PDO connection details. Less code. Less bugs. - $this->connections[$connection] = new Connection(call_user_func($this->config['connectors'][$connection], $this->config)); + $pdo = call_user_func($this->config['connectors'][$connection]); + + $this->connections[$connection] = new Connection($pdo, $this->config)); } return $this->connections[$connection]; diff --git a/laravel/form.php b/laravel/form.php index 73d607b7..1082b7f5 100644 --- a/laravel/form.php +++ b/laravel/form.php @@ -81,7 +81,7 @@ class Form { $attributes['accept-charset'] = $this->html->encoding; } - $append = ($method == 'PUT' or $method == 'DELETE') ? $this->hidden('REQUEST_METHOD', $method) : ''; + $append = ($method == 'PUT' or $method == 'DELETE') ? $this->hidden('_REQUEST_METHOD', $method) : ''; return 'html->attributes($attributes).'>'.$append.PHP_EOL; } diff --git a/laravel/request.php b/laravel/request.php index 187e20a6..9d99a3f3 100644 --- a/laravel/request.php +++ b/laravel/request.php @@ -9,13 +9,6 @@ class Request { */ public $server; - /** - * The route handling the current request. - * - * @var Routing\Route - */ - public $route; - /** * The $_POST array for the request. * @@ -40,6 +33,13 @@ class Request { */ protected $uri; + /** + * The route handling the current request. + * + * @var Routing\Route + */ + public $route; + /** * Create a new request instance. * @@ -84,7 +84,10 @@ class Request { throw new \Exception('Unable to determine the request URI.'); } - if ($uri === false) throw new \Exception('Malformed request URI. Request terminated.'); + if ($uri === false) + { + throw new \Exception('Malformed request URI. Request terminated.'); + } foreach (array(parse_url($this->url, PHP_URL_PATH), '/index.php') as $value) { @@ -117,7 +120,7 @@ class Request { */ public function method() { - return ($this->spoofed()) ? $this->post['REQUEST_METHOD'] : $this->server['REQUEST_METHOD']; + return ($this->spoofed()) ? $this->post['_REQUEST_METHOD'] : $this->server['REQUEST_METHOD']; } /** diff --git a/laravel/response.php b/laravel/response.php index 8300cbe0..63b26f76 100644 --- a/laravel/response.php +++ b/laravel/response.php @@ -263,8 +263,9 @@ class Response { /** * Send the response to the browser. * - * All of the response header will be sent to the browser first, followed by the content - * of the response instance, which will be evaluated and rendered by the render method. + * All of the response header will be sent to the browser first, followed by + * the content of the response instance, which will be evaluated and rendered + * by the render method. * * @return void */ diff --git a/public/index.php b/public/index.php index 58032d5d..17627ad5 100644 --- a/public/index.php +++ b/public/index.php @@ -49,3 +49,5 @@ $public = __DIR__; |-------------------------------------------------------------------------- */ require $laravel.'/laravel.php'; + +echo elapsed(); \ No newline at end of file