Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

Code.php

Go to the documentation of this file.
00001 <?php 00002 /* vim: set expandtab tabstop=4 shiftwidth=4: */ 00003 // 00004 // Copyright (c) 2003 Laurent Bedubourg 00005 // 00006 // This library is free software; you can redistribute it and/or 00007 // modify it under the terms of the GNU Lesser General Public 00008 // License as published by the Free Software Foundation; either 00009 // version 2.1 of the License, or (at your option) any later version. 00010 // 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 // Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public 00017 // License along with this library; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 // 00020 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr> 00021 // 00022 // $id$ 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 '// BEGIN EXTRACT __context__ 00037 if (is_array($__context__)) { extract($__context__); } 00038 if (is_object($__context__)) { extract($__context__->toHash()); } 00039 // END EXTRACT __context__ 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 // add extract code to function code 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 ?>

Generated on Tue Jun 29 23:40:03 2004 for Mediawiki by doxygen 1.3.7