/
Buffer.class.php
134 lines (124 loc) · 3.37 KB
/
Buffer.class.php
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
<?php
Library::import('recess.framework.helpers.blocks.Block');
Library::import('recess.framework.helpers.blocks.HtmlBlock');
Library::import('recess.framework.helpers.blocks.ListBlock');
Library::import('recess.framework.AbstractHelper');
/**
* Buffer is a helper class that acts as a factory for
* HtmlBlocks. Buffer and blocks are often used in conjunction
* with layouts as an easy mechanism for transferring chunks of
* HTML from a child template to a parent Layout.
*
* Buffer can be used to fill unempty HtmlBlocks,
* overwrite HtmlBlocks, or append/prepend to them. Here are some
* example usages:
*
* Buffer::to($block);
* echo 'hello world';
* Buffer::end();
* // $block is now an HtmlBlock with contents 'hello world'
*
* Buffer::append($block);
* echo '!<br />';
* Buffer::end();
* // $block is now an HtmlBlock with contents 'hello world!<br />'
*
* Buffer::to($block);
* print_r($block);
* Block::end();
* // $block is still 'hello world!<br />'
*
* Buffer::to($block, Buffer::OVERWRITE);
* echo 'overwritten';
* Buffer::end();
* // $block is now 'overwritten'
*
* echo $block;
* // overwritten
*
* @author Kris Jordan
*/
abstract class Buffer extends AbstractHelper {
const NORMAL = 0;
const OVERWRITE = 1;
const APPEND = 2;
const PREPEND = 3;
/** STATIC MEMBERS **/
private static $bufferBlocks = array();
private static $bufferModes = array();
/**
* Begin output buffering to the block passed by reference. If the
* reference is set to Null a new HtmlBlock will be assigned to the
* reference.
*
* @param HtmlBlock or Null - The block the buffer will fill.
* @param int Optional - mode used to fill block.
*/
public static function to(&$block, $mode = self::NORMAL) {
self::modalStart($block, $mode);
}
/**
* Buffer will append to the provided HtmlBlock. If null this will
* create a new block, not fail.
*
* @param HtmlBlock The block to append to.
*/
public static function appendTo(&$block) {
self::modalStart($block, self::APPEND);
}
/**
* Buffer will append to the provided HtmlBlock. If null this will
* create a new block, not fail.
*
* @param HtmlBlock The block to append to.
*/
public static function prependTo(&$block) {
self::modalStart($block, self::PREPEND);
}
/**
* Internal helper method for starting a new buffer.
*
* @param HtmlBlock The block to append to.
*/
private static function modalStart(&$block, $mode) {
if($block === null) {
$block = new HtmlBlock();
}
array_push(self::$bufferBlocks, $block);
array_push(self::$bufferModes, $mode);
ob_start();
}
/**
* End the output buffer, clear contents, and assign contents
* to the block passed by reference to start the buffer. Also
* returns the block.
*
* @return The final block.
*/
public static function end() {
if(empty(self::$bufferBlocks)) {
throw new RecessFrameworkException('Buffer ended without corresponding Buffer::to($block).', 2);
}
$buffer = ob_get_clean();
$mode = array_pop(self::$bufferModes);
$block = array_pop(self::$bufferBlocks);
switch($mode) {
case self::NORMAL:
if((string)$block === '') {
$block->set($buffer);
}
break;
case self::OVERWRITE:
$block->set($buffer);
break;
case self::APPEND:
$block->append($buffer);
break;
case self::PREPEND:
$block->prepend($buffer);
break;
}
return $block;
}
}
?>