1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class SnippetHelper extends Object
22: {
23:
24: public static $outputAllowed = TRUE;
25:
26:
27: private $id;
28:
29:
30: private $tag;
31:
32:
33: private $payload;
34:
35:
36: private $level;
37:
38:
39:
40: 41: 42: 43: 44: 45: 46:
47: public static function create(Control $control, $name = NULL, $tag = 'div')
48: {
49: if (self::$outputAllowed) { 50: $obj = new self;
51: $obj->tag = trim($tag, '<>');
52: if ($obj->tag) echo '<', $obj->tag, ' id="', $control->getSnippetId($name), '">';
53: return $obj; 54:
55: } elseif ($control->isControlInvalid($name)) { 56: $obj = new self;
57: $obj->id = $control->getSnippetId($name);
58: $obj->payload = $control->getPresenter()->getPayload();
59: ob_start();
60: $obj->level = ob_get_level();
61: self::$outputAllowed = TRUE;
62: return $obj;
63:
64: } else {
65: return FALSE;
66: }
67: }
68:
69:
70:
71: 72: 73: 74:
75: public function finish()
76: {
77: if ($this->tag !== NULL) { 78: if ($this->tag) echo "</$this->tag>";
79:
80: } else { 81: if ($this->level !== ob_get_level()) {
82: throw new InvalidStateException("Snippet '$this->id' cannot be ended here.");
83: }
84: $this->payload->snippets[$this->id] = ob_get_clean();
85: self::$outputAllowed = FALSE;
86: }
87: }
88:
89: }
90: