| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | /* |
| 4 | 4 | * This file is part of Mustache.php. |
| 5 | 5 | * |
| 6 | - * (c) 2012 Justin Hileman | |
| 6 | + * (c) 2010-2017 Justin Hileman | |
| 7 | 7 | * |
| 8 | 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | 9 | * file that was distributed with this source code. |
| 10 | 10 | */ |
| @@ -13,9 +13,10 @@ | ||
| 13 | 13 | * Mustache Template rendering Context. |
| 14 | 14 | */ |
| 15 | 15 | class Mustache_Context |
| 16 | 16 | { |
| 17 | - private $stack = array(); | |
| 17 | + private $stack = array(); | |
| 18 | + private $blockStack = array(); | |
| 18 | 19 | |
| 19 | 20 | /** |
| 20 | 21 | * Mustache rendering Context constructor. |
| 21 | 22 | * |
| @@ -38,8 +39,18 @@ | ||
| 38 | 39 | array_push($this->stack, $value); |
| 39 | 40 | } |
| 40 | 41 | |
| 41 | 42 | /** |
| 43 | + * Push a new Context frame onto the block context stack. | |
| 44 | + * | |
| 45 | + * @param mixed $value Object or array to use for block context | |
| 46 | + */ | |
| 47 | + public function pushBlockContext($value) | |
| 48 | + { | |
| 49 | + array_push($this->blockStack, $value); | |
| 50 | + } | |
| 51 | + | |
| 52 | + /** | |
| 42 | 53 | * Pop the last Context frame from the stack. |
| 43 | 54 | * |
| 44 | 55 | * @return mixed Last Context frame (object or array) |
| 45 | 56 | */ |
| @@ -48,8 +59,18 @@ | ||
| 48 | 59 | return array_pop($this->stack); |
| 49 | 60 | } |
| 50 | 61 | |
| 51 | 62 | /** |
| 63 | + * Pop the last block Context frame from the stack. | |
| 64 | + * | |
| 65 | + * @return mixed Last block Context frame (object or array) | |
| 66 | + */ | |
| 67 | + public function popBlockContext() | |
| 68 | + { | |
| 69 | + return array_pop($this->blockStack); | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 52 | 73 | * Get the last Context frame. |
| 53 | 74 | * |
| 54 | 75 | * @return mixed Last Context frame (object or array) |
| 55 | 76 | */ |
| @@ -120,8 +141,62 @@ | ||
| 120 | 141 | return $value; |
| 121 | 142 | } |
| 122 | 143 | |
| 123 | 144 | /** |
| 145 | + * Find an 'anchored dot notation' variable in the Context stack. | |
| 146 | + * | |
| 147 | + * This is the same as findDot(), except it looks in the top of the context | |
| 148 | + * stack for the first value, rather than searching the whole context stack | |
| 149 | + * and starting from there. | |
| 150 | + * | |
| 151 | + * @see Mustache_Context::findDot | |
| 152 | + * | |
| 153 | + * @throws Mustache_Exception_InvalidArgumentException if given an invalid anchored dot $id | |
| 154 | + * | |
| 155 | + * @param string $id Dotted variable selector | |
| 156 | + * | |
| 157 | + * @return mixed Variable value, or '' if not found | |
| 158 | + */ | |
| 159 | + public function findAnchoredDot($id) | |
| 160 | + { | |
| 161 | + $chunks = explode('.', $id); | |
| 162 | + $first = array_shift($chunks); | |
| 163 | + if ($first !== '') { | |
| 164 | + throw new Mustache_Exception_InvalidArgumentException(sprintf('Unexpected id for findAnchoredDot: %s', $id)); | |
| 165 | + } | |
| 166 | + | |
| 167 | + $value = $this->last(); | |
| 168 | + | |
| 169 | + foreach ($chunks as $chunk) { | |
| 170 | + if ($value === '') { | |
| 171 | + return $value; | |
| 172 | + } | |
| 173 | + | |
| 174 | + $value = $this->findVariableInStack($chunk, array($value)); | |
| 175 | + } | |
| 176 | + | |
| 177 | + return $value; | |
| 178 | + } | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * Find an argument in the block context stack. | |
| 182 | + * | |
| 183 | + * @param string $id | |
| 184 | + * | |
| 185 | + * @return mixed Variable value, or '' if not found | |
| 186 | + */ | |
| 187 | + public function findInBlock($id) | |
| 188 | + { | |
| 189 | + foreach ($this->blockStack as $context) { | |
| 190 | + if (array_key_exists($id, $context)) { | |
| 191 | + return $context[$id]; | |
| 192 | + } | |
| 193 | + } | |
| 194 | + | |
| 195 | + return ''; | |
| 196 | + } | |
| 197 | + | |
| 198 | + /** | |
| 124 | 199 | * Helper function to find a variable in the Context stack. |
| 125 | 200 | * |
| 126 | 201 | * @see Mustache_Context::find |
| 127 | 202 | * |
| @@ -132,16 +207,34 @@ | ||
| 132 | 207 | */ |
| 133 | 208 | private function findVariableInStack($id, array $stack) |
| 134 | 209 | { |
| 135 | 210 | for ($i = count($stack) - 1; $i >= 0; $i--) { |
| 136 | - if (is_object($stack[$i]) && !$stack[$i] instanceof Closure) { | |
| 137 | - if (method_exists($stack[$i], $id)) { | |
| 138 | - return $stack[$i]->$id(); | |
| 139 | - } elseif (isset($stack[$i]->$id)) { | |
| 140 | - return $stack[$i]->$id; | |
| 141 | - } | |
| 142 | - } elseif (is_array($stack[$i]) && array_key_exists($id, $stack[$i])) { | |
| 143 | - return $stack[$i][$id]; | |
| 211 | + $frame = &$stack[$i]; | |
| 212 | + | |
| 213 | + switch (gettype($frame)) { | |
| 214 | + case 'object': | |
| 215 | + if (!($frame instanceof Closure)) { | |
| 216 | + // Note that is_callable() *will not work here* | |
| 217 | + // See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods | |
| 218 | + if (method_exists($frame, $id)) { | |
| 219 | + return $frame->$id(); | |
| 220 | + } | |
| 221 | + | |
| 222 | + if (isset($frame->$id)) { | |
| 223 | + return $frame->$id; | |
| 224 | + } | |
| 225 | + | |
| 226 | + if ($frame instanceof ArrayAccess && isset($frame[$id])) { | |
| 227 | + return $frame[$id]; | |
| 228 | + } | |
| 229 | + } | |
| 230 | + break; | |
| 231 | + | |
| 232 | + case 'array': | |
| 233 | + if (array_key_exists($id, $frame)) { | |
| 234 | + return $frame[$id]; | |
| 235 | + } | |
| 236 | + break; | |
| 144 | 237 | } |
| 145 | 238 | } |
| 146 | 239 | |
| 147 | 240 | return ''; |