00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 require_once
PT_IP .
"/Types/OString.php";
00025 require_once
PT_IP .
"/Types/OHash.php";
00026
00035 define('_TYPES_CODE_EXTRACT_CODE_CONTEXT_',
00036 '
00037
if (is_array($__context__)) { extract($__context__); }
00038
if (is_object($__context__)) { extract($__context__->toHash()); }
00039
00040 ');
00041
00079
class Code
00080 {
00081 var
$_code;
00082 var
$_function;
00083 var
$_compiled =
false;
00084
00091 function
Code($code=
false)
00092 {
00093 $this->
__construct($code);
00094 }
00095
00096 function
__construct($code=
false)
00097 {
00098
if (!$code) {
00099
return $this;
00100 }
00101
if (is_object($code) && get_class($code) == 'code') {
00102 $this->_code = $code->getCode();
00103 }
else {
00104 $this->
setCode(Types::toString($code));
00105 }
00106 }
00107
00118 function &
execute()
00119 {
00120
if (!$this->_compiled) {
00121 $err =& $this->
compile();
00122
if (PEAR::isError($err)) {
00123
return $err;
00124 }
00125 }
00126 $argv = func_get_args();
00127 $argc = func_num_args();
00128
switch ($argc) {
00129
case 1:
00130 $context = $argv[0];
00131
break;
00132
default:
00133 $context =
OHash::ArrayToHash($argv);
00134
break;
00135 }
00136 $func = $this->_function;
00137
return $func($context);
00138 }
00139
00147 function
compile()
00148 {
00149 ob_start();
00150 $this->_function = create_function('$__context__', $this->_code);
00151 $ret = ob_get_contents();
00152 ob_end_clean();
00153
00154
if (!$this->_function) {
00155
return PEAR::raiseError($ret);
00156 }
00157
00158 $this->_compiled =
true;
00159 }
00160
00167 function
setCode($str)
00168 {
00169
00170 $str =
_TYPES_CODE_EXTRACT_CODE_CONTEXT_ . $str;
00171 $this->_code = $str;
00172 }
00173
00179 function
getCode()
00180 {
00181
return $this->_code;
00182 }
00183
00189 function
__sleep()
00190 {
00191
return array(
"_code");
00192 }
00193
00199 function
toString()
00200 {
00201
if ($this->_compiled) {
00202
return '<Code \''.$this->_function.
'\'>';
00203 }
else {
00204
return '<Code \'not compiled\
'>';
00205 }
00206 }
00207 }
00208
00215
class CodeError extends PEAR_Error
00216 {
00217 }
00218
00219 ?>