a122229bb6cecb84492183e25c511546756ff0dc
[lhc/web/wiklou.git] / PHPTAL-NP-0.7.0 / libs / Types / Code.php
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$
23
24 require_once "Types/OString.php";
25 require_once "Types/OHash.php";
26
27 /**
28 * Common php string that extract parameters from an associative array.
29 *
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.
32 *
33 * @access private
34 */
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 ');
41
42 /**
43 * Code class handle and evaluate php code.
44 *
45 * The aim of this class is to dynamically generate executable php code from
46 * php string.
47 *
48 * This kind of object can be safely serialized as the code it represents is
49 * stored in the _code member variable.
50 *
51 * When setting code to this object, a new anonymous function is created
52 * waiting to be invoqued using the execute() method.
53 *
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.
57 *
58 * Code object automatically append 'extract' code string to produced code.
59 *
60 * It's up to the code to assert parameters using 'isset()' php function.
61 *
62 * <?php
63 * require_once "Types/Code.php";
64 *
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 * );
72 *
73 * // will print 'foo value bar value baz value'
74 * echo $res, end;
75 * ?>
76 *
77 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
78 */
79 class Code
80 {
81 var $_code;
82 var $_function;
83 var $_compiled = false;
84
85 /**
86 * Construct a new code object.
87 *
88 * @param mixed $code (optional)
89 * php code string or object implementing toString method.
90 */
91 function Code($code=false)
92 {
93 $this->__construct($code);
94 }
95
96 function __construct($code=false)
97 {
98 if (!$code) {
99 return $this;
100 }
101 if (is_object($code) && get_class($code) == 'code') {
102 $this->_code = $code->getCode();
103 } else {
104 $this->setCode(Types::toString($code));
105 }
106 }
107
108 /**
109 * Execute code with specific context.
110 *
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
115 *
116 * @return mixed The execution result.
117 */
118 function &execute()
119 {
120 if (!$this->_compiled) {
121 $err =& $this->compile();
122 if (PEAR::isError($err)) {
123 return $err;
124 }
125 }
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;
135 }
136 $func = $this->_function;
137 return $func($context);
138 }
139
140 /**
141 * Compile php code.
142 *
143 * This function may produce parse errors.
144 *
145 * @throws CodeError
146 */
147 function compile()
148 {
149 ob_start();
150 $this->_function = create_function('$__context__', $this->_code);
151 $ret = ob_get_contents();
152 ob_end_clean();
153
154 if (!$this->_function) {
155 return PEAR::raiseError($ret);
156 }
157
158 $this->_compiled = true;
159 }
160
161 /**
162 * Set function code.
163 *
164 * @param string $str
165 * The php code string
166 */
167 function setCode($str)
168 {
169 // add extract code to function code
170 $str = _TYPES_CODE_EXTRACT_CODE_CONTEXT_ . $str;
171 $this->_code = $str;
172 }
173
174 /**
175 * Retrieve code.
176 *
177 * @return string
178 */
179 function getCode()
180 {
181 return $this->_code;
182 }
183
184 /**
185 * On serialization, we store only code, not additional variables.
186 *
187 * @access private
188 */
189 function __sleep()
190 {
191 return array("_code");
192 }
193
194 /**
195 * Make a string representation of this object.
196 *
197 * @return string
198 */
199 function toString()
200 {
201 if ($this->_compiled) {
202 return '<Code \''.$this->_function.'\'>';
203 } else {
204 return '<Code \'not compiled\'>';
205 }
206 }
207 }
208
209 /**
210 * Code compilation error.
211 *
212 * This error handles parse errors an other problems that may occurs while
213 * compiling a php code.
214 */
215 class CodeError extends PEAR_Error
216 {
217 }
218
219 ?>