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.

Actionscript:
  1. /**
  2. * A small utility class that allows you to set debug and live path variable for local and remote testing without having to toggle anything.
  3. *
  4. * @usage
  5. * <code>
  6. * <pre>
  7. import com.reintroducing.debug.Environment;
  8. var env:Environment = Environment.getInstance();
  9. env.setPaths("../", "");
  10. if (env.isLive)
  11. {
  12.     trace("The site is live!");
  13. }
  14. else
  15. {
  16.     trace("The site is being debugged locally");
  17. }
  18. var xmlURL:String = env.basePath + "xml/data.xml";
  19. * </pre>
  20. * </code>
  21. *
  22. * @author Joshua Perez [http://www.joshua-studios.com]
  23. * @author Matt Przybylski [http://www.reintroducing.com]
  24. * @version 2.0
  25. */
  26.  
  27. package com.reintroducing.debug
  28. {
  29.     import flash.system.Capabilities;
  30.    
  31.     public class Environment
  32.     {
  33. //- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
  34.  
  35.         // singleton instance
  36.         private static var _instance:Environment;
  37.         private static var _allowInstance:Boolean;
  38.        
  39.         private var _debugPath:String;
  40.         private var _livePath:String;
  41.         private var _basePath:String;
  42.         private var _isLive:Boolean;
  43.        
  44. //- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
  45.        
  46.         public static const DEFAULT_NAME:String = "com.reintroducing.debug.Environment";
  47.        
  48. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  49.    
  50.         // singleton instance of Environment
  51.         public static function getInstance():Environment
  52.         {
  53.             if (Environment._instance == null)
  54.             {
  55.                 Environment._allowInstance = true;
  56.                 Environment._instance = new Environment();
  57.                 Environment._allowInstance = false;
  58.             }
  59.            
  60.             return Environment._instance;
  61.         }
  62.        
  63.         public function Environment()
  64.         {
  65.             if (!Environment._allowInstance)
  66.             {
  67.                 throw new Error("Error: Use Environment.getInstance() instead of the new keyword.");
  68.             }
  69.         }
  70.        
  71. //- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
  72.        
  73.         private function setProperPath():void
  74.         {
  75.             if (Capabilities.playerType == "PlugIn" || Capabilities.playerType == "ActiveX")
  76.             {
  77.                 this._basePath = this._livePath;
  78.                 this._isLive = true;
  79.             }
  80.             else
  81.             {
  82.                 this._basePath = this._debugPath;
  83.                 this._isLive = false;
  84.             }
  85.         }
  86.        
  87. //- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
  88.    
  89.         /**
  90.          * Sets the proper debug/live paths.
  91.          *
  92.          * @usage <pre><code>Environment.getInstance().setPaths("../", "");</code></pre>
  93.          *
  94.          * @param $debugPath A string representing the local (debug) path
  95.          * @param $livePath A string representing the remote (live) path
  96.          */
  97.        
  98.         public function setPaths($debugPath:String, $livePath:String):void
  99.         {
  100.             this._debugPath = $debugPath;
  101.             this._livePath = $livePath;
  102.            
  103.             this.setProperPath();
  104.         }
  105.    
  106. //- EVENT HANDLERS ----------------------------------------------------------------------------------------
  107.    
  108.        
  109.    
  110. //- GETTERS & SETTERS -------------------------------------------------------------------------------------
  111.    
  112.         /**
  113.          * Returns the basePath for use in loading assets.
  114.          *
  115.          * @usage <pre><code>var xmlURL:String = Environment.getInstance().basePath + "xml/data.xml";</code></pre>
  116.          *
  117.          * @return String
  118.          */
  119.        
  120.         public function get basePath():String
  121.         {
  122.             return this._basePath;
  123.         }
  124.        
  125.         /**
  126.          * Returns a Boolean value if the site is on a live server or being tested locally.
  127.          *
  128.          * @usage <pre><code>var siteLive:Boolean = Environment.getInstance().isLive;</code></pre>
  129.          *
  130.          * @return Boolean
  131.          */
  132.        
  133.         public function get isLive():Boolean
  134.         {
  135.             return this._isLive;
  136.         }
  137.    
  138. //- HELPERS -----------------------------------------------------------------------------------------------
  139.    
  140.         public function toString():String
  141.         {
  142.             return "com.reintroducing.debug.Environment";
  143.         }
  144.    
  145. //- END CLASS ---------------------------------------------------------------------------------------------
  146.     }
  147. }

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

1 Comment

Awesome Matt! Glad we were both able to come up with this. Hopefully people will use this. I know I definitely will.

Leave a comment

(required)

(required)