Microsoft_WindowsAzure
[ class tree: Microsoft_WindowsAzure ] [ index: Microsoft_WindowsAzure ] [ all elements ]

Source for file RoleEnvironment.php

Documentation is available at RoleEnvironment.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009 - 2011, RealDolmen
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *     * Redistributions of source code must retain the above copyright
  9.  *       notice, this list of conditions and the following disclaimer.
  10.  *     * Redistributions in binary form must reproduce the above copyright
  11.  *       notice, this list of conditions and the following disclaimer in the
  12.  *       documentation and/or other materials provided with the distribution.
  13.  *     * Neither the name of RealDolmen nor the
  14.  *       names of its contributors may be used to endorse or promote products
  15.  *       derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  *
  28.  * @category   Microsoft
  29.  * @package    Microsoft_WindowsAzure
  30.  * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  31.  * @license    http://phpazure.codeplex.com/license
  32.  * @version    $Id: Storage.php 61042 2011-04-19 10:03:39Z unknown $
  33.  */
  34.  
  35. /**
  36.  * @see Microsoft_AutoLoader
  37.  */
  38. require_once dirname(__FILE__'/../AutoLoader.php';
  39.  
  40. /**
  41.  * @category   Microsoft
  42.  * @package    Microsoft_WindowsAzure
  43.  * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  44.  * @license    http://phpazure.codeplex.com/license
  45.  */
  46. {
  47.     /**
  48.      * First-level command cache
  49.      * 
  50.      * @var array 
  51.      */
  52.     private static $_commandCache array();
  53.         // TODO: may be interesting to cache commands for subsequent requests as well
  54.  
  55.     /**
  56.      * Is the Windows Azure role environment available? Will return true on Windows Azure and Development Fabric, false when run in any other environment.
  57.      * 
  58.      * @return boolean 
  59.      */
  60.     public static function isAvailable()
  61.     {
  62.         $value strtolower(self::_runCommand('IsAvailable'));
  63.         return $value == 'true';
  64.     }
  65.     
  66.     /**
  67.      * Is the application running in the development fabric?
  68.      * 
  69.      * @return boolean 
  70.      */
  71.     public static function isDevFabric()
  72.     {
  73.         $value strtolower(self::_runCommand('IsEmulated'));
  74.         return $value == 'true';
  75.     }
  76.     
  77.     /**
  78.      * Gets the current role instance ID. Note that this method will return null when not hosted on Windows Azure.
  79.      * 
  80.      * @return string 
  81.      */
  82.     public static function getCurrentRoleInstanceId()
  83.     {
  84.         if (!self::isAvailable()) {
  85.             return null;
  86.         }
  87.         
  88.         $value self::_runCommand('GetCurrentRoleInstanceId');
  89.         return $value;
  90.     }
  91.     
  92.     /**
  93.      * Gets the current role name. Note that this method will return null when not hosted on Windows Azure.
  94.      * 
  95.      * @return string 
  96.      */
  97.     public static function getCurrentRoleName(
  98.     {
  99.         if (!self::isAvailable()) {
  100.             return null;
  101.         }
  102.         
  103.         $value self::_runCommand('GetCurrentRoleName');
  104.         return $value;
  105.     }
  106.     
  107.     /**
  108.      * Gets the current deployment ID. Note that this method will return null when not hosted on Windows Azure.
  109.      * 
  110.      * @return string 
  111.      */
  112.     public static function getDeploymentId(
  113.     {
  114.         if (!self::isAvailable()) {
  115.             return null;
  116.         }
  117.         
  118.         $value self::_runCommand('GetDeploymentId');
  119.         return $value;
  120.     }
  121.  
  122.     /**
  123.      * Get configuration setting value from ServiceConfiguration.cscfg. Note that this method will return null when not hosted on Windows Azure.
  124.      * 
  125.      * @param string $configurationKey Configuration key.
  126.      * @return string 
  127.      */
  128.     public static function getConfigurationSettingValue($configurationKey)
  129.     {
  130.         if (!self::isAvailable()) {
  131.             return null;
  132.         }
  133.         
  134.         $value self::_runCommand('GetConfigurationSettingValue'array($configurationKey));
  135.         return $value;
  136.     }
  137.  
  138.     /**
  139.      * Get local resource root path from ServiceDefinition.csdef. Note that this method will return null when not hosted on Windows Azure.
  140.      * 
  141.      * @param string $localResourceKey Local resource key.
  142.      * @return string 
  143.      */
  144.     public static function getLocalResource($localResourceKey)
  145.     {
  146.         if (!self::isAvailable()) {
  147.             return null;
  148.         }
  149.         
  150.         $value self::_runCommand('GetLocalResource'array($localResourceKey));
  151.         return $value;
  152.     }
  153.  
  154.     /**
  155.      * Run a command through RoleEnvironmentProxy.exe.
  156.      * 
  157.      * @param string $command Command to run.
  158.      * @param array $parameters Command parameters.
  159.      * @return mixed 
  160.      */
  161.     protected static function _runCommand($command$parameters array())
  162.     {
  163.         for ($i 0$i count($parameters)$i++{
  164.             $parameters[$iescapeshellarg($parameters[$i]);
  165.         }
  166.         $parameters implode(' '$parameters);
  167.         $command escapeshellarg($command' ' $parameters;
  168.  
  169.         // Check for cached result
  170.         if (array_key_exists($commandself::$_commandCache)) {
  171.             return self::$_commandCache[$command];
  172.         }
  173.  
  174.         // Run command
  175.         $h popen(dirname(__FILE__'/bin/RoleEnvironmentProxy.exe ' $command ' 2>&1''r');
  176.         $value '';
  177.         while (!feof($h)) {
  178.           $value .= fread($h8192);
  179.         }
  180.         pclose($h);
  181.         
  182.         // Parse value
  183.         if ($value == 'null'{
  184.             $value null;
  185.         }
  186.         self::$_commandCache[$command$value;
  187.         return $value;
  188.     }
  189. }

Documentation generated on Sat, 03 Dec 2011 13:59:35 +0100 by phpDocumentor 1.4.3