summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/system/util/Register.php
blob: 5ef2b2fd7b9761e0c423f478aba1335c2c74fa92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

/**
 * Static class to handle a slot-listening system.
 *
 * Unlike the slots/signals Qt model, this class manages something that is
 * more like a simple hashtable, where each slot has only one value.  For that
 * reason "Registers" makes more sense, the reference being to CPU registers.
 *
 * This could be used for anything, but it's been built for a pretty specific phing
 * need, and that is to allow access to dynamic values that are set by logic
 * that is not represented in a build file.  For exampe, we need a system for getting
 * the current resource (file) that is being processed by a filterchain in a fileset.
 * 
 * Each slot corresponds to only one read-only, dynamic-value RegisterSlot object. In
 * a build.xml register slots are expressed using a syntax similar to variables:
 * 
 * <replaceregexp>
 *    <regexp pattern="\n" replace="%{task.current_file}"/>
 * </replaceregexp>
 * 
 * The task/type must provide a supporting setter for the attribute:
 * 
 * <code>
 *     function setListeningReplace(RegisterSlot $slot) {
 *        $this->replace = $slot;
 *  }
 *
 *  // in main()
 *  if ($this->replace instanceof RegisterSlot) {
 *        $this->regexp->setReplace($this->replace->getValue());
 *  } else {
 *        $this->regexp->setReplace($this->replace);
 *  }
 * </code>
 *
 * @author Hans Lellelid <hans@xmpl.org>
 * @version $Revision: 1.3 $
 * @package phing.system.util
 */
class Register {
    
    /** Slots that have been registered */
    private static $slots = array();
    
    /**
     * Returns RegisterSlot for specified key.
     * 
     * If not slot exists a new one is created for key.
     * 
     * @param string $key
     * @return RegisterSlot
     */
    public static function getSlot($key) {
        if (!isset(self::$slots[$key])) {
            self::$slots[$key] = new RegisterSlot($key);
        }
        return self::$slots[$key];
    }    
}


/**
 * Represents a slot in the register.
 */
class RegisterSlot {
    
    /** The name of this slot. */
    private $key;
    
    /** The value for this slot. */
    private $value;
    
    /**
     * Constructs a new RegisterSlot, setting the key to passed param.
     * @param string $key
     */
    public function __construct($key) {
        $this->key = (string) $key;
    }
    
    /**
     * Sets the key / name for this slot.
     * @param string $k
     */
    public function setKey($k) {
        $this->key = (string) $k;
    }

    /**
     * Gets the key / name for this slot.
     * @return string
     */
    public function getKey() {
        return $this->key;
    }
    
    /**
     * Sets the value for this slot.
     * @param mixed
     */
    public function setValue($v) {
        $this->value = $v;
    }
    
    /**
     * Returns the value at this slot.
     * @return mixed
     */
    public function getValue() {
        return $this->value;
    }
    
}
?>