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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| /**
* Holds several slots.
* A slot is a cross-template variable, like a block, a title or even an object.
*
* @since 0.1
*/
class SlotHolder {
/**
* Contains slots indexed by names.
*
* @var array
*/
private $slots = array();
/**
* The name of the slot that has been started with
* {@link SlotHolder::start()}. Null if no slot is open.
*
* @var string
*/
private $open = Null;
/**
* Adds a slot.
* Slot $name must not exist.
*
* @param string $name name for this slot
* @param mixed $value value for this slot
* @since 0.1
*/
public function add($name, $value) {
$this->crashIfExists($name);
$this->slots[$name] = $value;
}
/**
* Returns True if a slot is opened.
*
* @return bool
* @since 0.1
*/
public function isOpen() {
return $this->open !== Null;
}
/**
* Throws a SlotException if slot $name exists.
*
* @throws SlotException
* @since 0.1
*/
public function crashIfExists($name) {
if ($this->exist($name)) {
throw new SlotException(
'Caught attempt to duplicate slot "' . $name . '"');
}
}
/**
* If a slot is open, terminates the output buffer and throws a
* SlotException.
*
* @throws SlotException
* @since 0.1
*/
public function crashIfOpen() {
if ($this->isOpen()) {
$this->terminateBuffer();
throw new SlotException(
'Slot "' . $this->open . '" has not been closed.');
}
}
/**
* Opens a slot. Its contents will be the text printed until a call to
* {@link SlotHolder::close()} is issued.
* No slot must be opened, and slot $name must not exist.
*
* @param string $name the name of this slot
* @since 0.1
*/
public function open($name) {
$this->crashIfOpen();
$this->open = $name;
ob_start();
}
/**
* Stores the previously opened slot. Throws a SlotException if no slot is
* open.
*
* @throws SlotException
* @since 0.1
*/
public function close() {
if (!$this->isOpen()) {
throw new SlotException('No slot is open.');
}
$this->add($this->open, ob_get_contents());
$this->terminateBuffer();
}
/**
* Terminates the buffer and resets $open to Null.
* This must not be called unless a buffer has been opened by this template.
*/
private function terminateBuffer() {
ob_end_clean();
$this->open = Null;
}
/**
* Returns the value of slot $name if it exists,
* the default value otherwise.
*
* @param string $name
* @param mixed $default
* @return mixed
* @since 0.1
*/
public function getOrDefault($name, $default) {
return array_get_default($this->slots, $name, $default);
}
/**
* Returns the slot $name.
* Throws a SlotException if this slot does not exist.
*
* @param string $name
* @throws SlotException
* @return mixed
* @since 0.1
*/
public function getOrCrash($name) {
if (array_key_exists($name, $this->slots)) {
return $this->slots[$name];
}
else {
throw new SlotException(
'Required slot "' . $name . '" is missing.');
}
}
/**
* Returns True if slot $name exists, False otherwise.
*
* @param string $name
* @return bool
* @since 0.1
*/
public function exist($name) {
return array_key_exists($name, $this->slots) || $this->open === $name;
}
} |
Partager