2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
4 // Copyright (c) 2003 Laurent Bedubourg
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
24 require_once PT_IP
. "/Types/OString.php";
25 require_once PT_IP
. "/Types/OHash.php";
28 * Common php string that extract parameters from an associative array.
30 * This string is appended to the begin of the produced php function. It
31 * takes function context argument and extract it in named variables.
35 define('_TYPES_CODE_EXTRACT_CODE_CONTEXT_',
36 '// BEGIN EXTRACT __context__
37 if (is_array($__context__)) { extract($__context__); }
38 if (is_object($__context__)) { extract($__context__->toHash()); }
39 // END EXTRACT __context__
43 * Code class handle and evaluate php code.
45 * The aim of this class is to dynamically generate executable php code from
48 * This kind of object can be safely serialized as the code it represents is
49 * stored in the _code member variable.
51 * When setting code to this object, a new anonymous function is created
52 * waiting to be invoqued using the execute() method.
54 * As we can't know how many parameters this function should take, a 'context'
55 * hashtable is used as only parameter. This hash may contains any number
56 * of arguments with var name compliant keys.
58 * Code object automatically append 'extract' code string to produced code.
60 * It's up to the code to assert parameters using 'isset()' php function.
63 * require_once PT_IP . "/Types/Code.php";
66 * $o_code->setCode('return $foo . $bar . $baz;');
67 * $res = $o_code->execute(
68 * 'foo', 'foo value ',
69 * 'baz', 'bar value ',
73 * // will print 'foo value bar value baz value'
77 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
83 var $_compiled = false;
86 * Construct a new code object.
88 * @param mixed $code (optional)
89 * php code string or object implementing toString method.
91 function Code($code=false)
93 $this->__construct($code);
96 function __construct($code=false)
101 if (is_object($code) && get_class($code) == 'code') {
102 $this->_code
= $code->getCode();
104 $this->setCode(Types
::toString($code));
109 * Execute code with specific context.
112 * The function execution context. This may be an associative
113 * array, an OHash object, a list of key/value pairs that will
114 * be transformed into an associative array
116 * @return mixed The execution result.
120 if (!$this->_compiled
) {
121 $err =& $this->compile();
122 if (PEAR
::isError($err)) {
126 $argv = func_get_args();
127 $argc = func_num_args();
133 $context = OHash
::ArrayToHash($argv);
136 $func = $this->_function
;
137 return $func($context);
143 * This function may produce parse errors.
150 $this->_function
= create_function('$__context__', $this->_code
);
151 $ret = ob_get_contents();
154 if (!$this->_function
) {
155 return PEAR
::raiseError($ret);
158 $this->_compiled
= true;
165 * The php code string
167 function setCode($str)
169 // add extract code to function code
170 $str = _TYPES_CODE_EXTRACT_CODE_CONTEXT_
. $str;
185 * On serialization, we store only code, not additional variables.
191 return array("_code");
195 * Make a string representation of this object.
201 if ($this->_compiled
) {
202 return '<Code \''.$this->_function
.'\'>';
204 return '<Code \'not compiled\'>';
210 * Code compilation error.
212 * This error handles parse errors an other problems that may occurs while
213 * compiling a php code.
215 class CodeError
extends PEAR_Error