Register.php 2.8 KB

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