home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / phing / system / util / Register.php < prev    next >
Encoding:
PHP Script  |  2006-09-14  |  2.8 KB  |  116 lines

  1. <?php
  2.  
  3. /**
  4.  * Static class to handle a slot-listening system.
  5.  *
  6.  * Unlike the slots/signals Qt model, this class manages something that is
  7.  * more like a simple hashtable, where each slot has only one value.  For that
  8.  * reason "Registers" makes more sense, the reference being to CPU registers.
  9.  *
  10.  * This could be used for anything, but it's been built for a pretty specific phing
  11.  * need, and that is to allow access to dynamic values that are set by logic
  12.  * that is not represented in a build file.  For exampe, we need a system for getting
  13.  * the current resource (file) that is being processed by a filterchain in a fileset.
  14.  * 
  15.  * Each slot corresponds to only one read-only, dynamic-value RegisterSlot object. In
  16.  * a build.xml register slots are expressed using a syntax similar to variables:
  17.  * 
  18.  * <replaceregexp>
  19.  *    <regexp pattern="\n" replace="%{task.current_file}"/>
  20.  * </replaceregexp>
  21.  * 
  22.  * The task/type must provide a supporting setter for the attribute:
  23.  * 
  24.  * <code>
  25.  *     function setListeningReplace(RegisterSlot $slot) {
  26.  *        $this->replace = $slot;
  27.  *  }
  28.  *
  29.  *  // in main()
  30.  *  if ($this->replace instanceof RegisterSlot) {
  31.  *        $this->regexp->setReplace($this->replace->getValue());
  32.  *  } else {
  33.  *        $this->regexp->setReplace($this->replace);
  34.  *  }
  35.  * </code>
  36.  *
  37.  * @author Hans Lellelid <hans@xmpl.org>
  38.  * @version $Revision: 1.3 $
  39.  * @package phing.system.util
  40.  */
  41. class Register {
  42.     
  43.     /** Slots that have been registered */
  44.     private static $slots = array();
  45.     
  46.     /**
  47.      * Returns RegisterSlot for specified key.
  48.      * 
  49.      * If not slot exists a new one is created for key.
  50.      * 
  51.      * @param string $key
  52.      * @return RegisterSlot
  53.      */
  54.     public static function getSlot($key) {
  55.         if (!isset(self::$slots[$key])) {
  56.             self::$slots[$key] = new RegisterSlot($key);
  57.         }
  58.         return self::$slots[$key];
  59.     }    
  60. }
  61.  
  62.  
  63. /**
  64.  * Represents a slot in the register.
  65.  */
  66. class RegisterSlot {
  67.     
  68.     /** The name of this slot. */
  69.     private $key;
  70.     
  71.     /** The value for this slot. */
  72.     private $value;
  73.     
  74.     /**
  75.      * Constructs a new RegisterSlot, setting the key to passed param.
  76.      * @param string $key
  77.      */
  78.     public function __construct($key) {
  79.         $this->key = (string) $key;
  80.     }
  81.     
  82.     /**
  83.      * Sets the key / name for this slot.
  84.      * @param string $k
  85.      */
  86.     public function setKey($k) {
  87.         $this->key = (string) $k;
  88.     }
  89.  
  90.     /**
  91.      * Gets the key / name for this slot.
  92.      * @return string
  93.      */
  94.     public function getKey() {
  95.         return $this->key;
  96.     }
  97.     
  98.     /**
  99.      * Sets the value for this slot.
  100.      * @param mixed
  101.      */
  102.     public function setValue($v) {
  103.         $this->value = $v;
  104.     }
  105.     
  106.     /**
  107.      * Returns the value at this slot.
  108.      * @return mixed
  109.      */
  110.     public function getValue() {
  111.         return $this->value;
  112.     }
  113.     
  114. }
  115. ?>
  116.