AS3: Environment
View Example
View Documentation
Download Class & Example Files
UPDATED 10/26/07
The Environment class has been re-versioned to 2.0. It is now a singleton which means you can only have one instance of it anywhere in your application. This is a cleaner approach. The code has changed only slightly as it has added a setPaths() method and obviously you now call everything through Environment.getInstance().
UPDATED 10/22/07
Version 1.1 adds a Boolean, isLive, that can be checked to see if the site is live or not and have other parts of your code react accordingly.
A small utility class that allows you to set debug and live path variable for local and remote testing without having to toggle anything. Made in conjunction with Josh Perez.
-
/**
-
* A small utility class that allows you to set debug and live path variable for local and remote testing without having to toggle anything.
-
*
-
* @usage
-
* <code>
-
* <pre>
-
import com.reintroducing.debug.Environment;
-
var env:Environment = Environment.getInstance();
-
env.setPaths("../", "");
-
if (env.isLive)
-
{
-
trace("The site is live!");
-
}
-
else
-
{
-
trace("The site is being debugged locally");
-
}
-
var xmlURL:String = env.basePath + "xml/data.xml";
-
* </pre>
-
* </code>
-
*
-
* @author Joshua Perez [http://www.joshua-studios.com]
-
* @author Matt Przybylski [http://www.reintroducing.com]
-
* @version 2.0
-
*/
-
-
package com.reintroducing.debug
-
{
-
import flash.system.Capabilities;
-
-
public class Environment
-
{
-
//- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
-
-
// singleton instance
-
private static var _instance:Environment;
-
private static var _allowInstance:Boolean;
-
-
private var _debugPath:String;
-
private var _livePath:String;
-
private var _basePath:String;
-
private var _isLive:Boolean;
-
-
//- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
-
-
public static const DEFAULT_NAME:String = "com.reintroducing.debug.Environment";
-
-
//- CONSTRUCTOR -------------------------------------------------------------------------------------------
-
-
// singleton instance of Environment
-
public static function getInstance():Environment
-
{
-
if (Environment._instance == null)
-
{
-
Environment._allowInstance = true;
-
Environment._instance = new Environment();
-
Environment._allowInstance = false;
-
}
-
-
return Environment._instance;
-
}
-
-
public function Environment()
-
{
-
if (!Environment._allowInstance)
-
{
-
throw new Error("Error: Use Environment.getInstance() instead of the new keyword.");
-
}
-
}
-
-
//- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
-
-
private function setProperPath():void
-
{
-
if (Capabilities.playerType == "PlugIn" || Capabilities.playerType == "ActiveX")
-
{
-
this._basePath = this._livePath;
-
this._isLive = true;
-
}
-
else
-
{
-
this._basePath = this._debugPath;
-
this._isLive = false;
-
}
-
}
-
-
//- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
-
-
/**
-
* Sets the proper debug/live paths.
-
*
-
* @usage <pre><code>Environment.getInstance().setPaths("../", "");</code></pre>
-
*
-
* @param $debugPath A string representing the local (debug) path
-
* @param $livePath A string representing the remote (live) path
-
*/
-
-
public function setPaths($debugPath:String, $livePath:String):void
-
{
-
this._debugPath = $debugPath;
-
this._livePath = $livePath;
-
-
this.setProperPath();
-
}
-
-
//- EVENT HANDLERS ----------------------------------------------------------------------------------------
-
-
-
-
//- GETTERS & SETTERS -------------------------------------------------------------------------------------
-
-
/**
-
* Returns the basePath for use in loading assets.
-
*
-
* @usage <pre><code>var xmlURL:String = Environment.getInstance().basePath + "xml/data.xml";</code></pre>
-
*
-
* @return String
-
*/
-
-
public function get basePath():String
-
{
-
return this._basePath;
-
}
-
-
/**
-
* Returns a Boolean value if the site is on a live server or being tested locally.
-
*
-
* @usage <pre><code>var siteLive:Boolean = Environment.getInstance().isLive;</code></pre>
-
*
-
* @return Boolean
-
*/
-
-
public function get isLive():Boolean
-
{
-
return this._isLive;
-
}
-
-
//- HELPERS -----------------------------------------------------------------------------------------------
-
-
public function toString():String
-
{
-
return "com.reintroducing.debug.Environment";
-
}
-
-
//- END CLASS ---------------------------------------------------------------------------------------------
-
}
-
}