Skip to content

Commit b48a0b7

Browse files
committed
Clean-up output buffering
1 parent d611c4d commit b48a0b7

16 files changed

+121
-82
lines changed

_experimental/outcontrol/flush.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ function flush () {
44
// * example 1: flush();
55
// * returns 1: undefined
66

7+
this.php_js = this.phpjs || {};
8+
var phpjs = this.php_js, obs = phpjs.obs;
9+
710
// Not distinct from ob_flush() in JavaScript, since not sending to a browser
8-
if (!this.php_js || !this.php_js.obs || !this.php_js.obs.length) {
11+
if (!obs || !obs.length) {
912
return;
1013
}
11-
this.echo(this.php_js.obs[this.php_js.obs.length-1].buffer);
12-
this.php_js.obs[this.php_js.obs.length-1].buffer = '';
14+
this.echo(obs[obs.length-1].buffer);
15+
obs[obs.length-1].buffer = '';
1316
}

_experimental/outcontrol/ob_clean.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ function ob_clean () {
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// * example 1: ob_clean();
55
// * returns 1: undefined
6-
if (!this.php_js || !this.php_js.obs || !this.php_js.obs.length) {
6+
7+
this.php_js = this.phpjs || {};
8+
var phpjs = this.php_js, obs = phpjs.obs;
9+
10+
if (!obs || !obs.length) {
711
return;
812
}
9-
this.php_js.obs[this.php_js.obs.length-1].buffer = '';
13+
obs[obs.length-1].buffer = '';
1014
}

_experimental/outcontrol/ob_end_clean.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ function ob_end_clean () {
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// * example 1: ob_end_clean();
55
// * returns 1: true
6-
if (!this.php_js || !this.php_js.obs || !this.php_js.obs.length) {
6+
7+
this.php_js = this.phpjs || {};
8+
var phpjs = this.php_js, obs = phpjs.obs;
9+
10+
if (!obs || !obs.length) {
711
return false;
812
}
9-
this.php_js.obs.pop();
13+
obs.pop();
1014
return true;
1115
}

_experimental/outcontrol/ob_end_flush.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ function ob_end_flush () {
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// * example 1: ob_end_flush();
55
// * returns 1: true
6-
7-
if (!this.php_js || !this.php_js.obs || !this.php_js.obs.length) {
6+
7+
this.php_js = this.phpjs || {};
8+
var obs = this.php_js.obs;
9+
10+
if (!obs || !obs.length) {
811
return false;
912
}
10-
var contents = this.php_js.obs[this.php_js.obs.length-1].buffer;
11-
this.php_js.obs.pop();
13+
var contents = obs[obs.length-1].buffer;
14+
obs.pop();
1215
this.echo(contents);
1316

1417
return true;

_experimental/outcontrol/ob_flush.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ function ob_flush () {
44
// * example 1: ob_flush();
55
// * returns 1: undefined
66

7-
if (!this.php_js || !this.php_js.obs || !this.php_js.obs.length) {
7+
this.php_js = this.phpjs || {};
8+
var phpjs = this.php_js, obs = phpjs.obs;
9+
10+
if (!obs || !obs.length) {
811
return;
912
}
10-
this.echo(this.php_js.obs[this.php_js.obs.length-1].buffer);
11-
this.php_js.obs[this.php_js.obs.length-1].buffer = '';
13+
this.echo(obs[obs.length-1].buffer);
14+
obs[obs.length-1].buffer = '';
1215
}

_experimental/outcontrol/ob_get_clean.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ function ob_get_clean () {
44
// * example 1: ob_get_clean();
55
// * returns 1: 'some buffer contents'
66
var buffer = '';
7-
if (!this.php_js || !this.php_js.obs || !this.php_js.obs.length) {
7+
this.php_js = this.phpjs || {};
8+
var phpjs = this.php_js, obs = phpjs.obs;
9+
if (!obs || !obs.length) {
810
return false;
911
}
10-
buffer = this.php_js.obs[this.php_js.obs.length-1].buffer;
11-
this.php_js.obs.pop();
12+
buffer = obs[obs.length-1].buffer;
13+
obs.pop();
1214
return buffer;
1315
}

_experimental/outcontrol/ob_get_contents.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ function ob_get_contents () {
44
// * example 1: ob_get_contents();
55
// * returns 1: 'some buffer contents'
66

7-
var phpjs = this.php_js;
8-
if (!phpjs || !phpjs.obs || !phpjs.obs.length) {
9-
if (phpjs.ini && phpjs.ini.output_buffering &&
10-
(typeof phpjs.ini.output_buffering.local_value !== 'string' ||
11-
phpjs.ini.output_buffering.local_value.toLowerCase() !== 'off')) {
12-
return ''; // If output was already buffered, it would be available in phpjs.obs
13-
}
14-
return false;
7+
this.php_js = this.phpjs || {};
8+
var phpjs = this.php_js, ini = phpjs.ini, obs = phpjs.obs;
9+
if (!obs || !obs.length) {
10+
return (ini && ini.output_buffering &&
11+
(typeof ini.output_buffering.local_value !== 'string' ||
12+
ini.output_buffering.local_value.toLowerCase() !== 'off')) ? '' : false; // If output was already buffered, it would be available in obs
1513
}
16-
return phpjs.obs[phpjs.obs.length-1].buffer; // Retrieve most recently added buffer contents
14+
return obs[obs.length-1].buffer; // Retrieve most recently added buffer contents
1715
}

_experimental/outcontrol/ob_get_flush.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ function ob_get_flush () {
55
// * returns 1: 'some buffer contents'
66

77
var buffer = '';
8-
if (!this.php_js || !this.php_js.obs || !this.php_js.obs.length) {
8+
this.php_js = this.phpjs || {};
9+
var phpjs = this.php_js, obs = phpjs.obs;
10+
if (!obs || !obs.length) {
911
return false;
1012
}
11-
buffer = this.php_js.obs[this.php_js.obs.length-1].buffer;
13+
buffer = obs[obs.length-1].buffer;
1214
this.echo(buffer);
13-
this.php_js.obs.pop();
15+
obs.pop();
1416
return buffer;
1517
}

_experimental/outcontrol/ob_get_length.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ function ob_get_length () {
44
// * example 1: ob_get_length();
55
// * returns 1: 155
66

7-
if (!this.php_js || !this.php_js.obs || !this.php_js.obs.length) {
8-
if (this.php_js.ini && this.php_js.ini['output_buffering'] && (typeof this.php_js.ini['output_buffering'].local_value !== 'string' || this.php_js.ini['output_buffering'].local_value.toLowerCase() !== 'off')) {
9-
return 0; // If output was already buffered, it would be available in this.php_js.obs
10-
}
11-
return false;
7+
this.php_js = this.phpjs || {};
8+
var phpjs = this.php_js, ini = phpjs.ini, obs = phpjs.obs;
9+
10+
if (!obs || !obs.length) {
11+
return (ini && ini['output_buffering'] &&
12+
(typeof ini['output_buffering'].local_value !== 'string' ||
13+
ini['output_buffering'].local_value.toLowerCase() !== 'off')) ? 0 : false; // If output was already buffered, it would be available in obs
1214
}
13-
return this.php_js.obs[this.php_js.obs.length-1].buffer.length; // Retrieve length of most recently added buffer contents
15+
return obs[obs.length-1].buffer.length; // Retrieve length of most recently added buffer contents
1416
}

_experimental/outcontrol/ob_get_level.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ function ob_get_level () {
44
// * example 1: ob_get_level();
55
// * returns 1: 1
66

7-
if (!this.php_js || !this.php_js.obs || !this.php_js.obs.length) {
8-
if (this.php_js.ini && this.php_js.ini['output_buffering'] && (typeof this.php_js.ini['output_buffering'].local_value !== 'string' || this.php_js.ini['output_buffering'].local_value.toLowerCase() !== 'off')) {
9-
return 1;
10-
}
11-
return 0;
7+
this.php_js = this.phpjs || {};
8+
var phpjs = this.php_js, ini = phpjs.ini, obs = phpjs.obs;
9+
10+
if (!obs || !obs.length) {
11+
return (ini && ini['output_buffering'] &&
12+
(typeof ini['output_buffering'].local_value !== 'string' ||
13+
ini['output_buffering'].local_value.toLowerCase() !== 'off')) ? 1 : 0;
1214
}
13-
return this.php_js.obs.length;
15+
return obs.length;
1416
}

_experimental/outcontrol/ob_get_status.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ function ob_get_status (full_status) {
44
// * example 1: ob_get_status(true);
55
// * returns 1: [{chunk_size:4096, name:myCallback, del:true, type:1,status:0}]
66

7-
var i=0, retObj = {}, obs={}, retArr=[], name = '';
7+
var i = 0, retObj = {}, ob = {}, retArr = [], name = '';
88
var getFuncName = function (fn) {
99
var name=(/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
1010
if (!name) {
1111
return '(Anonymous)';
1212
}
1313
return name[1];
1414
};
15-
if (!this.php_js || !this.php_js.obs || !this.php_js.obs.length) {
16-
if (this.php_js.ini && this.php_js.ini['output_buffering'] &&
17-
(typeof this.php_js.ini['output_buffering'].local_value !== 'string' ||
18-
this.php_js.ini['output_buffering'].local_value.toLowerCase() !== 'off')
15+
16+
this.php_js = this.phpjs || {};
17+
var phpjs = this.php_js, ini = phpjs.ini, obs = phpjs.obs;
18+
19+
if (!obs || !obs.length) {
20+
if (ini && ini.output_buffering &&
21+
(typeof ini.output_buffering.local_value !== 'string' ||
22+
ini.output_buffering.local_value.toLowerCase() !== 'off')
1923
) { // handler itself is stored in 'output_handler' ini
2024
retObj = {type:1, status:0, name:'default output handler', del:true};
2125
if (full_status) {
@@ -30,22 +34,25 @@ function ob_get_status (full_status) {
3034
return retArr;
3135
}
3236
if (full_status) {
33-
for (i=0; i < this.php_js.obs.length; i++) {
34-
obs = this.php_js.obs[i];
35-
name = obs.callback && getFuncName(obs.callback) ? (getFuncName(obs.callback) === 'URLRewriter' ? 'URL-Rewriter' : getFuncName(obs.callback)) : undefined;
36-
retObj = {chunk_size:obs.chunk_size, name:name, del:obs.erase, type:obs.type,status:obs.status};
37-
if (obs.size) {
38-
retObj.size = obs.size;
37+
for (i=0; i < obs.length; i++) {
38+
ob = obs[i];
39+
name = ob.callback && getFuncName(ob.callback) ?
40+
(getFuncName(ob.callback) === 'URLRewriter' ?
41+
'URL-Rewriter' :
42+
getFuncName(ob.callback)
43+
) : undefined;
44+
retObj = {chunk_size: ob.chunk_size, name: name, del: ob.erase, type: ob.type, status: ob.status};
45+
if (ob.size) {
46+
retObj.size = ob.size;
3947
}
40-
if (obs.block_size) {
41-
retObj.block_size = obs.block_size;
48+
if (ob.block_size) {
49+
retObj.block_size = ob.block_size;
4250
}
4351
retArr.push(retObj);
4452
}
4553
return retArr;
4654
}
47-
obs = this.php_js.obs[this.php_js.obs.length-1];
48-
name = getFuncName(obs.callback);
49-
retObj = {level:this.php_js.obs.length, name:name, del:obs.erase, type:obs.type,status:obs.status};
50-
return retObj;
55+
ob = obs[phpjs.obs.length-1];
56+
name = getFuncName(ob.callback);
57+
return {level: phpjs.obs.length, name: name, del: ob.erase, type: ob.type, status: ob.status};
5158
}

_experimental/outcontrol/ob_implicit_flush.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ function ob_implicit_flush () {
66
// BEGIN REDUNDANT
77
this.php_js = this.php_js || {};
88
// END REDUNDANT
9-
if (!this.php_js.ob_implicit_flush) {
10-
this.php_js.ob_implicit_flush = true;
11-
return;
12-
}
13-
this.php_js.ob_implicit_flush = !this.php_js.ob_implicit_flush;
9+
var phpjs = this.php_js;
10+
phpjs.ob_implicit_flush = !phpjs.ob_implicit_flush;
1411
}

_experimental/outcontrol/ob_list_handlers.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,28 @@ function ob_list_handlers () {
44
// * example 1: ob_list_handlers();
55
// * returns 1: ['default output handler', 'myOwnHandler']
66

7-
var i=0, arr=[], name='';
7+
var i = 0, arr = [], name='', cbname = '';
88
var getFuncName = function (fn) {
99
var name=(/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
1010
if (!name) {
1111
return '(Anonymous)';
1212
}
1313
return name[1];
1414
};
15-
var cbname=getFuncName(this.php_js.obs[i].callback);
15+
16+
this.php_js = this.phpjs || {};
17+
var phpjs = this.php_js, ini = phpjs.ini;
1618

17-
if (!this.php_js || !this.php_js.obs || !this.php_js.obs.length) {
18-
if (this.php_js.ini && this.php_js.ini['output_buffering'] && (typeof this.php_js.ini['output_buffering'].local_value !== 'string' || this.php_js.ini['output_buffering'].local_value.toLowerCase() !== 'off')) {
19+
if (!phpjs.obs || !phpjs.obs.length) {
20+
if (ini && ini['output_buffering'] &&
21+
(typeof ini['output_buffering'].local_value !== 'string' ||
22+
ini['output_buffering'].local_value.toLowerCase() !== 'off')) {
1923
return ['default output handler']; // PHP doesn't return output_handler ini, even if it is set
2024
}
2125
return arr;
2226
}
23-
for (i=0; i < this.php_js.obs.length; i++) {
27+
for (i=0; i < phpjs.obs.length; i++) {
28+
cbname = getFuncName(phpjs.obs[i].callback);
2429
name = cbname === '' ? 'default output handler' : cbname === 'URLRewriter' ? 'URL-Rewriter' : cbname;
2530
arr.push(name);
2631
}

_experimental/outcontrol/ob_start.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ function ob_start (output_callback, chunk_size, erase) {
88
erase = !(erase === false); // true is default
99
chunk_size = chunk_size === 1 ? 4096 : (chunk_size || 0);
1010

11-
this.php_js = phpjs || {};
12-
var phpjs = this.php_js;
13-
if (!phpjs.obs &&
14-
(phpjs.ini && phpjs.ini.output_buffering &&
15-
(typeof phpjs.ini.output_buffering.local_value !== 'string' ||
16-
phpjs.ini.output_buffering.local_value.toLowerCase() !== 'off')
11+
this.php_js = this.phpjs || {};
12+
this.php_js.obs = phpjs.obs || []; // Array for nestable buffers
13+
var phpjs = this.php_js, ini = phpjs.ini, obs = phpjs.obs;
14+
15+
if (!obs &&
16+
(ini && ini.output_buffering &&
17+
(typeof ini.output_buffering.local_value !== 'string' ||
18+
ini.output_buffering.local_value.toLowerCase() !== 'off')
1719
)
1820
) {
1921
extra = true; // We'll run another ob_start() below (recursion prevented)
2022
}
2123

22-
this.php_js.obs = phpjs.obs || []; // Array for nestable buffers
2324
if (typeof output_callback === 'string') {
2425
if (output_callback === 'URL-Rewriter') { // Any others?
2526
internalType = true;
@@ -42,7 +43,7 @@ function ob_start (output_callback, chunk_size, erase) {
4243
bufferObj.type = 0;
4344
}
4445

45-
this.php_js.obs.push(bufferObj);
46+
obs.push(bufferObj);
4647

4748
if (extra) {
4849
return this.ob_start(); // We have to start buffering ourselves if the preference is set (and no buffering on yet)

_experimental/outcontrol/output_add_rewrite_var.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ function output_add_rewrite_var (name, value) {
66
// * example 1: output_add_rewrite_var('var', 'value');
77
// * returns 1: true
88

9-
var handlers = [], handler='', startAgain=true;
9+
var handlers = [], handler = '', startAgain = true;
10+
11+
this.php_js = this.phpjs || {};
12+
var phpjs = this.php_js, obs = phpjs.obs;
1013

1114
handlers = this.ob_list_handlers();
1215

@@ -20,10 +23,10 @@ function output_add_rewrite_var (name, value) {
2023
this.ob_start('URL-Rewriter', 0, true);
2124
}
2225

23-
if (!this.php_js.obs[this.php_js.obs.length-1].vars) {
24-
this.php_js.obs[this.php_js.obs.length-1].vars = {};
26+
if (!obs[obs.length-1].vars) {
27+
obs[obs.length-1].vars = {};
2528
}
26-
this.php_js.obs[this.php_js.obs.length-1].vars[name] = value;
29+
obs[obs.length-1].vars[name] = value;
2730

2831
return true;
2932
}

_experimental/outcontrol/output_reset_rewrite_vars.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ function output_reset_rewrite_vars () {
66

77
// Fix: also need to deal with those set by session_start() (will add to same obs?)
88

9-
if (this.php_js && this.php_js.obs && this.php_js.obs.length && this.php_js.obs[this.php_js.obs.length-1].vars) {
10-
this.php_js.obs[this.php_js.obs.length-1].vars = {};
9+
this.php_js = this.phpjs || {};
10+
var phpjs = this.php_js, obs = phpjs.obs;
11+
12+
if (obs && obs.length && obs[obs.length-1].vars) {
13+
obs[obs.length-1].vars = {};
1114
}
1215
return true;
1316
}

0 commit comments

Comments
 (0)