notice hopefully gone now
[mediawiki.git] / PHPTAL-NP-0.7.0 / libs / Types / Code.php
bloba122229bb6cecb84492183e25c511546756ff0dc
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 //
4 // Copyright (c) 2003 Laurent Bedubourg
5 //
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.
10 //
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.
15 //
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
19 //
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
21 //
22 // $id$
24 require_once "Types/OString.php";
25 require_once "Types/OHash.php";
27 /**
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.
33 * @access private
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__
40 ');
42 /**
43 * Code class handle and evaluate php code.
45 * The aim of this class is to dynamically generate executable php code from
46 * php string.
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.
62 * <?php
63 * require_once "Types/Code.php";
65 * $o_code = Code()
66 * $o_code->setCode('return $foo . $bar . $baz;');
67 * $res = $o_code->execute(
68 * 'foo', 'foo value ',
69 * 'baz', 'bar value ',
70 * 'baz', 'baz value'
71 * );
73 * // will print 'foo value bar value baz value'
74 * echo $res, end;
75 * ?>
77 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
79 class Code
81 var $_code;
82 var $_function;
83 var $_compiled = false;
85 /**
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)
98 if (!$code) {
99 return $this;
101 if (is_object($code) && get_class($code) == 'code') {
102 $this->_code = $code->getCode();
103 } else {
104 $this->setCode(Types::toString($code));
109 * Execute code with specific context.
111 * @param mixed ...
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.
118 function &execute()
120 if (!$this->_compiled) {
121 $err =& $this->compile();
122 if (PEAR::isError($err)) {
123 return $err;
126 $argv = func_get_args();
127 $argc = func_num_args();
128 switch ($argc) {
129 case 1:
130 $context = $argv[0];
131 break;
132 default:
133 $context = OHash::ArrayToHash($argv);
134 break;
136 $func = $this->_function;
137 return $func($context);
141 * Compile php code.
143 * This function may produce parse errors.
145 * @throws CodeError
147 function compile()
149 ob_start();
150 $this->_function = create_function('$__context__', $this->_code);
151 $ret = ob_get_contents();
152 ob_end_clean();
154 if (!$this->_function) {
155 return PEAR::raiseError($ret);
158 $this->_compiled = true;
162 * Set function code.
164 * @param string $str
165 * The php code string
167 function setCode($str)
169 // add extract code to function code
170 $str = _TYPES_CODE_EXTRACT_CODE_CONTEXT_ . $str;
171 $this->_code = $str;
175 * Retrieve code.
177 * @return string
179 function getCode()
181 return $this->_code;
185 * On serialization, we store only code, not additional variables.
187 * @access private
189 function __sleep()
191 return array("_code");
195 * Make a string representation of this object.
197 * @return string
199 function toString()
201 if ($this->_compiled) {
202 return '<Code \''.$this->_function.'\'>';
203 } else {
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