From 71b0ab8b8d482afee92777d8bf6a002c7359481f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 5 Oct 2011 18:32:48 -0500 Subject: [PATCH] refactoring container for speed. --- application/config/aliases.php | 104 +++---- application/config/application.php | 228 +++++++------- application/config/auth.php | 126 ++++---- application/config/cache.php | 102 +++---- application/config/database.php | 144 ++++----- application/config/error.php | 172 +++++------ application/config/session.php | 186 ++++++------ application/routes.php | 2 +- laravel/bootstrap/core.php | 4 +- laravel/config.php | 306 +++++++++---------- laravel/config/container.php | 381 ++++++++++-------------- laravel/container.php | 20 +- laravel/cookie.php | 38 +-- laravel/database/eloquent/model.php | 25 +- laravel/database/query.php | 2 +- laravel/facades.php | 8 +- laravel/input.php | 43 ++- laravel/laravel.php | 53 +++- laravel/loader.php | 37 +-- laravel/paginator.php | 17 +- laravel/proxy.php | 21 -- laravel/request.php | 81 ++--- laravel/response.php | 6 +- laravel/routing/router.php | 9 +- laravel/security/auth.php | 51 ++-- laravel/security/crypter.php | 68 ++--- laravel/session/drivers/cookie.php | 41 +-- laravel/session/transporters/cookie.php | 22 +- laravel/uri.php | 46 +-- laravel/url.php | 2 +- laravel/validation/validator.php | 7 +- laravel/view.php | 355 +++++++++------------- 32 files changed, 1221 insertions(+), 1486 deletions(-) delete mode 100644 laravel/proxy.php diff --git a/application/config/aliases.php b/application/config/aliases.php index 7e2de566..faba2422 100644 --- a/application/config/aliases.php +++ b/application/config/aliases.php @@ -1,53 +1,53 @@ - 'Laravel\\Arr', - 'Asset' => 'Laravel\\Asset', - 'Auth' => 'Laravel\\Facades\\Auth', - 'Benchmark' => 'Laravel\\Benchmark', - 'Cache' => 'Laravel\\Cache', - 'Config' => 'Laravel\\Config', - 'Controller' => 'Laravel\\Controller', - 'Cookie' => 'Laravel\\Facades\\Cookie', - 'Crypter' => 'Laravel\\Facades\\Crypter', - 'DB' => 'Laravel\\Database\\Manager', - 'Eloquent' => 'Laravel\\Database\\Eloquent\\Model', - 'File' => 'Laravel\\File', - 'Form' => 'Laravel\\Form', - 'Hasher' => 'Laravel\\Facades\\Hasher', - 'HTML' => 'Laravel\\HTML', - 'Inflector' => 'Laravel\\Inflector', - 'Input' => 'Laravel\\Facades\\Input', - 'IoC' => 'Laravel\\IoC', - 'Lang' => 'Laravel\\Lang', - 'Loader' => 'Laravel\\Loader', - 'Messages' => 'Laravel\\Validation\\Messages', - 'Package' => 'Laravel\\Facades\\Package', - 'URI' => 'Laravel\\Facades\\URI', - 'URL' => 'Laravel\\URL', - 'Redirect' => 'Laravel\\Redirect', - 'Request' => 'Laravel\\Facades\\Request', - 'Response' => 'Laravel\\Response', - 'Session' => 'Laravel\\Facades\\Session', - 'Str' => 'Laravel\\Str', - 'Validator' => 'Laravel\\Validation\\Validator', - 'View' => 'Laravel\\Facades\\View', - + 'Laravel\\Arr', + 'Asset' => 'Laravel\\Asset', + 'Auth' => 'Laravel\\Auth', + 'Benchmark' => 'Laravel\\Benchmark', + 'Cache' => 'Laravel\\Cache', + 'Config' => 'Laravel\\Config', + 'Controller' => 'Laravel\\Controller', + 'Cookie' => 'Laravel\\Cookie', + 'Crypter' => 'Laravel\\Crypter', + 'DB' => 'Laravel\\Database\\Manager', + 'Eloquent' => 'Laravel\\Database\\Eloquent\\Model', + 'File' => 'Laravel\\File', + 'Form' => 'Laravel\\Form', + 'Hasher' => 'Laravel\\Facades\\Hasher', + 'HTML' => 'Laravel\\HTML', + 'Inflector' => 'Laravel\\Inflector', + 'Input' => 'Laravel\\Input', + 'IoC' => 'Laravel\\IoC', + 'Lang' => 'Laravel\\Lang', + 'Loader' => 'Laravel\\Loader', + 'Messages' => 'Laravel\\Validation\\Messages', + 'Package' => 'Laravel\\Facades\\Package', + 'URI' => 'Laravel\\URI', + 'URL' => 'Laravel\\URL', + 'Redirect' => 'Laravel\\Redirect', + 'Request' => 'Laravel\\Request', + 'Response' => 'Laravel\\Response', + 'Session' => 'Laravel\\Facades\\Session', + 'Str' => 'Laravel\\Str', + 'Validator' => 'Laravel\\Validation\\Validator', + 'View' => 'Laravel\\View', + ); \ No newline at end of file diff --git a/application/config/application.php b/application/config/application.php index 7503048d..94dc5a3e 100644 --- a/application/config/application.php +++ b/application/config/application.php @@ -1,115 +1,115 @@ - 'http://localhost', - - /* - |-------------------------------------------------------------------------- - | Application Index - |-------------------------------------------------------------------------- - | - | If you are including the "index.php" in your URLs, you can ignore this. - | - | However, if you are using mod_rewrite or something similar to get - | cleaner URLs, set this option to an empty string. - | - */ - - 'index' => 'index.php', - - /* - |-------------------------------------------------------------------------- - | Application Language - |-------------------------------------------------------------------------- - | - | The default language of your application. This language will be used by - | Lang library as the default language when doing string localization. - | - */ - - 'language' => 'en', - - /* - |-------------------------------------------------------------------------- - | Application Timezone - |-------------------------------------------------------------------------- - | - | The default timezone of your application. This timezone will be used when - | Laravel needs a date, such as when writing to a log file. - | - */ - - 'timezone' => 'UTC', - - /* - |-------------------------------------------------------------------------- - | Application Character Encoding - |-------------------------------------------------------------------------- - | - | The default character encoding used by your application. This is the - | character encoding that will be used by the Str, Text, and Form classes. - | - */ - - 'encoding' => 'UTF-8', - - /* - |-------------------------------------------------------------------------- - | Application Key - |-------------------------------------------------------------------------- - | - | Your application key should be a 32 character string that is totally - | random and secret. This key is used by the encryption class to generate - | secure, encrypted strings. - | - */ - - 'key' => '', - - /* - |-------------------------------------------------------------------------- - | SSL Link Generation - |-------------------------------------------------------------------------- - | - | Many sites use SSL to protect their users data. However, you may not - | always be able to use SSL on your development machine, meaning all HTTPS - | will be broken during development. - | - | For this reason, you may wish to disable the generation of HTTPS links - | throughout your application. This option does just that. All attempts to - | generate HTTPS links will generate regular HTTP links instead. - | - */ - - 'ssl' => true, - - /* - |-------------------------------------------------------------------------- - | Auto-Loaded Packages - |-------------------------------------------------------------------------- - | - | The packages that should be auto-loaded each time Laravel handles a - | request. These should generally be packages that you use on almost every - | request to your application. - | - | Each package specified here will be bootstrapped and can be conveniently - | used by your application's routes, models, and libraries. - | - | Note: The package names in this array should correspond to a package - | directory in application/packages. - | - */ - - 'packages' => array(), - + 'http://localhost', + + /* + |-------------------------------------------------------------------------- + | Application Index + |-------------------------------------------------------------------------- + | + | If you are including the "index.php" in your URLs, you can ignore this. + | + | However, if you are using mod_rewrite or something similar to get + | cleaner URLs, set this option to an empty string. + | + */ + + 'index' => 'index.php', + + /* + |-------------------------------------------------------------------------- + | Application Language + |-------------------------------------------------------------------------- + | + | The default language of your application. This language will be used by + | Lang library as the default language when doing string localization. + | + */ + + 'language' => 'en', + + /* + |-------------------------------------------------------------------------- + | Application Timezone + |-------------------------------------------------------------------------- + | + | The default timezone of your application. This timezone will be used when + | Laravel needs a date, such as when writing to a log file. + | + */ + + 'timezone' => 'UTC', + + /* + |-------------------------------------------------------------------------- + | Application Character Encoding + |-------------------------------------------------------------------------- + | + | The default character encoding used by your application. This is the + | character encoding that will be used by the Str, Text, and Form classes. + | + */ + + 'encoding' => 'UTF-8', + + /* + |-------------------------------------------------------------------------- + | Auto-Loaded Packages + |-------------------------------------------------------------------------- + | + | The packages that should be auto-loaded each time Laravel handles a + | request. These should generally be packages that you use on almost every + | request to your application. + | + | Each package specified here will be bootstrapped and can be conveniently + | used by your application's routes, models, and libraries. + | + | Note: The package names in this array should correspond to a package + | directory in application/packages. + | + */ + + 'packages' => array(), + + /* + |-------------------------------------------------------------------------- + | Application Key + |-------------------------------------------------------------------------- + | + | Your application key should be a 32 character string that is totally + | random and secret. This key is used by the encryption class to generate + | secure, encrypted strings. + | + */ + + 'key' => '', + + /* + |-------------------------------------------------------------------------- + | SSL Link Generation + |-------------------------------------------------------------------------- + | + | Many sites use SSL to protect their users data. However, you may not + | always be able to use SSL on your development machine, meaning all HTTPS + | will be broken during development. + | + | For this reason, you may wish to disable the generation of HTTPS links + | throughout your application. This option does just that. All attempts to + | generate HTTPS links will generate regular HTTP links instead. + | + */ + + 'ssl' => true, + ); \ No newline at end of file diff --git a/application/config/auth.php b/application/config/auth.php index 8c9b14fa..9e70a54c 100644 --- a/application/config/auth.php +++ b/application/config/auth.php @@ -1,64 +1,64 @@ - function($id) - { - if ( ! is_null($id)) return User::find($id); - }, - - /* - |-------------------------------------------------------------------------- - | Authenticate User Credentials - |-------------------------------------------------------------------------- - | - | This closure is called by the Auth::attempt() method when attempting to - | authenticate a user that is logging into your application. - | - | If the provided credentials are correct, simply return an object that - | represents the user being authenticated. If the credentials are not - | valid, don't return anything. - | - | Note: If a user object is returned, it must have an "id" property. - | - */ - - 'attempt' => function($username, $password) - { - if ( ! is_null($user = User::where('email', '=', $username)->first())) - { - if (Hasher::check($password, $user->password)) return $user; - } - }, - - /* - |-------------------------------------------------------------------------- - | Logout - |-------------------------------------------------------------------------- - | - | Here you may do anything that needs to be done when a user logs out of - | your application, such as call the logout method on a third-party API - | you are using for authentication, or anything else you desire. - | - */ - - 'logout' => function($user) {} - + function($id) + { + if ( ! is_null($id)) return User::find($id); + }, + + /* + |-------------------------------------------------------------------------- + | Authenticate User Credentials + |-------------------------------------------------------------------------- + | + | This closure is called by the Auth::attempt() method when attempting to + | authenticate a user that is logging into your application. + | + | If the provided credentials are correct, simply return an object that + | represents the user being authenticated. If the credentials are not + | valid, don't return anything. + | + | Note: If a user object is returned, it must have an "id" property. + | + */ + + 'attempt' => function($username, $password) + { + if ( ! is_null($user = User::where('email', '=', $username)->first())) + { + if (Hasher::check($password, $user->password)) return $user; + } + }, + + /* + |-------------------------------------------------------------------------- + | Logout + |-------------------------------------------------------------------------- + | + | Here you may do anything that needs to be done when a user logs out of + | your application, such as call the logout method on a third-party API + | you are using for authentication, or anything else you desire. + | + */ + + 'logout' => function($user) {} + ); \ No newline at end of file diff --git a/application/config/cache.php b/application/config/cache.php index a36c50e1..7c6bf32e 100644 --- a/application/config/cache.php +++ b/application/config/cache.php @@ -1,52 +1,52 @@ - 'file', - - /* - |-------------------------------------------------------------------------- - | Cache Key - |-------------------------------------------------------------------------- - | - | This key will be prepended to item keys stored using Memcached and APC to - | prevent collisions with other applications on the server. - | - */ - - 'key' => 'laravel', - - /* - |-------------------------------------------------------------------------- - | Memcached Servers - |-------------------------------------------------------------------------- - | - | The Memcached servers used by your application. - | - | Memcached is a free and open source, high-performance, distributed memory - | object caching system, generic in nature, but intended for use in speeding - | up dynamic web applications by alleviating database load. - | - | For more information about Memcached, check out: http://memcached.org - | - */ - - 'servers' => array( - array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100), - ), - + 'file', + + /* + |-------------------------------------------------------------------------- + | Cache Key + |-------------------------------------------------------------------------- + | + | This key will be prepended to item keys stored using Memcached and APC to + | prevent collisions with other applications on the server. + | + */ + + 'key' => 'laravel', + + /* + |-------------------------------------------------------------------------- + | Memcached Servers + |-------------------------------------------------------------------------- + | + | The Memcached servers used by your application. + | + | Memcached is a free and open source, high-performance, distributed memory + | object caching system, generic in nature, but intended for use in speeding + | up dynamic web applications by alleviating database load. + | + | For more information about Memcached, check out: http://memcached.org + | + */ + + 'servers' => array( + array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100), + ), + ); \ No newline at end of file diff --git a/application/config/database.php b/application/config/database.php index c07d3982..d8109118 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -1,73 +1,73 @@ - 'sqlite', - - /* - |-------------------------------------------------------------------------- - | Database Connections - |-------------------------------------------------------------------------- - | - | All of the database connections used by your application. - | - | Supported Drivers: 'mysql', 'pgsql', 'sqlite'. - | - | Note: When using the SQLite driver, the path and "sqlite" extention will - | be added automatically. You only need to specify the database name. - | - | Using a driver that isn't supported? You can still establish a PDO - | connection. Simply specify a driver and DSN option: - | - | 'odbc' => array( - | 'driver' => 'odbc', - | 'dsn' => 'your-dsn', - | 'username' => 'username', - | 'password' => 'password', - | ) - | - | Note: When using an unsupported driver, Eloquent and the fluent query - | builder may not work as expected. - | - */ - - 'connections' => array( - - 'sqlite' => array( - 'driver' => 'sqlite', - 'database' => 'application', - ), - - 'mysql' => array( - 'driver' => 'mysql', - 'host' => 'localhost', - 'database' => 'database', - 'username' => 'root', - 'password' => 'password', - 'charset' => 'utf8', - ), - - 'pgsql' => array( - 'driver' => 'pgsql', - 'host' => 'localhost', - 'database' => 'database', - 'username' => 'root', - 'password' => 'password', - 'charset' => 'utf8', - ), - - ), - + 'sqlite', + + /* + |-------------------------------------------------------------------------- + | Database Connections + |-------------------------------------------------------------------------- + | + | All of the database connections used by your application. + | + | Supported Drivers: 'mysql', 'pgsql', 'sqlite'. + | + | Note: When using the SQLite driver, the path and "sqlite" extention will + | be added automatically. You only need to specify the database name. + | + | Using a driver that isn't supported? You can still establish a PDO + | connection. Simply specify a driver and DSN option: + | + | 'odbc' => array( + | 'driver' => 'odbc', + | 'dsn' => 'your-dsn', + | 'username' => 'username', + | 'password' => 'password', + | ) + | + | Note: When using an unsupported driver, Eloquent and the fluent query + | builder may not work as expected. + | + */ + + 'connections' => array( + + 'sqlite' => array( + 'driver' => 'sqlite', + 'database' => 'application', + ), + + 'mysql' => array( + 'driver' => 'mysql', + 'host' => 'localhost', + 'database' => 'database', + 'username' => 'root', + 'password' => 'password', + 'charset' => 'utf8', + ), + + 'pgsql' => array( + 'driver' => 'pgsql', + 'host' => 'localhost', + 'database' => 'database', + 'username' => 'root', + 'password' => 'password', + 'charset' => 'utf8', + ), + + ), + ); \ No newline at end of file diff --git a/application/config/error.php b/application/config/error.php index 1f8c1cd9..6cb77488 100644 --- a/application/config/error.php +++ b/application/config/error.php @@ -1,87 +1,87 @@ - true, - - /* - |-------------------------------------------------------------------------- - | Error Logging - |-------------------------------------------------------------------------- - | - | Error Logging will use the "logger" function defined below to log error - | messages, which gives you complete freedom to determine how error - | messages are logged. Enjoy the flexibility. - | - */ - - 'log' => false, - - /* - |-------------------------------------------------------------------------- - | Error Handler - |-------------------------------------------------------------------------- - | - | Because of the various ways of managing error logging, you get complete - | flexibility in Laravel to manage error logging as you see fit. - | - | This function will be called when an error occurs in your application. - | You are free to handle the exception any way your heart desires. - | - | The error "severity" passed to the method is a human-readable severity - | level such as "Parsing Error" or "Fatal Error". - | - */ - - 'handler' => function($exception, $severity, $message, $config) - { - if ($config['detail']) - { - $data = compact('exception', 'severity', 'message'); - - $response = Response::view('error.exception', $data)->status(500); - } - else - { - $response = Response::error('500'); - } - - $response->send(); - }, - - /* - |-------------------------------------------------------------------------- - | Error Logger - |-------------------------------------------------------------------------- - | - | Because of the various ways of managing error logging, you get complete - | flexibility to manage error logging as you see fit. - | - | This function will be called when an error occurs in your application - | and error loggins is enabled. You can log the error however you like. - | - | A simple logging system has been setup for you. By default, all errors - | will be logged to the storage/log.txt file. - | - */ - - 'logger' => function($exception, $severity, $message, $config) - { - File::append(STORAGE_PATH.'log.txt', date('Y-m-d H:i:s').' '.$severity.' - '.$message.PHP_EOL); - } - + true, + + /* + |-------------------------------------------------------------------------- + | Error Logging + |-------------------------------------------------------------------------- + | + | Error Logging will use the "logger" function defined below to log error + | messages, which gives you complete freedom to determine how error + | messages are logged. Enjoy the flexibility. + | + */ + + 'log' => false, + + /* + |-------------------------------------------------------------------------- + | Error Handler + |-------------------------------------------------------------------------- + | + | Because of the various ways of managing error logging, you get complete + | flexibility in Laravel to manage error logging as you see fit. + | + | This function will be called when an error occurs in your application. + | You are free to handle the exception any way your heart desires. + | + | The error "severity" passed to the method is a human-readable severity + | level such as "Parsing Error" or "Fatal Error". + | + */ + + 'handler' => function($exception, $severity, $message, $config) + { + if ($config['detail']) + { + $data = compact('exception', 'severity', 'message'); + + $response = Response::view('error.exception', $data)->status(500); + } + else + { + $response = Response::error('500'); + } + + $response->send(); + }, + + /* + |-------------------------------------------------------------------------- + | Error Logger + |-------------------------------------------------------------------------- + | + | Because of the various ways of managing error logging, you get complete + | flexibility to manage error logging as you see fit. + | + | This function will be called when an error occurs in your application + | and error loggins is enabled. You can log the error however you like. + | + | A simple logging system has been setup for you. By default, all errors + | will be logged to the storage/log.txt file. + | + */ + + 'logger' => function($exception, $severity, $message, $config) + { + File::append(STORAGE_PATH.'log.txt', date('Y-m-d H:i:s').' '.$severity.' - '.$message.PHP_EOL); + } + ); \ No newline at end of file diff --git a/application/config/session.php b/application/config/session.php index 1c167143..ed7f3825 100644 --- a/application/config/session.php +++ b/application/config/session.php @@ -1,94 +1,94 @@ - '', - - /* - |-------------------------------------------------------------------------- - | Session Database - |-------------------------------------------------------------------------- - | - | The database table on which the session should be stored. - | - | This option is only relevant when using the "database" session driver. - | - */ - - 'table' => 'sessions', - - /* - |-------------------------------------------------------------------------- - | Session Garbage Collection Probability - |-------------------------------------------------------------------------- - | - | Some session drivers require the manual clean-up of expired sessions. - | This option specifies the probability of session garbage collection - | occuring for any given request. - | - | For example, the default value states that garbage collection has about - | a 2% (2 / 100) chance of occuring for any given request. - | - */ - - 'sweepage' => array(2, 100), - - /* - |-------------------------------------------------------------------------- - | Session Lifetime - |-------------------------------------------------------------------------- - | - | The number of minutes a session can be idle before expiring. - | - */ - - 'lifetime' => 60, - - /* - |-------------------------------------------------------------------------- - | Session Expiration On Close - |-------------------------------------------------------------------------- - | - | Determines if the session should expire when the user's web browser closes. - | - */ - - 'expire_on_close' => false, - - /* - |-------------------------------------------------------------------------- - | Session Cookie Path - |-------------------------------------------------------------------------- - | - | The path for which the session cookie is available. - | - */ - - 'path' => '/', - - /* - |-------------------------------------------------------------------------- - | Session Cookie Domain - |-------------------------------------------------------------------------- - | - | The domain for which the session cookie is available. - | - */ - - 'domain' => null, - + '', + + /* + |-------------------------------------------------------------------------- + | Session Database + |-------------------------------------------------------------------------- + | + | The database table on which the session should be stored. + | + | This option is only relevant when using the "database" session driver. + | + */ + + 'table' => 'sessions', + + /* + |-------------------------------------------------------------------------- + | Session Garbage Collection Probability + |-------------------------------------------------------------------------- + | + | Some session drivers require the manual clean-up of expired sessions. + | This option specifies the probability of session garbage collection + | occuring for any given request. + | + | For example, the default value states that garbage collection has about + | a 2% (2 / 100) chance of occuring for any given request. + | + */ + + 'sweepage' => array(2, 100), + + /* + |-------------------------------------------------------------------------- + | Session Lifetime + |-------------------------------------------------------------------------- + | + | The number of minutes a session can be idle before expiring. + | + */ + + 'lifetime' => 60, + + /* + |-------------------------------------------------------------------------- + | Session Expiration On Close + |-------------------------------------------------------------------------- + | + | Determines if the session should expire when the user's web browser closes. + | + */ + + 'expire_on_close' => false, + + /* + |-------------------------------------------------------------------------- + | Session Cookie Path + |-------------------------------------------------------------------------- + | + | The path for which the session cookie is available. + | + */ + + 'path' => '/', + + /* + |-------------------------------------------------------------------------- + | Session Cookie Domain + |-------------------------------------------------------------------------- + | + | The domain for which the session cookie is available. + | + */ + + 'domain' => null, + ); \ No newline at end of file diff --git a/application/routes.php b/application/routes.php index 94efd50d..0262a25a 100644 --- a/application/routes.php +++ b/application/routes.php @@ -37,7 +37,7 @@ | */ - 'GET /' => function() + 'GET /' => function($application) { return View::make('home.index'); }, diff --git a/laravel/bootstrap/core.php b/laravel/bootstrap/core.php index 8b763475..8264bce7 100644 --- a/laravel/bootstrap/core.php +++ b/laravel/bootstrap/core.php @@ -61,7 +61,9 @@ * for the lazy-loading of all of the Laravel core classes, as well as * the developer created libraries and models. */ -spl_autoload_register(array($container->resolve('laravel.loader'), 'load')); +spl_autoload_register(array('Laravel\\Loader', 'load')); + +Loader::$aliases = Config::get('aliases'); /** * Define a few convenient global functions. diff --git a/laravel/config.php b/laravel/config.php index 619b7005..525992e3 100644 --- a/laravel/config.php +++ b/laravel/config.php @@ -1,154 +1,154 @@ - - * // Determine if the "session" configuration file exists - * $exists = Config::has('session'); - * - * // Determine if the "timezone" option exists in the "application" configuration array - * $exists = Config::has('application.timezone'); - * - * - * @param string $key - * @return bool - */ - public static function has($key) - { - return ! is_null(static::get($key)); - } - - /** - * Get a configuration item. - * - * If no item is requested, the entire configuration array will be returned. - * - * - * // Get the "session" configuration array - * $session = Config::get('session'); - * - * // Get the "timezone" option from the "application" configuration file - * $timezone = Config::get('application.timezone'); - * - * - * @param string $key - * @param string $default - * @return array - */ - public static function get($key, $default = null) - { - list($file, $key) = static::parse($key); - - if ( ! static::load($file)) - { - return ($default instanceof Closure) ? call_user_func($default) : $default; - } - - if (is_null($key)) return static::$items[$file]; - - return Arr::get(static::$items[$file], $key, $default); - } - - /** - * Set a configuration item's value. - * - * - * // Set the "session" configuration array - * Config::set('session', $array); - * - * // Set the "timezone" option in the "application" configuration file - * Config::set('application.timezone', 'UTC'); - * - * - * @param string $key - * @param mixed $value - * @return void - */ - public static function set($key, $value) - { - list($file, $key) = static::parse($key); - - static::load($file); - - (is_null($key)) ? Arr::set(static::$items, $file, $value) : Arr::set(static::$items[$file], $key, $value); - } - - /** - * Parse a configuration key and return its file and key segments. - * - * @param string $key - * @return array - */ - protected static function parse($key) - { - $segments = explode('.', $key); - - // If there is only one segment after exploding on dots, we will return NULL - // as the key value, causing the entire configuration array to be returned. - $key = (count($segments) > 1) ? implode('.', array_slice($segments, 1)) : null; - - return array($segments[0], $key); - } - - /** - * Load all of the configuration items from a configuration file. - * - * @param string $file - * @return bool - */ - protected static function load($file) - { - if (isset(static::$items[$file])) return true; - - $config = array(); - - // Configuration files cascade. Typically, the system configuration array is loaded - // first, followed by the application array, providing the convenient cascading - // of configuration options from system to application. - foreach (static::$paths as $directory) - { - if (file_exists($path = $directory.$file.EXT)) - { - $config = array_merge($config, require $path); - } - } - - // If configuration options were actually found, they will be loaded into the - // array containing all of the options for all files. The array is keyed by the - // configuration file name. - if (count($config) > 0) static::$items[$file] = $config; - - return isset(static::$items[$file]); - } - - /** - * Add a directory to the configuration manager's search paths. - * - * @param string $path - * @return void - */ - public static function glance($path) - { - static::$paths[] = $path; - } - + + * // Determine if the "session" configuration file exists + * $exists = Config::has('session'); + * + * // Determine if the "timezone" option exists in the "application" configuration array + * $exists = Config::has('application.timezone'); + * + * + * @param string $key + * @return bool + */ + public static function has($key) + { + return ! is_null(static::get($key)); + } + + /** + * Get a configuration item. + * + * If no item is requested, the entire configuration array will be returned. + * + * + * // Get the "session" configuration array + * $session = Config::get('session'); + * + * // Get the "timezone" option from the "application" configuration file + * $timezone = Config::get('application.timezone'); + * + * + * @param string $key + * @param string $default + * @return array + */ + public static function get($key, $default = null) + { + list($file, $key) = static::parse($key); + + if ( ! static::load($file)) + { + return ($default instanceof Closure) ? call_user_func($default) : $default; + } + + if (is_null($key)) return static::$items[$file]; + + return Arr::get(static::$items[$file], $key, $default); + } + + /** + * Set a configuration item's value. + * + * + * // Set the "session" configuration array + * Config::set('session', $array); + * + * // Set the "timezone" option in the "application" configuration file + * Config::set('application.timezone', 'UTC'); + * + * + * @param string $key + * @param mixed $value + * @return void + */ + public static function set($key, $value) + { + list($file, $key) = static::parse($key); + + static::load($file); + + (is_null($key)) ? Arr::set(static::$items, $file, $value) : Arr::set(static::$items[$file], $key, $value); + } + + /** + * Parse a configuration key and return its file and key segments. + * + * @param string $key + * @return array + */ + protected static function parse($key) + { + $segments = explode('.', $key); + + // If there is only one segment after exploding on dots, we will return NULL + // as the key value, causing the entire configuration array to be returned. + $key = (count($segments) > 1) ? implode('.', array_slice($segments, 1)) : null; + + return array($segments[0], $key); + } + + /** + * Load all of the configuration items from a configuration file. + * + * @param string $file + * @return bool + */ + protected static function load($file) + { + if (isset(static::$items[$file])) return true; + + $config = array(); + + // Configuration files cascade. Typically, the system configuration array is loaded + // first, followed by the application array, providing the convenient cascading + // of configuration options from system to application. + foreach (static::$paths as $directory) + { + if (file_exists($path = $directory.$file.EXT)) + { + $config = array_merge($config, require $path); + } + } + + // If configuration options were actually found, they will be loaded into the + // array containing all of the options for all files. The array is keyed by the + // configuration file name. + if (count($config) > 0) static::$items[$file] = $config; + + return isset(static::$items[$file]); + } + + /** + * Add a directory to the configuration manager's search paths. + * + * @param string $path + * @return void + */ + public static function glance($path) + { + static::$paths[] = $path; + } + } \ No newline at end of file diff --git a/laravel/config/container.php b/laravel/config/container.php index 6396daa3..3667fead 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -1,217 +1,166 @@ - array('singleton' => true, 'resolver' => function($c) - { - return new Security\Auth($c->resolve('laravel.session')); - }), - - - 'laravel.cookie' => array('singleton' => true, 'resolver' => function($c) - { - return new Cookie($_COOKIE); - }), - - - 'laravel.crypter' => array('resolver' => function($c) - { - return new Security\Crypter(MCRYPT_RIJNDAEL_256, 'cbc', Config::get('application.key')); - }), - - - 'laravel.hasher' => array('singleton' => true, 'resolver' => function($c) - { - return new Security\Hashing\Bcrypt(8, false); - }), - - - 'laravel.input' => array('singleton' => true, 'resolver' => function($c) - { - $input = array(); - - switch ($c->resolve('laravel.request')->method()) - { - case 'GET': - $input = $_GET; - break; - - case 'POST': - $input = $_POST; - break; - - case 'PUT': - case 'DELETE': - if ($c->resolve('laravel.request')->spoofed()) - { - $input = $_POST; - } - else - { - parse_str(file_get_contents('php://input'), $input); - } - } - - unset($input[Request::spoofer]); - - return new Input($input, $_FILES); - }), - - - 'laravel.loader' => array('singleton' => true, 'resolver' => function($c) - { - return new Loader(array(BASE_PATH, MODEL_PATH, LIBRARY_PATH, BASE_PATH), Config::get('aliases')); - }), - - - 'laravel.request' => array('singleton' => true, 'resolver' => function($c) - { - return new Request($c->resolve('laravel.uri'), $_SERVER, $_POST); - }), - - - 'laravel.uri' => array('singleton' => true, 'resolver' => function($c) - { - return new URI($_SERVER); - }), - - - 'laravel.view' => array('singleton' => true, 'resolver' => function($c) - { - require_once SYS_PATH.'view'.EXT; - - return new View_Factory($c->resolve('laravel.composer'), VIEW_PATH, STORAGE_PATH.'views/'); - }), - - - 'laravel.composer' => array('singleton' => true, 'resolver' => function($c) - { - return new View_Composer(require APP_PATH.'composers'.EXT); - }), - - /* - |-------------------------------------------------------------------------- - | Laravel Routing Components - |-------------------------------------------------------------------------- - */ - - 'laravel.routing.router' => array('singleton' => true, 'resolver' => function($c) - { - return new Routing\Router($c->resolve('laravel.routing.loader'), CONTROLLER_PATH); - }), - - - 'laravel.routing.loader' => array('singleton' => true, 'resolver' => function($c) - { - return new Routing\Loader(APP_PATH, ROUTE_PATH); - }), - - - 'laravel.routing.caller' => array('resolver' => function($c) - { - return new Routing\Caller($c, require APP_PATH.'filters'.EXT, CONTROLLER_PATH); - }), - - /* - |-------------------------------------------------------------------------- - | Laravel Caching Components - |-------------------------------------------------------------------------- - */ - - 'laravel.cache.apc' => array('resolver' => function($c) - { - return new Cache\Drivers\APC(Config::get('cache.key')); - }), - - - 'laravel.cache.file' => array('resolver' => function($c) - { - return new Cache\Drivers\File(CACHE_PATH); - }), - - - 'laravel.cache.memcached' => array('resolver' => function($c) - { - return new Cache\Drivers\Memcached($c->resolve('laravel.cache.memcache.connection'), Config::get('cache.key')); - }), - - - 'laravel.cache.memcache.connection' => array('singleton' => true, 'resolver' => function($c) - { - $memcache = new \Memcache; - - foreach (Config::get('cache.servers') as $server) - { - $memcache->addServer($server['host'], $server['port'], true, $server['weight']); - } - - if ($memcache->getVersion() === false) - { - throw new \Exception('Could not establish memcached connection. Please verify your memcached configuration.'); - } - - return $memcache; - }), - - /* - |-------------------------------------------------------------------------- - | Laravel Session Components - |-------------------------------------------------------------------------- - */ - - 'laravel.session.id' => array('singleton' => true, 'resolver' => function($c) - { - return $c->resolve('laravel.cookie')->get('laravel_session'); - }), - - - 'laravel.session.manager' => array('singleton' => true, 'resolver' => function($c) - { - $driver = $c->resolve('laravel.session.'.Config::get('session.driver')); - - return new Session\Manager($driver, $c->resolve('laravel.session.transporter')); - }), - - - 'laravel.session.transporter' => array('resolver' => function($c) - { - return new Session\Transporters\Cookie($c->resolve('laravel.cookie')); - }), - - - 'laravel.session.apc' => array('resolver' => function($c) - { - return new Session\Drivers\APC($c->resolve('laravel.cache.apc')); - }), - - - 'laravel.session.cookie' => array('resolver' => function($c) - { - return new Session\Drivers\Cookie($c->resolve('laravel.crypter'), $c->resolve('laravel.cookie')); - }), - - - 'laravel.session.database' => array('resolver' => function($c) - { - return new Session\Drivers\Database(Database\Manager::connection()); - }), - - - 'laravel.session.file' => array('resolver' => function($c) - { - return new Session\Drivers\File(SESSION_PATH); - }), - - - 'laravel.session.memcached' => array('resolver' => function($c) - { - return new Session\Drivers\Memcached($c->resolve('laravel.cache.memcached')); - }), - + array('singleton' => true, 'resolver' => function($c) + { + return new Security\Hashing\Bcrypt(8, false); + }), + + /* + |-------------------------------------------------------------------------- + | Laravel Routing Components + |-------------------------------------------------------------------------- + | + | The following components are used by the Laravel routing system. + | + | The router is used to map a given method and URI to a route intance. + | + | The route loader is responsible for loading the appropriates routes file + | for a given request URI, as well as loading all routes when the framework + | needs to find a named route wtihin the application. + | + | The route caller is responsible for receiving a route and taking the + | appropriate action to execute that route. Some routes delegate execution + | to a controller, so this class will also resolve controllers out of the + | container and call the appropriate methods on those controllers. + | + */ + + 'laravel.routing.router' => array('singleton' => true, 'resolver' => function($c) + { + return new Routing\Router($c->core('routing.loader'), CONTROLLER_PATH); + }), + + + 'laravel.routing.loader' => array('singleton' => true, 'resolver' => function($c) + { + return new Routing\Loader(APP_PATH, ROUTE_PATH); + }), + + + 'laravel.routing.caller' => array('resolver' => function($c) + { + return new Routing\Caller($c, require APP_PATH.'filters'.EXT, CONTROLLER_PATH); + }), + + /* + |-------------------------------------------------------------------------- + | Laravel Caching Components + |-------------------------------------------------------------------------- + | + | The following components are used by the wonderfully, simple Laravel + | caching system. Each driver is resolved through the container. + | + | New cache drivers may be added to the framework by simply registering + | them into the container. + | + */ + + 'laravel.cache.apc' => array('resolver' => function($c) + { + return new Cache\Drivers\APC(Config::get('cache.key')); + }), + + + 'laravel.cache.file' => array('resolver' => function($c) + { + return new Cache\Drivers\File(CACHE_PATH); + }), + + + 'laravel.cache.memcached' => array('resolver' => function($c) + { + return new Cache\Drivers\Memcached($c->core('cache.memcache.connection'), Config::get('cache.key')); + }), + + + 'laravel.cache.memcache.connection' => array('singleton' => true, 'resolver' => function($c) + { + $memcache = new \Memcache; + + foreach (Config::get('cache.servers') as $server) + { + $memcache->addServer($server['host'], $server['port'], true, $server['weight']); + } + + if ($memcache->getVersion() === false) + { + throw new \Exception('Could not establish memcached connection. Please verify your memcached configuration.'); + } + + return $memcache; + }), + + /* + |-------------------------------------------------------------------------- + | Laravel Session Components + |-------------------------------------------------------------------------- + | + | The following components are used by the Laravel session system. + | + | The framework allows the session ID to be transported via a variety + | of different mechanisms by resolve the ID itself and the session + | transporter instance out of the container. This allows sessions + | to be used by clients who cannot receive cookies. + | + | The session manager is responsible for loading the session payload + | from the session driver, as well as examining the payload validitiy + | and things like the CSRF token. + | + | Like the caching components, each session driver is resolved via the + | container and new drivers may be added by registering them into the + | container. Several session drivers are "driven" by the cache drivers. + | + */ + + 'laravel.session.id' => array('singleton' => true, 'resolver' => function($c) + { + return Cookie::get('laravel_session'); + }), + + + 'laravel.session.manager' => array('singleton' => true, 'resolver' => function($c) + { + $driver = $c->core('session.'.Config::get('session.driver')); + + return new Session\Manager($driver, $c->core('session.transporter')); + }), + + + 'laravel.session.transporter' => array('resolver' => function($c) + { + return new Session\Transporters\Cookie; + }), + + + 'laravel.session.apc' => array('resolver' => function($c) + { + return new Session\Drivers\APC($c->core('cache.apc')); + }), + + + 'laravel.session.cookie' => array('resolver' => function($c) + { + return new Session\Drivers\Cookie; + }), + + + 'laravel.session.database' => array('resolver' => function($c) + { + return new Session\Drivers\Database(Database\Manager::connection()); + }), + + + 'laravel.session.file' => array('resolver' => function($c) + { + return new Session\Drivers\File(SESSION_PATH); + }), + + + 'laravel.session.memcached' => array('resolver' => function($c) + { + return new Session\Drivers\Memcached($c->core('cache.memcached')); + }), + ); \ No newline at end of file diff --git a/laravel/container.php b/laravel/container.php index 4754fcd5..a5f5af06 100644 --- a/laravel/container.php +++ b/laravel/container.php @@ -134,19 +134,23 @@ public function instance($name, $instance) * Resolve a core Laravel class from the container. * * - * // Resolve the "laravel.input" class from the container + * // Resolve the "laravel.router" class from the container * $input = IoC::container()->core('input'); * * // Equivalent resolution using the "resolve" method - * $input = IoC::container()->resolve('laravel.input'); + * $input = IoC::container()->resolve('laravel.router'); + * + * // Pass an array of parameters to the resolver + * $input = IoC::container()->core('input', array('test')); * * * @param string $name + * @param array $parameters * @return mixed */ - public function core($name) + public function core($name, $parameters = array()) { - return $this->resolve("laravel.{$name}"); + return $this->resolve("laravel.{$name}", $parameters); } /** @@ -155,12 +159,16 @@ public function core($name) * * // Get an instance of the "mailer" object registered in the container * $mailer = IoC::container()->resolve('mailer'); + * + * // Pass an array of parameters to the resolver + * $mailer = IoC::container()->resolve('mailer', array('test')); * * * @param string $name + * @param array $parameters * @return mixed */ - public function resolve($name) + public function resolve($name, $parameters = array()) { if (array_key_exists($name, $this->singletons)) return $this->singletons[$name]; @@ -169,7 +177,7 @@ public function resolve($name) throw new \Exception("Error resolving [$name]. No resolver has been registered in the container."); } - $object = call_user_func($this->registry[$name]['resolver'], $this); + $object = call_user_func($this->registry[$name]['resolver'], $this, $parameters); return (isset($this->registry[$name]['singleton'])) ? $this->singletons[$name] = $object : $object; } diff --git a/laravel/cookie.php b/laravel/cookie.php index 3c9a832d..f7270709 100644 --- a/laravel/cookie.php +++ b/laravel/cookie.php @@ -2,33 +2,15 @@ class Cookie { - /** - * The cookies for the current request. - * - * @var array - */ - protected $cookies; - - /** - * Create a new cookie manager instance. - * - * @param array $cookies - * @return void - */ - public function __construct(&$cookies) - { - $this->cookies =& $cookies; - } - /** * Determine if a cookie exists. * * @param string $name * @return bool */ - public function has($name) + public static function has($name) { - return ! is_null($this->get($name)); + return ! is_null(static::get($name)); } /** @@ -38,9 +20,9 @@ public function has($name) * @param mixed $default * @return string */ - public function get($name, $default = null) + public static function get($name, $default = null) { - return Arr::get($this->cookies, $name, $default); + return Arr::get($_COOKIE, $name, $default); } /** @@ -54,9 +36,9 @@ public function get($name, $default = null) * @param bool $http_only * @return bool */ - public function forever($name, $value, $path = '/', $domain = null, $secure = false, $http_only = false) + public static function forever($name, $value, $path = '/', $domain = null, $secure = false, $http_only = false) { - return $this->put($name, $value, 2628000, $path, $domain, $secure, $http_only); + return static::put($name, $value, 2628000, $path, $domain, $secure, $http_only); } /** @@ -77,11 +59,11 @@ public function forever($name, $value, $path = '/', $domain = null, $secure = fa * @param bool $http_only * @return bool */ - public function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false, $http_only = false) + public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false, $http_only = false) { if (headers_sent()) return false; - if ($minutes < 0) unset($this->cookies[$name]); + if ($minutes < 0) unset($_COOKIE[$name]); // Since PHP needs the cookie lifetime in seconds, we will calculate it here. // A "0" lifetime means the cookie expires when the browser closes. @@ -96,9 +78,9 @@ public function put($name, $value, $minutes = 0, $path = '/', $domain = null, $s * @param string $name * @return bool */ - public function forget($name) + public static function forget($name) { - return $this->put($name, null, -60); + return static::put($name, null, -60); } } \ No newline at end of file diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index 24dbb3df..979a86d3 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -3,6 +3,7 @@ use Laravel\IoC; use Laravel\Str; use Laravel\Inflector; +use Laravel\Paginator; use Laravel\Database\Manager as DB; abstract class Model { @@ -223,6 +224,28 @@ private function _first() return (count($results = $this->take(1)->_get()) > 0) ? reset($results) : null; } + /** + * Get paginated model results as a Paginator instance. + * + * @param int $per_page + * @return Paginator + */ + private function _paginate($per_page = null) + { + $total = $this->query->count(); + + // The number of models to show per page may be specified as a static property + // on the model. The models shown per page may also be overriden for the model + // by passing the number into this method. If the models to show per page is + // not available via either of these avenues, a default number will be shown. + if (is_null($per_page)) + { + $per_page = (property_exists(get_class($this), 'per_page')) ? static::$per_page : 20; + } + + return Paginator::make($this->for_page(Paginator::page($total, $per_page), $per_page)->get(), $total, $per_page); + } + /** * Retrieve the query for a 1:1 relationship. * @@ -484,7 +507,7 @@ public function __call($method, $parameters) // To allow the "with", "get", "first", and "paginate" methods to be called both // staticly and on an instance, we need to have private, underscored versions // of the methods and handle them dynamically. - if (in_array($method, array('with', 'get', 'first'))) + if (in_array($method, array('with', 'get', 'first', 'paginate'))) { return call_user_func_array(array($this, '_'.$method), $parameters); } diff --git a/laravel/database/query.php b/laravel/database/query.php index 36dab41a..25ba2d79 100644 --- a/laravel/database/query.php +++ b/laravel/database/query.php @@ -525,7 +525,7 @@ private function aggregate($aggregator, $column) * @param array $columns * @return Paginator */ - public function paginate($per_page, $columns = array('*')) + public function paginate($per_page = 20, $columns = array('*')) { // Calculate the current page for the request. The page number will be validated // and adjusted by the Paginator class, so we can assume it is valid. diff --git a/laravel/facades.php b/laravel/facades.php index 92923f42..f875ed29 100644 --- a/laravel/facades.php +++ b/laravel/facades.php @@ -32,12 +32,6 @@ public static function __callStatic($method, $parameters) } -class Auth extends Facade { public static $resolve = 'laravel.auth'; } -class Cookie extends Facade { public static $resolve = 'laravel.cookie'; } class Crypter extends Facade { public static $resolve = 'laravel.crypter'; } class Hasher extends Facade { public static $resolve = 'laravel.hasher'; } -class Input extends Facade { public static $resolve = 'laravel.input'; } -class Request extends Facade { public static $resolve = 'laravel.request'; } -class Session extends Facade { public static $resolve = 'laravel.session'; } -class URI extends Facade { public static $resolve = 'laravel.uri'; } -class View extends Facade { public static $resolve = 'laravel.view'; } \ No newline at end of file +class Session extends Facade { public static $resolve = 'laravel.session'; } \ No newline at end of file diff --git a/laravel/input.php b/laravel/input.php index 0d8ab094..76b91b84 100644 --- a/laravel/input.php +++ b/laravel/input.php @@ -7,14 +7,7 @@ class Input { * * @var array */ - protected $input; - - /** - * The $_FILES array for the current request. - * - * @var array - */ - protected $files; + protected static $input; /** * The key used to store old input in the session. @@ -24,16 +17,14 @@ class Input { const old_input = 'laravel_old_input'; /** - * Create a new input manager instance. + * Set the input for the current request. * * @param array $input - * @param array $files * @return void */ - public function __construct($input, $files) + public static function set($input) { - $this->input = $input; - $this->files = $files; + static::$input = $input; } /** @@ -43,9 +34,9 @@ public function __construct($input, $files) * * @return array */ - public function all() + public static function all() { - return array_merge($this->get(), $this->file()); + return array_merge(static::get(), static::file()); } /** @@ -56,9 +47,9 @@ public function all() * @param string $key * @return bool */ - public function has($key) + public static function has($key) { - return ( ! is_null($this->get($key)) and trim((string) $this->get($key)) !== ''); + return ( ! is_null(static::get($key)) and trim((string) static::get($key)) !== ''); } /** @@ -78,9 +69,9 @@ public function has($key) * @param mixed $default * @return mixed */ - public function get($key = null, $default = null) + public static function get($key = null, $default = null) { - return Arr::get($this->input, $key, $default); + return Arr::get(static::$input, $key, $default); } /** @@ -89,9 +80,9 @@ public function get($key = null, $default = null) * @param string $key * @return bool */ - public function had($key) + public static function had($key) { - return ( ! is_null($this->old($key)) and trim((string) $this->old($key)) !== ''); + return ( ! is_null(static::old($key)) and trim((string) static::old($key)) !== ''); } /** @@ -109,7 +100,7 @@ public function had($key) * @param mixed $default * @return string */ - public function old($key = null, $default = null) + public static function old($key = null, $default = null) { if (Config::get('session.driver') == '') { @@ -136,9 +127,9 @@ public function old($key = null, $default = null) * @param mixed $default * @return array */ - public function file($key = null, $default = null) + public static function file($key = null, $default = null) { - return Arr::get($this->files, $key, $default); + return Arr::get($_FILES, $key, $default); } /** @@ -155,9 +146,9 @@ public function file($key = null, $default = null) * @param string $path * @return bool */ - public function upload($key, $path) + public static function upload($key, $path) { - return array_key_exists($key, $this->files) ? File::upload($key, $path, $this->files) : false; + return array_key_exists($key, $_FILES) ? File::upload($key, $path, $_FILES) : false; } } \ No newline at end of file diff --git a/laravel/laravel.php b/laravel/laravel.php index a7e0bd03..4af26395 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -32,23 +32,58 @@ } /** - * Resolve the incoming request instance from the IoC container - * and route the request to the proper route in the application. - * If a route is found, the route will be called with the current - * requst instance. If no route is found, the 404 response will - * be returned to the browser. + * Manually load some core classes that are used on every request + * This allows to avoid using the loader for these classes. */ +require SYS_PATH.'uri'.EXT; require SYS_PATH.'request'.EXT; require SYS_PATH.'routing/route'.EXT; require SYS_PATH.'routing/router'.EXT; require SYS_PATH.'routing/loader'.EXT; require SYS_PATH.'routing/caller'.EXT; -$request = $container->core('request'); +/** + * Gather the input to the application for the current request. + * The input will be gathered based on the current request method + * and will be set on the Input manager. + */ +$input = array(); -list($method, $uri) = array($request->method(), $request->uri()); +switch (Request::method()) +{ + case 'GET': + $input = $_GET; + break; -$route = $container->core('routing.router')->route($request, $method, $uri); + case 'POST': + $input = $_POST; + break; + + case 'PUT': + case 'DELETE': + if (Request::spoofed()) + { + $input = $_POST; + } + else + { + parse_str(file_get_contents('php://input'), $input); + } +} + +unset($input[Request::spoofer]); + +Input::set($input); + +/** + * Route the request to the proper route in the application. If a + * route is found, the route will be called with the current request + * instance. If no route is found, the 404 response will be returned + * to the browser. + */ +list($method, $uri) = array(Request::method(), URI::get()); + +$route = $container->core('routing.router')->route($method, $uri); if ( ! is_null($route)) { @@ -75,7 +110,7 @@ */ if (isset($session)) { - $flash = array(Input::old_input => $container->core('input')->get()); + $flash = array(Input::old_input => Input::get()); $session->close($container->core('session'), Config::get('session'), $flash); } diff --git a/laravel/loader.php b/laravel/loader.php index 693742af..8127038f 100644 --- a/laravel/loader.php +++ b/laravel/loader.php @@ -7,27 +7,14 @@ class Loader { * * @var array */ - protected $paths = array(); + public static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH, APP_PATH); /** * The class aliases defined for the application. * * @var array */ - protected $aliases = array(); - - /** - * Create a new class loader instance. - * - * @param array $paths - * @param array $aliases - * @return void - */ - public function __construct($paths, $aliases = array()) - { - $this->paths = $paths; - $this->aliases = $aliases; - } + public static $aliases = array(); /** * Load the file for a given class. @@ -43,7 +30,7 @@ public function __construct($paths, $aliases = array()) * @param string $class * @return void */ - public function load($class) + public static function load($class) { // All Laravel core classes follow a namespace to directory convention. // We will replace all of the namespace slashes with directory slashes. @@ -51,12 +38,12 @@ public function load($class) // Check to determine if an alias exists. If it does, we will define the // alias and bail out. Aliases are defined for most used core classes. - if (array_key_exists($class, $this->aliases)) + if (array_key_exists($class, static::$aliases)) { - return class_alias($this->aliases[$class], $class); + return class_alias(static::$aliases[$class], $class); } - foreach ($this->paths as $path) + foreach (static::$paths as $path) { if (file_exists($path = $path.$file.EXT)) { @@ -74,9 +61,9 @@ public function load($class) * @param string $class * @return void */ - public function alias($alias, $class) + public static function alias($alias, $class) { - $this->aliases[$alias] = $class; + static::$aliases[$alias] = $class; } /** @@ -85,9 +72,9 @@ public function alias($alias, $class) * @param string $path * @return void */ - public function path($path) + public static function path($path) { - $this->paths[] = rtrim($path, '/').'/'; + static::$paths[] = rtrim($path, '/').'/'; } /** @@ -96,9 +83,9 @@ public function path($path) * @param string $alias * @return void */ - public function forget_alias($alias) + public static function forget_alias($alias) { - unset($this->aliases[$alias]); + unset(static::$aliases[$alias]); } } \ No newline at end of file diff --git a/laravel/paginator.php b/laravel/paginator.php index b4727cc7..33cf7a75 100644 --- a/laravel/paginator.php +++ b/laravel/paginator.php @@ -54,14 +54,7 @@ class Paginator { protected $appendage; /** - * The current request instance. - * - * @var Request - */ - protected $request; - - /** - * The paginatino elements that will be generated. + * The pagination elements that will be generated. * * @var array */ @@ -84,10 +77,6 @@ protected function __construct($results, $page, $total, $per_page, $last) $this->total = $total; $this->results = $results; $this->per_page = $per_page; - - // Grab the active request instance. This is used to determine the current URI - // and to determine if HTTPS links should be generated. - $this->request = IoC::container()->core('request'); } /** @@ -112,7 +101,7 @@ public static function make($results, $total, $per_page) */ public static function page($total, $per_page) { - $page = IoC::container()->core('input')->get('page', 1); + $page = Input::get('page', 1); // The page will be validated and adjusted if it is less than one or greater // than the last page. For example, if the current page is not an integer or @@ -253,7 +242,7 @@ protected function element($element, $text, $page, $disabler) // We will assume the page links should use HTTPS if the current request // is also using HTTPS. Since pagination links automatically point to // the current URI, this makes pretty good sense. - list($uri, $secure) = array($this->request->uri(), $this->request->secure()); + list($uri, $secure) = array(URI::get(), Request::secure()); return HTML::link($uri.$this->appendage($element, $page), $text, array('class' => $class), $secure); } 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 @@ -uri = $uri; - $this->post = $post; - $this->server = $server; - } - /** * Get the URI for the current request. * @@ -52,9 +23,9 @@ public function __construct(URI $uri, $server, $post) * * @return string */ - public function uri() + public static function uri() { - return $this->uri->get(); + return URI::get(); } /** @@ -64,9 +35,9 @@ public function uri() * * @return string */ - public function format() + public static function format() { - return (($extension = pathinfo($this->uri->get(), PATHINFO_EXTENSION)) !== '') ? $extension : 'html'; + return (($extension = pathinfo(URI::get(), PATHINFO_EXTENSION)) !== '') ? $extension : 'html'; } /** @@ -78,9 +49,9 @@ public function format() * * @return string */ - public function method() + public static function method() { - return ($this->spoofed()) ? $this->post[Request::spoofer] : $this->server['REQUEST_METHOD']; + return (static::spoofed()) ? $_POST[Request::spoofer] : $_SERVER['REQUEST_METHOD']; } /** @@ -92,9 +63,9 @@ public function method() * @param mixed $default * @return string */ - public function server($key = null, $default = null) + public static function server($key = null, $default = null) { - return Arr::get($this->server, strtoupper($key), $default); + return Arr::get($_SERVER, strtoupper($key), $default); } /** @@ -106,9 +77,9 @@ public function server($key = null, $default = null) * * @return bool */ - public function spoofed() + public static function spoofed() { - return is_array($this->post) and array_key_exists(Request::spoofer, $this->post); + return is_array($_POST) and array_key_exists(Request::spoofer, $_POST); } /** @@ -117,19 +88,19 @@ public function spoofed() * @param mixed $default * @return string */ - public function ip($default = '0.0.0.0') + public static function ip($default = '0.0.0.0') { - if (isset($this->server['HTTP_X_FORWARDED_FOR'])) + if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { - return $this->server['HTTP_X_FORWARDED_FOR']; + return $_SERVER['HTTP_X_FORWARDED_FOR']; } - elseif (isset($this->server['HTTP_CLIENT_IP'])) + elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { - return $this->server['HTTP_CLIENT_IP']; + return $_SERVER['HTTP_CLIENT_IP']; } - elseif (isset($this->server['REMOTE_ADDR'])) + elseif (isset($_SERVER['REMOTE_ADDR'])) { - return $this->server['REMOTE_ADDR']; + return $_SERVER['REMOTE_ADDR']; } return ($default instanceof Closure) ? call_user_func($default) : $default; @@ -143,9 +114,9 @@ public function ip($default = '0.0.0.0') * * @return string */ - public function protocol() + public static function protocol() { - return (isset($this->server['HTTPS']) and $this->server['HTTPS'] !== 'off') ? 'https' : 'http'; + return (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; } /** @@ -153,9 +124,9 @@ public function protocol() * * @return bool */ - public function secure() + public static function secure() { - return $this->protocol() == 'https'; + return static::protocol() == 'https'; } /** @@ -163,11 +134,11 @@ public function secure() * * @return bool */ - public function ajax() + public static function ajax() { - if ( ! isset($this->server['HTTP_X_REQUESTED_WITH'])) return false; + if ( ! isset($_SERVER['HTTP_X_REQUESTED_WITH'])) return false; - return strtolower($this->server['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'; + return strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'; } /** @@ -175,6 +146,6 @@ public function ajax() * * @return Route */ - public function route() { return $this->route; } + public static function route() { return static::$route; } } \ No newline at end of file diff --git a/laravel/response.php b/laravel/response.php index f04b6d00..a5de21df 100644 --- a/laravel/response.php +++ b/laravel/response.php @@ -133,7 +133,7 @@ public static function make($content, $status = 200, $headers = array()) */ public static function view($view, $data = array()) { - return new static(IoC::container()->core('view')->make($view, $data)); + return new static(View::make($view, $data)); } /** @@ -153,7 +153,7 @@ public static function view($view, $data = array()) */ public static function of($name, $data = array()) { - return new static(IoC::container()->core('view')->of($name, $data)); + return new static(View::of($name, $data)); } /** @@ -177,7 +177,7 @@ public static function of($name, $data = array()) */ public static function error($code, $data = array()) { - return new static(IoC::container()->core('view')->make('error/'.$code, $data), $code); + return new static(View::make('error/'.$code, $data), $code); } /** diff --git a/laravel/routing/router.php b/laravel/routing/router.php index c9ec23d9..fad899ab 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -87,12 +87,11 @@ public function find($name) /** * Search the routes for the route matching a request method and URI. * - * @param Request $request * @param string $method * @param string $uri * @return Route */ - public function route(Request $request, $method, $uri) + public function route($method, $uri) { $routes = $this->loader->load($uri); @@ -104,7 +103,7 @@ public function route(Request $request, $method, $uri) // no need to spin through all of the routes. if (isset($routes[$destination])) { - return $request->route = new Route($destination, $routes[$destination], array()); + return Request::$route = new Route($destination, $routes[$destination], array()); } foreach ($routes as $keys => $callback) @@ -120,13 +119,13 @@ public function route(Request $request, $method, $uri) if (preg_match('#^'.$this->translate_wildcards($key).'$#', $destination)) { - return $request->route = new Route($keys, $callback, $this->parameters($destination, $key)); + return Request::$route = new Route($keys, $callback, $this->parameters($destination, $key)); } } } } - return $request->route = $this->route_to_controller($method, $uri, $destination); + return Request::$route = $this->route_to_controller($method, $uri, $destination); } /** diff --git a/laravel/security/auth.php b/laravel/security/auth.php index cbfa6233..2bb2b606 100644 --- a/laravel/security/auth.php +++ b/laravel/security/auth.php @@ -1,5 +1,6 @@ session = $session; - } - /** * Determine if the current user of the application is authenticated. * * @return bool */ - public function check() + public static function check() { - return ! is_null($this->user()); + return ! is_null(static::user()); } /** @@ -63,11 +46,13 @@ public function check() * * @return object */ - public function user() + public static function user() { - if ( ! is_null($this->user)) return $this->user; + if ( ! is_null(static::$user)) return static::$user; - return $this->user = call_user_func(Config::get('auth.user'), $this->session->get(Auth::user_key)); + $id = IoC::container()->core('session')->get(Auth::user_key); + + return static::$user = call_user_func(Config::get('auth.user'), $id); } /** @@ -80,11 +65,11 @@ public function user() * @param string $password * @return bool */ - public function attempt($username, $password = null) + public static function attempt($username, $password = null) { if ( ! is_null($user = call_user_func(Config::get('auth.attempt'), $username, $password))) { - $this->remember($user); + static::remember($user); return true; } @@ -100,11 +85,11 @@ public function attempt($username, $password = null) * @param object $user * @return void */ - public function remember($user) + public static function remember($user) { - $this->user = $user; + static::$user = $user; - $this->session->put(Auth::user_key, $user->id); + IoC::container()->core('session')->put(Auth::user_key, $user->id); } /** @@ -114,13 +99,13 @@ public function remember($user) * * @return void */ - public function logout() + public static function logout() { - call_user_func(Config::get('auth.logout'), $this->user()); + call_user_func(Config::get('auth.logout'), static::user()); - $this->user = null; + static::$user = null; - $this->session->forget(Auth::user_key); + IoC::container()->core('session')->forget(Auth::user_key); } } \ No newline at end of file diff --git a/laravel/security/crypter.php b/laravel/security/crypter.php index 3d4abdda..4cf3f2ea 100644 --- a/laravel/security/crypter.php +++ b/laravel/security/crypter.php @@ -1,5 +1,12 @@ key = $key; - $this->mode = $mode; - $this->cipher = $cipher; - - if (trim((string) $this->key) === '') - { - throw new \Exception('The encryption class may not be used without an encryption key.'); - } - } + protected static $mode = 'cbc'; /** * Encrypt a string using Mcrypt. @@ -60,10 +37,10 @@ public function __construct($cipher, $mode, $key) * @param string $value * @return string */ - public function encrypt($value) + public static function encrypt($value) { - // Determine the most appropriate random number generator for the operating - // system and environment the application is running on. + // Determine the most appropriate random number generator for the + // OS and system and environment the application is running on. if (defined('MCRYPT_DEV_URANDOM')) { $randomizer = MCRYPT_DEV_URANDOM; @@ -77,16 +54,14 @@ public function encrypt($value) $randomizer = MCRYPT_RAND; } - $iv = mcrypt_create_iv($this->iv_size(), $randomizer); + $iv = mcrypt_create_iv(static::iv_size(), $randomizer); - return base64_encode($iv.mcrypt_encrypt($this->cipher, $this->key, $value, $this->mode, $iv)); + return base64_encode($iv.mcrypt_encrypt(static::$cipher, Config::get('application.key'), $value, static::$mode, $iv)); } /** * Decrypt a string using Mcrypt. * - * The string will be decrypted using the cipher and mode specified when the crypter was created. - * * * // Decrypt a string using the Mcrypt PHP extension * $decrypted = Crypter::decrypt($secret); @@ -95,7 +70,7 @@ public function encrypt($value) * @param string $value * @return string */ - public function decrypt($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. @@ -104,10 +79,13 @@ public function decrypt($value) throw new \Exception('Decryption error. Input value is not valid base64 data.'); } - // Extract the input vector and the encrypted string from the value - list($iv, $value) = array(substr($value, 0, $this->iv_size()), substr($value, $this->iv_size())); + // 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()); - return rtrim(mcrypt_decrypt($this->cipher, $this->key, $value, $this->mode, $iv), "\0"); + $value = substr($value, static::iv_size()); + + return rtrim(mcrypt_decrypt(static::$cipher, Config::get('application.key'), $value, static::$mode, $iv), "\0"); } /** @@ -117,9 +95,9 @@ public function decrypt($value) * * @return int */ - private function iv_size() + protected static function iv_size() { - return mcrypt_get_iv_size($this->cipher, $this->mode); + return mcrypt_get_iv_size(static::$cipher, static::$mode); } } \ No newline at end of file diff --git a/laravel/session/drivers/cookie.php b/laravel/session/drivers/cookie.php index 92f12f62..188de1bf 100644 --- a/laravel/session/drivers/cookie.php +++ b/laravel/session/drivers/cookie.php @@ -4,37 +4,6 @@ class Cookie implements Driver { - /** - * The crypter instance. - * - * All session cookies have an encrypted payload. Since the session contains sensitive - * data that cannot be compromised, it is important that the payload be encrypted using - * the strong encryption provided by the Crypter class. - * - * @var Crypter - */ - private $crypter; - - /** - * The cookie manager instance. - * - * @var Cookie - */ - private $cookies; - - /** - * Create a new Cookie session driver instance. - * - * @param Crypter $crypter - * @param Cookie $cookies - * @return void - */ - public function __construct(Crypter $crypter, \Laravel\Cookie $cookies) - { - $this->crypter = $crypter; - $this->cookies = $cookies; - } - /** * Load a session from storage by a given ID. * @@ -45,9 +14,9 @@ public function __construct(Crypter $crypter, \Laravel\Cookie $cookies) */ public function load($id) { - if ($this->cookies->has('session_payload')) + if (\Laravel\Cookie::has('session_payload')) { - return unserialize($this->crypter->decrypt($this->cookies->get('session_payload'))); + return unserialize(Crypter::decrypt(\Laravel\Cookie::get('session_payload'))); } } @@ -63,9 +32,9 @@ public function save($session, $config, $exists) { extract($config); - $payload = $this->crypter->encrypt(serialize($session)); + $payload = Crypter::encrypt(serialize($session)); - $this->cookies->put('session_payload', $payload, $lifetime, $path, $domain); + \Laravel\Cookie::put('session_payload', $payload, $lifetime, $path, $domain); } /** @@ -76,7 +45,7 @@ public function save($session, $config, $exists) */ public function delete($id) { - $this->cookies->forget('session_payload'); + \Laravel\Cookie::forget('session_payload'); } } \ No newline at end of file diff --git a/laravel/session/transporters/cookie.php b/laravel/session/transporters/cookie.php index 73790a0e..d7b4d648 100644 --- a/laravel/session/transporters/cookie.php +++ b/laravel/session/transporters/cookie.php @@ -2,13 +2,6 @@ class Cookie implements Transporter { - /** - * The cookie manager instance. - * - * @var Cookie - */ - protected $cookies; - /** * The name of the cookie used to store the session ID. * @@ -16,17 +9,6 @@ class Cookie implements Transporter { */ const key = 'laravel_session'; - /** - * Create a new cookie session transporter instance. - * - * @param Cookie $cookie - * @return void - */ - public function __construct(\Laravel\Cookie $cookies) - { - $this->cookies = $cookies; - } - /** * Get the session identifier for the request. * @@ -35,7 +17,7 @@ public function __construct(\Laravel\Cookie $cookies) */ public function get($config) { - return $this->cookies->get(Cookie::key); + return \Laravel\Cookie::get(Cookie::key); } /** @@ -52,7 +34,7 @@ public function put($id, $config) // deleted until the user closes their browser. $minutes = ( ! $config['expire_on_close']) ? $config['lifetime'] : 0; - $this->cookies->put(Cookie::key, $id, $minutes, $config['path'], $config['domain']); + \Laravel\Cookie::put(Cookie::key, $id, $minutes, $config['path'], $config['domain']); } } \ No newline at end of file diff --git a/laravel/uri.php b/laravel/uri.php index 93310beb..18745f36 100644 --- a/laravel/uri.php +++ b/laravel/uri.php @@ -9,25 +9,7 @@ class URI { * * @var string */ - protected $uri; - - /** - * The $_SERVER array for the current request. - * - * @var array - */ - protected $server; - - /** - * Create a new URI parser instance. - * - * @param array $server - * @return void - */ - public function __construct($server) - { - $this->server = $server; - } + protected static $uri; /** * Determine the request URI. @@ -41,16 +23,16 @@ public function __construct($server) * * @return string */ - public function get() + public static function get() { - if ( ! is_null($this->uri)) return $this->uri; + if ( ! is_null(static::$uri)) return static::$uri; - if (($uri = $this->from_server()) === false) + if (($uri = static::from_server()) === false) { throw new \Exception('Malformed request URI. Request terminated.'); } - return $this->uri = $this->format($this->clean($uri)); + return static::$uri = static::format(static::clean($uri)); } /** @@ -68,9 +50,9 @@ public function get() * @param mixed $default * @return string */ - public function segment($segment = null, $default = null) + public static function segment($segment = null, $default = null) { - $segments = Arr::without(explode('/', $this->get()), array('')); + $segments = Arr::without(explode('/', static::get()), array('')); if ( ! is_null($segment)) $segment = $segment - 1; @@ -82,20 +64,20 @@ public function segment($segment = null, $default = null) * * @return string */ - protected function from_server() + protected static function from_server() { // If the PATH_INFO $_SERVER element is set, we will use since it contains // the request URI formatted perfectly for Laravel's routing engine. - if (isset($this->server['PATH_INFO'])) + if (isset($_SERVER['PATH_INFO'])) { - return $this->server['PATH_INFO']; + return $_SERVER['PATH_INFO']; } // If the REQUEST_URI is set, we need to extract the URL path since this // should return the URI formatted in a manner similar to PATH_INFO. - elseif (isset($this->server['REQUEST_URI'])) + elseif (isset($_SERVER['REQUEST_URI'])) { - return parse_url($this->server['REQUEST_URI'], PHP_URL_PATH); + return parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); } throw new \Exception('Unable to determine the request URI.'); @@ -110,7 +92,7 @@ protected function from_server() * @param string $uri * @return string */ - protected function clean($uri) + protected static function clean($uri) { foreach (array(parse_url(Config::get('application.url'), PHP_URL_PATH), '/index.php') as $value) { @@ -128,7 +110,7 @@ protected function clean($uri) * @param string $uri * @return string */ - protected function format($uri) + protected static function format($uri) { return (($uri = trim($uri, '/')) == '') ? '/' : $uri; } diff --git a/laravel/url.php b/laravel/url.php index ad04111c..409045a4 100644 --- a/laravel/url.php +++ b/laravel/url.php @@ -57,7 +57,7 @@ public static function to_secure($url = '') */ public static function to_asset($url, $https = null) { - if (is_null($https)) $https = IoC::container()->core('request')->secure(); + if (is_null($https)) $https = Request::secure(); return str_replace('index.php/', '', static::to($url, $https)); } diff --git a/laravel/validation/validator.php b/laravel/validation/validator.php index 4f544455..91d6ec77 100644 --- a/laravel/validation/validator.php +++ b/laravel/validation/validator.php @@ -4,6 +4,7 @@ use Laravel\IoC; use Laravel\Str; use Laravel\Lang; +use Laravel\Input; use Laravel\Database\Manager as DB; class Validator { @@ -304,9 +305,7 @@ protected function get_size($attribute) $value = $this->attributes[$attribute]; - $files = IoC::container()->resolve('laravel.input')->file(); - - return (array_key_exists($attribute, $files)) ? $value['size'] / 1024 : Str::length(trim($value)); + return (array_key_exists($attribute, Input::file())) ? $value['size'] / 1024 : Str::length(trim($value)); } /** @@ -478,7 +477,7 @@ protected function get_message($attribute, $rule) // the default error message for the appropriate units. if (in_array($rule, $this->size_rules) and ! $this->has_rule($attribute, $this->numeric_rules)) { - return (array_key_exists($attribute, IoC::container()->resolve('laravel.input')->file())) + return (array_key_exists($attribute, Input::file())) ? rtrim($message, '.').' '.Lang::line('validation.kilobytes')->get($this->language).'.' : rtrim($message, '.').' '.Lang::line('validation.characters')->get($this->language).'.'; } diff --git a/laravel/view.php b/laravel/view.php index dc9af0c0..47c32e2e 100644 --- a/laravel/view.php +++ b/laravel/view.php @@ -1,195 +1,5 @@ path = $path; - $this->composer = $composer; - $this->compiled = $compiled; - } - - /** - * Create a new view instance. - * - * The name of the view given to this method should correspond to a view - * within your application views directory. Dots or slashes may used to - * reference views within sub-directories. - * - * - * // Create a new view instance - * $view = View::make('home.index'); - * - * // Create a new view instance with bound data - * $view = View::make('home.index', array('name' => 'Taylor')); - * - * - * @param string $view - * @param array $data - * @return View - */ - public function make($view, $data = array()) - { - return new View($this, $this->composer, $view, $data, $this->path($view)); - } - - /** - * Create a new view instance from a view name. - * - * View names are defined in the application composers file. - * - * - * // Create an instance of the "layout" named view - * $view = View::of('layout'); - * - * // Create an instance of the "layout" view with bound data - * $view = View::of('layout', array('name' => 'Taylor')); - * - * - * @param string $name - * @param array $data - * @return View - */ - public function of($name, $data = array()) - { - if ( ! is_null($view = $this->composer->name($name))) return $this->make($view, $data); - - throw new \Exception("Named view [$name] is not defined."); - } - - /** - * Get the path to a given view on disk. - * - * @param string $view - * @return string - */ - protected function path($view) - { - $view = str_replace('.', '/', $view); - - foreach (array(EXT, BLADE_EXT) as $extension) - { - if (file_exists($path = $this->path.$view.$extension)) return $path; - } - - throw new \Exception("View [$view] does not exist."); - } - - /** - * Magic Method for handling the dynamic creation of named views. - * - * - * // Create an instance of the "layout" named view - * $view = View::of_layout(); - * - * // Create an instance of a named view with data - * $view = View::of_layout(array('name' => 'Taylor')); - * - */ - public function __call($method, $parameters) - { - if (strpos($method, 'of_') === 0) - { - return $this->of(substr($method, 3), Arr::get($parameters, 0, array())); - } - } - -} - - -class View_Composer { - - /** - * The view composers. - * - * @var array - */ - protected $composers; - - /** - * Create a new view composer instance. - * - * @param array $composers - * @return void - */ - public function __construct($composers) - { - $this->composers = $composers; - } - - /** - * Find the key for a view by name. - * - * The view's key can be used to create instances of the view through the typical - * methods available on the view factory. - * - * @param string $name - * @return string - */ - public function name($name) - { - // 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, the composer - // could have an array value in which a "name" key exists. - 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) - { - // The shared composer is called for every view instance. This allows the - // convenient binding of global view data or partials within a single method. - if (isset($this->composers['shared'])) call_user_func($this->composers['shared'], $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); - } - } - } - -} - - class View { /** @@ -214,36 +24,138 @@ class View { protected $path; /** - * The view composer instance. + * All of the view composers for the application. * - * @var View_Composer + * @var array */ - protected $composer; - - /** - * The view factory instance, which is used to create sub-views. - * - * @var View_Factory - */ - protected $factory; + protected static $composers; /** * Create a new view instance. * - * @param View_Factory $factory - * @param View_Composer $composer - * @param string $view - * @param array $data - * @param string $path + * @param string $view + * @param array $data * @return void */ - public function __construct(View_Factory $factory, View_Composer $composer, $view, $data, $path) + public function __construct($view, $data = array()) { $this->view = $view; $this->data = $data; - $this->path = $path; - $this->factory = $factory; - $this->composer = $composer; + $this->path = $this->path($view); + } + + /** + * Get the path to a given view on disk. + * + * The view may be either a "normal" view or a "Blade" view, so we will + * need to check the view directory for either extension. + * + * @param string $view + * @return string + */ + protected function path($view) + { + $view = str_replace('.', '/', $view); + + foreach (array(EXT, BLADE_EXT) as $extension) + { + if (file_exists($path = VIEW_PATH.$view.$extension)) return $path; + } + + throw new \Exception("View [$view] does not exist."); + } + + /** + * Create a new view instance. + * + * The name of the view given to this method should correspond to a view + * within your application views directory. Dots or slashes may used to + * reference views within sub-directories. + * + * + * // Create a new view instance + * $view = View::make('home.index'); + * + * // Create a new view instance with bound data + * $view = View::make('home.index', array('name' => 'Taylor')); + * + * + * @param string $view + * @param array $data + * @return View + */ + public static function make($view, $data = array()) + { + return new static($view, $data); + } + + /** + * Create a new view instance from a view name. + * + * View names are defined in the application composers file. + * + * + * // Create an instance of the "layout" named view + * $view = View::of('layout'); + * + * // Create an instance of the "layout" view with bound data + * $view = View::of('layout', array('name' => 'Taylor')); + * + * + * @param string $name + * @param array $data + * @return View + */ + public static function of($name, $data = array()) + { + if ( ! is_null($view = static::name($name))) return static::make($view, $data); + + throw new \Exception("Named view [$name] is not defined."); + } + + /** + * Find the key for a view by name. + * + * The view's key can be used to create instances of the view through the typical + * methods available on the view factory. + * + * @param string $name + * @return string + */ + 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, the composer + // could have an array value in which a "name" key exists. + foreach (static::$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 + */ + protected static function compose(View $view) + { + if (is_null(static::$composers)) static::$composers = require APP_PATH.'composers'.EXT; + + // The shared composer is called for every view instance. This allows the + // convenient binding of global view data or partials within a single method. + if (isset(static::$composers['shared'])) call_user_func(static::$composers['shared'], $view); + + if (isset(static::$composers[$view->view])) + { + foreach ((array) static::$composers[$view->view] as $key => $value) + { + if ($value instanceof Closure) return call_user_func($value, $view); + } + } } /** @@ -253,7 +165,7 @@ public function __construct(View_Factory $factory, View_Composer $composer, $vie */ public function render() { - $this->composer->compose($this); + 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 @@ -288,7 +200,7 @@ protected function compile() // For simplicity, compiled views are stored in a single directory by the MD5 hash of // their name. This allows us to avoid recreating the entire view directory structure // within the compiled views directory. - $compiled = $this->factory->compiled.md5($this->view); + $compiled = STORAGE_PATH.'views/'.md5($this->view); // The view will only be re-compiled if the view has been modified since the last compiled // version of the view was created or no compiled view exists. Otherwise, the path will @@ -322,7 +234,7 @@ protected function compile() */ public function nest($key, $view, $data = array()) { - return $this->with($key, $this->factory->make($view, $data)); + return $this->with($key, static::make($view, $data)); } /** @@ -373,4 +285,23 @@ public function __unset($key) unset($this->data[$key]); } + /** + * Magic Method for handling the dynamic creation of named views. + * + * + * // Create an instance of the "layout" named view + * $view = View::of_layout(); + * + * // Create an instance of a named view with data + * $view = View::of_layout(array('name' => 'Taylor')); + * + */ + public static function __callStatic($method, $parameters) + { + if (strpos($method, 'of_') === 0) + { + return static::of(substr($method, 3), Arr::get($parameters, 0, array())); + } + } + } \ No newline at end of file