Browse Source

added simpler and easier environment handling.

Taylor Otwell 12 years ago
parent
commit
ab6e364546

+ 70 - 41
laravel/core.php

@@ -108,93 +108,122 @@ Autoloader::namespaces(array(
 
 /*
 |--------------------------------------------------------------------------
-| Set The CLI Options Array
+| Magic Quotes Strip Slashes
 |--------------------------------------------------------------------------
 |
-| If the current request is from the Artisan command-line interface, we
-| will parse the command line arguments and options and set them the
-| array of options in the $_SERVER global array for convenience.
+| Even though "Magic Quotes" are deprecated in PHP 5.3.x, they may still
+| be enabled on the server. To account for this, we will strip slashes
+| on all input arrays if magic quotes are enabled for the server.
 |
 */
 
-if (defined('STDIN'))
+if (magic_quotes())
 {
-	$console = CLI\Command::options($_SERVER['argv']);
+	$magics = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
 
-	list($arguments, $options) = $console;
+	foreach ($magics as &$magic)
+	{
+		$magic = array_strip_slashes($magic);
+	}
+}
 
-	$options = array_change_key_case($options, CASE_UPPER);
+/*
+|--------------------------------------------------------------------------
+| Create The HttpFoundation Request
+|--------------------------------------------------------------------------
+|
+| Laravel uses the HttpFoundation Symfony component to handle the request
+| and response functionality for the framework. This allows us to not
+| worry about that boilerplate code and focus on what matters.
+|
+*/
 
-	$_SERVER['CLI'] = $options;
-}
+use Symfony\Component\HttpFoundation\LaravelRequest as RequestFoundation;
+
+Request::$foundation = RequestFoundation::createFromGlobals();
 
 /*
 |--------------------------------------------------------------------------
-| Set The CLI Laravel Environment
+| Determine The Application Environment
 |--------------------------------------------------------------------------
 |
-| Next we'll set the LARAVEL_ENV variable if the current request is from
-| the Artisan command-line interface. Since the environment is often
-| specified within an Apache .htaccess file, we need to set it here
-| when the request is not coming through Apache.
+| Next we're ready to determine the application environment. This may be
+| set either via the command line options, or, if the request is from
+| the web, via the mapping of URIs to environments that lives in
+| the "paths.php" file for the application and is parsed.
 |
 */
 
-if (isset($_SERVER['CLI']['ENV']))
+if (Request::cli())
+{
+	foreach (Request::foundation()->server->get('argv') as $argument)
+	{
+		if (starts_with($argument, '--env='))
+		{
+			$environment = substr($argument, 6);
+
+			break;
+		}
+	}
+}
+else
 {
-	$_SERVER['LARAVEL_ENV'] = $_SERVER['CLI']['ENV'];
+	$environment = Request::detect_env($environments);
 }
 
 /*
 |--------------------------------------------------------------------------
-| Register The Laravel Bundles
+| Set The Application Environment
 |--------------------------------------------------------------------------
 |
-| Finally we will register all of the bundles that have been defined for
-| the application. None of them will be started, yet but will be setup
-| so that they may be started by the develop at any time.
+| Once we have determined the application environment, we will set it on
+| the global server array of the HttpFoundation request. This makes it
+| available throughout the application, thought it is mainly only
+| used to determine which configuration files to merge in.
 |
 */
 
-$bundles = require path('app').'bundles'.EXT;
-
-foreach ($bundles as $bundle => $config)
+if ( ! is_null($environment))
 {
-	Bundle::register($bundle, $config);
+	Request::foundation()->server->set('LARAVEL_ENV', $environment);
 }
 
 /*
 |--------------------------------------------------------------------------
-| Magic Quotes Strip Slashes
+| Set The CLI Options Array
 |--------------------------------------------------------------------------
 |
-| Even though "Magic Quotes" are deprecated in PHP 5.3.x, they may still
-| be enabled on the server. To account for this, we will strip slashes
-| on all input arrays if magic quotes are enabled for the server.
+| If the current request is from the Artisan command-line interface, we
+| will parse the command line arguments and options and set them the
+| array of options in the $_SERVER global array for convenience.
 |
 */
 
-if (magic_quotes())
+if (defined('STDIN'))
 {
-	$magics = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
+	$console = CLI\Command::options($_SERVER['argv']);
 
-	foreach ($magics as &$magic)
-	{
-		$magic = array_strip_slashes($magic);
-	}
+	list($arguments, $options) = $console;
+
+	$options = array_change_key_case($options, CASE_UPPER);
+
+	$_SERVER['CLI'] = $options;
 }
 
 /*
 |--------------------------------------------------------------------------
-| Create The HttpFoundation Request
+| Register The Laravel Bundles
 |--------------------------------------------------------------------------
 |
-| Laravel uses the HttpFoundation Symfony component to handle the request
-| and response functionality for the framework. This allows us to not
-| worry about that boilerplate code and focus on what matters.
+| Finally we will register all of the bundles that have been defined for
+| the application. None of them will be started, yet but will be setup
+| so that they may be started by the develop at any time.
 |
 */
 
-use Symfony\Component\HttpFoundation\LaravelRequest as RequestFoundation;
+$bundles = require path('app').'bundles'.EXT;
 
-Request::$foundation = RequestFoundation::createFromGlobals();
+foreach ($bundles as $bundle => $config)
+{
+	Bundle::register($bundle, $config);
+}

+ 25 - 0
laravel/request.php

@@ -208,6 +208,31 @@ class Request {
 		return static::env() === $env;
 	}
 
+	/**
+	 * Detect the current environment from an environment configuration.
+	 *
+	 * @param  array        $environments
+	 * @return string|null
+	 */
+	public static function detect_env(array $environments)
+	{
+		$root = static::foundation()->getRootUrl();
+
+		foreach ($environments as $environment => $patterns)
+		{
+			// Essentially we just want to loop through each environment pattern
+			// and determine if the current URI matches the pattern and if so
+			// we'll simply return the environment for that URI pattern.
+			foreach ($patterns as $pattern)
+			{
+				if (Str::is($pattern, $root))
+				{
+					return $environment;
+				}
+			}
+		}
+	}
+
 	/**
 	 * Get the main route handling the request.
 	 *

+ 1 - 3
laravel/url.php

@@ -70,9 +70,7 @@ class URL {
 		}
 		else
 		{
-			$f = Request::foundation();
-
-			$base = $f->getScheme().'://'.$f->getHttpHost().$f->getBasePath();
+			$base = Request::foundation()->getRootUrl();
 		}
 
 		return static::$base = $base;

+ 10 - 0
laravel/vendor/Symfony/Component/HttpFoundation/LaravelRequest.php

@@ -24,4 +24,14 @@ class LaravelRequest extends Request {
         return $request;
     }
 
+    /**
+     * Get the root URL of the application.
+     *
+     * @return string
+     */
+    public function getRootUrl()
+    {
+        return $this->getScheme().'://'.$this->getHttpHost().$this->getBasePath();
+    }
+
 }