/
DisplayObject.php
185 lines (156 loc) · 4.28 KB
/
DisplayObject.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* DisplayObject
*
* Copyright (C) 2009 Adam Venturella
*
* LICENSE:
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Adam Venturella - aventurella@gmail.com
*
* @package CouchDB_PHP_Proxy
* @author Adam Venturella <aventurella@gmail.com>
* @copyright Copyright (C) 2009 Adam Venturella
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
*
*
**/
class DisplayObject
{
public $context;
protected $url;
protected $source;
/* Convenience Methods */
public static function initWithUrlAndContext($url, $context)
{
$instance = new DisplayObject();
$instance->url = $url;
$instance->context = $context;
return $instance;
}
public static function initWithUrl($url)
{
$instance = new DisplayObject();
$instance->url = $url;
return $instance;
}
public static function initWithString($string)
{
$instance = new DisplayObject();
$instance->source = $string;
return $instance;
}
public function render()
{
if($this->source)
{
$output = $this->source;
foreach($this->context as $key => $value){
$this->applyPropertyToView($key, $value, $output);
}
return $output;
}
else
{
return $this->renderWithContext();
}
}
protected function renderWithContext()
{
if(!in_array("displayObjectRenderer", stream_get_filters()))
stream_filter_register("displayObjectRenderer", "DisplayObjectRenderer");
$pointer = fopen($this->url, "r");
if($this->context)
stream_filter_append($pointer, "displayObjectRenderer", STREAM_FILTER_READ, $this->context);
else
stream_filter_append($pointer, "displayObjectRenderer", STREAM_FILTER_READ);
return stream_get_contents($pointer);
}
protected function applyPropertyToView($name, $value, &$component)
{
$component = str_replace("{".$name."}", $value, $component);
}
public function __toString()
{
return $this->render();
}
}
class DisplayObjectRenderer extends php_user_filter
{
const kTempStreamMemoryLimit = 10240; // 10 * 1024 = 10k
public function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in))
{
if(count($this->params))
{
foreach($this->params as $key => $value)
{
if(is_object($value))
{
$outValue = $value->__toString();
}
else if(!empty($value) && is_array($value))
{
/*
We open a temp stream here, because if the array that comes in maps to a template that is
HUGE then we will quickily run out of system resources by $outValue .= $item->__toString()
as $outValue will be storing the output of another DisplayObject of arbitrary size.
*/
$stream_temp = fopen("php://temp/maxmemory:".DisplayObjectRenderer::kTempStreamMemoryLimit, 'r+');
foreach($value as $item)
{
if(is_object($item))
{
fwrite($stream_temp, $item->__toString());
}
}
}
else
{
$outValue = $value;
}
if($outValue)
{
if(strpos($bucket->data, '{'.$key.'}') !== false){
$bucket->data = str_replace('{'.$key.'}', $outValue, $bucket->data);
}
$outValue = null;
}
if(isset($stream_temp) && ftell($stream_temp))
{
rewind($stream_temp);
if(strpos($bucket->data, '{'.$key.'}') !== false){
$bucket->data = str_replace('{'.$key.'}', stream_get_contents($stream_temp), $bucket->data);
}
fclose($stream_temp);
}
}
}
$this->cleanup($bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
private function cleanup(&$data)
{
$data = preg_replace('/\{[\d\w]+\}/', '', $data);
}
public function onCreate()
{
return true;
}
}
?>