| Web Commons | ||||
|
||||
| Coverage | ||||||||||||
| Classes | Functions / Methods | Lines | ||||||||||
| Total |
|
100.00% | 3 / 3 |
|
100.00% | 12 / 12 |
|
100.00% | 40 / 40 | |||
| SlotHolder |
|
100.00% | 1 / 1 |
|
100.00% | 10 / 10 |
|
100.00% | 31 / 31 | |||
| public function add($name, $value) |
|
100.00% | 1 / 1 |
|
100.00% | 3 / 3 | ||||||
| public function isOpen() |
|
100.00% | 1 / 1 |
|
100.00% | 1 / 1 | ||||||
| public function crashIfExists($name) |
|
100.00% | 1 / 1 |
|
100.00% | 4 / 4 | ||||||
| public function crashIfOpen() |
|
100.00% | 1 / 1 |
|
100.00% | 5 / 5 | ||||||
| public function open($name) |
|
100.00% | 1 / 1 |
|
100.00% | 4 / 4 | ||||||
| public function close() |
|
100.00% | 1 / 1 |
|
100.00% | 5 / 5 | ||||||
| private function terminateBuffer() |
|
100.00% | 1 / 1 |
|
100.00% | 3 / 3 | ||||||
| public function getOrDefault($name, $default) |
|
100.00% | 1 / 1 |
|
100.00% | 1 / 1 | ||||||
| public function getOrCrash($name) |
|
100.00% | 1 / 1 |
|
100.00% | 4 / 4 | ||||||
| public function exist($name) |
|
100.00% | 1 / 1 |
|
100.00% | 1 / 1 | ||||||
| FileBasedTemplate |
|
100.00% | 1 / 1 |
|
100.00% | 2 / 2 |
|
100.00% | 9 / 9 | |||
| public function __construct($filename) |
|
100.00% | 1 / 1 |
|
100.00% | 5 / 5 | ||||||
| public function render($vars, SlotHolder $slots=Null) |
|
100.00% | 1 / 1 |
|
100.00% | 4 / 4 | ||||||
| SlotException |
|
100.00% | 1 / 1 |
|
|
100.00% | 0 / 0 | |||||
1 : <?php 2 : /** 3 : * This file groups classes pertaining to the "view" part of MVC. 4 : * 5 : * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org> 6 : * @license http://www.opensource.org/licenses/mit-license.php 7 : * @version 0.1dev 8 : */ 9 : 10 : /** 11 : * Holds several slots. 12 : * A slot is a cross-template variable, like a block, a title or even an object. 13 : * 14 : * @since 0.1 15 : */ 16 : class SlotHolder { 17 : /** 18 : * Contains slots indexed by names. 19 : * 20 : * @var array 21 : */ 22 : private $slots = array(); 23 : 24 : /** 25 : * The name of the slot that has been started with 26 : * {@link SlotHolder::start()}. Null if no slot is open. 27 : * 28 : * @var string 29 : */ 30 : private $open = Null; 31 : 32 : /** 33 : * Adds a slot. 34 : * Slot $name must not exist. 35 : * 36 : * @param string $name name for this slot 37 : * @param mixed $value value for this slot 38 : * @since 0.1 39 : */ 40 : public function add($name, $value) { 41 6 : $this->crashIfExists($name); 42 : 43 5 : $this->slots[$name] = $value; 44 5 : } 45 : 46 : /** 47 : * Returns True if a slot is opened. 48 : * 49 : * @return bool 50 : * @since 0.1 51 : */ 52 : public function isOpen() { 53 10 : return $this->open !== Null; 54 : } 55 : 56 : /** 57 : * Throws a SlotException if slot $name exists. 58 : * 59 : * @throws SlotException 60 : * @since 0.1 61 : */ 62 : public function crashIfExists($name) { 63 6 : if ($this->exist($name)) { 64 2 : throw new SlotException( 65 2 : 'Caught attempt to duplicate slot "' . $name . '"'); 66 : } 67 5 : } 68 : 69 : /** 70 : * If a slot is open, terminates the output buffer and throws a 71 : * SlotException. 72 : * 73 : * @throws SlotException 74 : * @since 0.1 75 : */ 76 : public function crashIfOpen() { 77 3 : if ($this->isOpen()) { 78 1 : $this->terminateBuffer(); 79 1 : throw new SlotException( 80 1 : 'Slot "' . $this->open . '" has not been closed.'); 81 : } 82 3 : } 83 : 84 : /** 85 : * Opens a slot. Its contents will be the text printed until a call to 86 : * {@link SlotHolder::close()} is issued. 87 : * No slot must be opened, and slot $name must not exist. 88 : * 89 : * @param string $name the name of this slot 90 : * @since 0.1 91 : */ 92 : public function open($name) { 93 3 : $this->crashIfOpen(); 94 : 95 3 : $this->open = $name; 96 3 : ob_start(); 97 3 : } 98 : 99 : /** 100 : * Stores the previously opened slot. Throws a SlotException if no slot is 101 : * open. 102 : * 103 : * @throws SlotException 104 : * @since 0.1 105 : */ 106 : public function close() { 107 3 : if (!$this->isOpen()) { 108 1 : throw new SlotException('No slot is open.'); 109 : } 110 : 111 2 : $this->slots[$this->open] = ob_get_contents(); 112 2 : $this->terminateBuffer(); 113 2 : } 114 : 115 : /** 116 : * Terminates the buffer and resets $open to Null. 117 : * This must not be called unless a buffer has been opened by this template. 118 : */ 119 : private function terminateBuffer() { 120 3 : ob_end_clean(); 121 3 : $this->open = Null; 122 3 : } 123 : 124 : /** 125 : * Returns the value of slot $name if it exists, 126 : * the default value otherwise. 127 : * 128 : * @param string $name 129 : * @param mixed $default 130 : * @return mixed 131 : * @since 0.1 132 : */ 133 : public function getOrDefault($name, $default) { 134 2 : return array_get_default($this->slots, $name, $default); 135 : } 136 : 137 : /** 138 : * Returns the slot $name. 139 : * Throws a SlotException if this slot does not exist. 140 : * 141 : * @param string $name 142 : * @throws SlotException 143 : * @return mixed 144 : * @since 0.1 145 : */ 146 : public function getOrCrash($name) { 147 5 : if (array_key_exists($name, $this->slots)) { 148 4 : return $this->slots[$name]; 149 : } 150 : else { 151 1 : throw new SlotException( 152 1 : 'Required slot "' . $name . '" is missing.'); 153 : } 154 : } 155 : 156 : /** 157 : * Returns True if slot $name exists, False otherwise. 158 : * 159 : * @param string $name 160 : * @return bool 161 : * @since 0.1 162 : */ 163 : public function exist($name) { 164 6 : return array_key_exists($name, $this->slots) || $this->open === $name; 165 : } 166 : } 167 : 168 : /** 169 : * Renders an associative array. 170 : * A template can be used as many times as needed. 171 : * 172 : * @since 0.1 173 : */ 174 : interface Template { 175 : /** 176 : * Renders the given object/value. 177 : * $slots is injected in the template as '$slots'. 178 : * 179 : * @param mixed $object 180 : * @since 0.1 181 : */ 182 : function render($vars, SlotHolder $slots=Null); 183 : } 184 : 185 : /** 186 : * Represents a template described by a PHP/HTML file. 187 : * 188 : * @since 0.1 189 : */ 190 : class FileBasedTemplate implements Template { 191 : private $filename; 192 : 193 : /** 194 : * @param string $filename path to template file 195 : */ 196 : public function __construct($filename) { 197 4 : if (is_file($filename)) { 198 4 : $this->filename = $filename; 199 4 : } 200 : else { 201 1 : throw new InvalidArgumentException('No such template: '. $filename); 202 : } 203 4 : } 204 : 205 : /** 206 : * (non-PHPdoc) 207 : * @see WebCommons/Template::render() 208 : */ 209 : public function render($vars, SlotHolder $slots=Null) { 210 3 : ob_start(); 211 3 : extract($vars); 212 3 : include($this->filename); 213 3 : return ob_get_clean(); 214 : } 215 : } 216 : 217 : /** 218 : * Exception thrown when an errors occurs with slots. 219 : * 220 : * @since 0.1 221 : */ 222 : class SlotException extends Exception {} |
| Generated by PHPUnit 3.4.5 and Xdebug 2.1.0 at Fri Jul 16 0:48:38 CEST 2010. |