Add untracked files.
[order_line_extra.git] / hincludes / lib / php / Boot.class.php
1 <?php
2
3 class php_Boot {
4         public function __construct(){}
5         static $qtypes;
6         static $ttypes;
7         static $tpaths;
8         static $skip_constructor = false;
9         function __toString() { return 'php.Boot'; }
10 }
11 {
12         $_hx_class_prefix = null;
13         
14 function _hx_add($a, $b) {
15         if (!_hx_is_numeric($a) || !_hx_is_numeric($b)) {
16                 return $a . $b;
17         } else {
18                 return $a + $b;
19         }
20 }
21
22 function _hx_anonymous($arr = array()) {
23         $o = new _hx_anonymous();
24         foreach($arr as $k => $v)
25                 $o->$k = $v;
26         return $o;
27 }
28
29 class _hx_array implements ArrayAccess, IteratorAggregate {
30         var $»a;
31         var $length;
32         function __construct($a = array()) {
33                 $this->»a = $a;
34                 $this->length = count($a);
35         }
36
37         function concat($a) {
38                 return new _hx_array(array_merge($this->»a, $a->»a));
39         }
40
41         function copy() {
42                 return new _hx_array($this->»a);
43         }
44
45         function &get($index) {
46                 if(isset($this->»a[$index])) return $this->»a[$index];
47                 return null;
48         }
49
50         function insert($pos, $x) {
51                 array_splice($this->»a, $pos, 0, array($x));
52                 $this->length++;
53         }
54
55         function iterator() {
56                 return new _hx_array_iterator($this->»a);
57         }
58
59         function getIterator() {
60                 return $this->iterator();
61         }
62
63         function join($sep) {
64                 return implode($sep, $this->»a);
65         }
66
67         function pop() {
68                 $r = array_pop($this->»a);
69                 $this->length = count($this->»a);
70                 return $r;
71         }
72
73         function push($x) {
74                 $this->»a[] = $x;
75                 return ++$this->length;
76         }
77
78         function remove($x) {
79                 for($i = 0; $i < count($this->»a); $i++)
80                         if($this->»a[$i] === $x) {
81                                 unset($this->»a[$i]);
82                                 $this->»a = array_values($this->»a);
83                                 $this->length--;
84                                 return true;
85                         }
86                 return false;
87         }
88
89         function removeAt($pos) {
90                 if(array_key_exists($pos, $this->»a)) {
91                         unset($this->»a[$pos]);
92                         $this->length--;
93                         return true;
94                 } else
95                         return false;
96         }
97
98         function reverse() {
99                 $this->»a = array_reverse($this->»a, false);
100         }
101
102         function shift() {
103                 $r = array_shift($this->»a);
104                 $this->length = count($this->»a);
105                 return $r;
106         }
107
108         function slice($pos, $end) {
109                 if($end === null)
110                         return new _hx_array(array_slice($this->»a, $pos));
111                 else
112                         return new _hx_array(array_slice($this->»a, $pos, $end-$pos));
113         }
114
115         function sort($f) {
116                 usort($this->»a, $f);
117         }
118
119         function splice($pos, $len) {
120                 if($len < 0) $len = 0;
121                 $nh = new _hx_array(array_splice($this->»a, $pos, $len));
122                 $this->length = count($this->»a);
123                 return $nh;
124         }
125
126         function toString() {
127                 return '['.implode(',', array_map('_hx_string_rec',$this->»a,array())).']';
128         }
129
130         function __toString() {
131                 return $this->toString();
132         }
133
134         function unshift($x) {
135                 array_unshift($this->»a, $x);
136                 $this->length++;
137         }
138
139         // ArrayAccess methods:
140         function offsetExists($offset) {
141                 return isset($this->»a[$offset]);
142         }
143
144         function offsetGet($offset) {
145                 if(isset($this->»a[$offset])) return $this->»a[$offset];
146                 return null;
147         }
148
149         function offsetSet($offset, $value) {
150                 if($this->length <= $offset) {
151                         $this->»a = array_merge($this->»a, array_fill(0, $offset+1-$this->length, null));
152                         $this->length = $offset+1;
153                 }
154                 return $this->»a[$offset] = $value;
155         }
156
157         function offsetUnset($offset) {
158                 return $this->removeAt($offset);
159         }
160 }
161
162 class _hx_array_iterator implements Iterator {
163         private $»a;
164         private $»i;
165         public function __construct($a) {
166                 $this->»a = $a;
167                 $this->»i = 0;
168         }
169
170         public function next() {
171                 if(!$this->hasNext()) return null;
172                 return $this->»a[$this->»i++];
173         }
174
175         public function hasNext() {
176                 return $this->»i < count($this->»a);
177         }
178
179         public function current() {
180                 if (!$this->hasNext()) return false;
181                 return $this->»a[$this->»i];
182         }
183
184         public function key() {
185                 return $this->»i;
186         }
187
188         public function valid() {
189                 return $this->current() !== false;
190         }
191
192         public function rewind() {
193                 $this->»i = 0;
194         }
195         public function size() {
196                 return count($this->»a);
197         }
198 }
199
200 function _hx_array_get($a, $pos) { return $a[$pos]; }
201
202 function _hx_array_increment($a, $pos) { return $a[$pos] += 1; }
203 function _hx_array_decrement($a, $pos) { return $a[$pos] -= 1; }
204
205 function _hx_array_assign($a, $i, $v) { return $a[$i] = $v; }
206
207 class _hx_break_exception extends Exception {}
208
209 function _hx_cast($v, $type) {
210         if(Std::is($v, $type)) {
211                 return $v;
212         } else {
213                 throw new HException('Class cast error');
214         }
215 }
216
217 function _hx_char_at($o, $i) { $c = substr($o, $i, 1); return FALSE === $c ? '' : $c; }
218
219 function _hx_char_code_at($s, $pos) {
220         if($pos < 0 || $pos >= strlen($s)) return null;
221         return ord($s{$pos});
222 }
223
224 function _hx_deref($o) { return $o; }
225
226 function _hx_equal($x, $y) {
227         if(is_null($x)) {
228                 return is_null($y);
229         } else {
230                 if(is_null($y)) {
231                         return false;
232                 } else {
233                         if((is_float($x) || is_int($x)) && (is_float($y) || is_int($y))) {
234                                 return $x == $y;
235                         } else {
236                                 return $x === $y;
237                         }
238                 }
239         }
240 }
241
242 function _hx_mod($x, $y) {
243         if (is_int($x) && is_int($y)) {
244                 if ($y == 0) return 0;
245                 return $x % $y;
246         }
247         if (!is_nan($x) && !is_nan($y) && !is_finite($y) && is_finite($x)) {
248                 return $x;
249         } 
250         return fmod($x, $y);
251 }
252
253 function _hx_error_handler($errno, $errmsg, $filename, $linenum, $vars) {
254         $msg = $errmsg . ' (errno: ' . $errno . ') in ' . $filename . ' at line #' . $linenum;
255         $e = new HException($msg, $errmsg, $errno, _hx_anonymous(array('fileName' => 'Boot.hx', 'lineNumber' => __LINE__, 'className' => 'php.Boot', 'methodName' => '_hx_error_handler')));
256         $e->setFile($filename);
257         $e->setLine($linenum);
258         throw $e;
259         return null;
260 }
261
262 function _hx_exception_handler($e) {
263         if(0 == strncasecmp(PHP_SAPI, 'cli', 3)) {
264                 $msg   = $e-> getMessage();
265                 $nl    = "\n";
266                 $pre   = '';
267                 $post  = '';
268         } else {
269                 $msg   = '<b>' . $e-> getMessage() . '</b>';
270                 $nl    = "<br />";
271                 $pre   = '<pre>';
272                 $post  = '</pre>';
273         }
274         if(isset($GLOBALS['%s'])) {
275                 $stack = '';
276                 $i = $GLOBALS['%s']->length;
277                 while(--$i >= 0)
278                         $stack .= 'Called from '.$GLOBALS['%s'][$i].$nl;
279                 die($pre.'uncaught exception: '.$msg.$nl.$nl.$stack.$post);
280         } else
281                 die($pre.'uncaught exception: '.$msg.$nl.$nl.'in file: '.$e->getFile().' line '.$e->getLine().$nl.$e->getTraceAsString().$post);
282 }
283
284 function _hx_explode($delimiter, $s) {
285         if($delimiter == '')
286                 return new _hx_array(str_split($s, 1));
287         return new _hx_array(explode($delimiter, $s));
288 }
289
290 function _hx_explode2($s, $delimiter) {
291         if($delimiter == '')
292                 return new _hx_array(str_split($s, 1));
293         return new _hx_array(explode($delimiter, $s));
294 }
295
296 function _hx_field($o, $field) {
297         if(_hx_has_field($o, $field)) {
298                 if($o instanceof _hx_type) {
299                         if(is_callable($c = array($o->__tname__, $field)) && !property_exists($o->__tname__, $field)) {
300                                 return $c;
301                         } else {
302                                 $name = $o->__tname__;
303                                 return eval('return '.$name.'::$'.$field.';');
304                         }
305                 } else {
306                         if(is_string($o)) {
307                                 if($field == 'length') {
308                                         return strlen($o);
309                                 } else {
310                                         switch($field) {
311                                                 case 'charAt'     : return array(new _hx_lambda(array(&$o), '_hx_char_at'), 'execute');
312                                                 case 'charCodeAt' : return array(new _hx_lambda(array(&$o), '_hx_char_code_at'), 'execute');
313                                                 case 'indexOf'    : return array(new _hx_lambda(array(&$o), '_hx_index_of'), 'execute');
314                                                 case 'lastIndexOf': return array(new _hx_lambda(array(&$o), '_hx_last_index_of'), 'execute');
315                                                 case 'split'      : return array(new _hx_lambda(array(&$o), '_hx_explode2'), 'execute');
316                                                 case 'substr'     : return array(new _hx_lambda(array(&$o), '_hx_substr'), 'execute');
317                                                 case 'toUpperCase': return array(new _hx_lambda(array(&$o), 'strtoupper'), 'execute');
318                                                 case 'toLowerCase': return array(new _hx_lambda(array(&$o), 'strtolower'), 'execute');
319                                                 case 'toString'   : return array(new _hx_lambda(array(&$o), '_hx_deref'), 'execute');
320                                         }
321                                         return null;
322                                 }
323                         } else {
324                                 if(property_exists($o, $field)) {
325                                         if(is_array($o->$field) && is_callable($o->$field)) {
326                                                 return $o->$field;
327                                         } else {
328                                                 if(is_string($o->$field) && _hx_is_lambda($o->$field)) {
329                                                         return array($o, $field);
330                                                 } else {
331                                                         return $o->$field;
332                                                 }
333                                         }
334                                 } else if(isset($o->»dynamics[$field])) {
335                                         return $o->»dynamics[$field];
336                                 } else {
337                                         return array($o, $field);
338                                 }
339                         }
340                 }
341         } else {
342                 return null;
343         }
344 }
345
346 function _hx_get_object_vars($o) {
347         $a = array_keys(get_object_vars($o));
348         if(isset($o->»dynamics))
349                 $a = array_merge($a, array_keys($o->»dynamics));
350         $arr = array();
351         for($i=0;$i<count($a); $i++)
352         {
353                 $k = '' . $a[$i];
354                 if(substr($k, 0, 1) != '»')
355                         $arr[] = $k;
356         }
357         return $arr;
358 }
359
360 function _hx_has_field($o, $field) {
361         return
362                 (is_object($o) && (method_exists($o, $field) || isset($o->$field) || property_exists($o, $field) || isset($o->»dynamics[$field])))
363                 ||
364                 (is_string($o) && (in_array($field, array('toUpperCase', 'toLowerCase', 'charAt', 'charCodeAt', 'indexOf', 'lastIndexOf', 'split', 'substr', 'toString', 'length'))))
365         ;
366 }
367
368 function _hx_index_of($s, $value, $startIndex = null) {
369         $x = strpos($s, $value, $startIndex);
370         if($x === false)
371                 return -1;
372         else
373                 return $x;
374 }
375
376 function _hx_instanceof($v, $t) {
377         if($t === null) {
378                 return false;
379         }
380         switch($t->__tname__) {
381                 case 'Array'  : return is_array($v);
382                 case 'String' : return is_string($v) && !_hx_is_lambda($v);
383                 case 'Bool'   : return is_bool($v);
384                 case 'Int'    : return is_int($v) || (is_float($v) && intval($v) == $v && !is_nan($v));
385                 case 'Float'  : return is_float($v) || is_int($v);
386                 case 'Dynamic': return true;
387                 case 'Class'  : return ($v instanceof _hx_class || $v instanceof _hx_interface) && $v->__tname__ != 'Enum';
388                 case 'Enum'   : return $v instanceof _hx_enum;
389                 default       : return is_a($v, $t->__tname__);
390         }
391 }
392
393 function _hx_is_lambda($s) {
394         return (is_string($s) && substr($s, 0, 8) == chr(0).'lambda_') || (is_array($s) && count($s) > 0 && (is_a($s[0], '_hx_lambda') || is_a($s[0], '_hx_lambda2')));
395 }
396
397 function _hx_is_numeric($v)
398 {
399         return is_numeric($v) && !is_string($v);
400 }
401
402 function _hx_last_index_of($s, $value, $startIndex = null) {
403         $x = strrpos($s, $value, $startIndex === null ? null : strlen($s) - $startIndex);
404         if($x === false)
405                 return -1;
406         else
407                 return $x;
408 }
409
410 function _hx_len($o) {
411         return is_string($o) ? strlen($o) : $o->length;
412 }
413
414 class _hx_list_iterator implements Iterator {
415         private $»h;
416         private $»list;
417         private $»counter;
418         public function __construct($list) {
419                 $this->»list = $list;
420                 $this->rewind();
421         }
422
423         public function next() {
424                 if($this->»h == null) return null;
425                 $this->»counter++;
426                 $x = $this->»h[0];
427                 $this->»h = $this->»h[1];
428                 return $x;
429         }
430
431         public function hasNext() {
432                 return $this->»h != null;
433         }
434
435         public function current() {
436                 if (!$this->hasNext()) return null;
437                 return $this->»h[0];
438         }
439
440         public function key() {
441                 return $this->»counter;
442         }
443
444         public function valid() {
445                 return $this->current() !== null;
446         }
447
448         public function rewind() {
449                 $this->»counter = -1;
450                 $this->»h = $this->»list->h;
451         }
452
453         public function size() {
454                 return $this->»list->length;
455         }
456 }
457
458 function _hx_null() { return null; }
459
460 class _hx_nullob {
461         function _throw()       { throw new HException('Null object'); }
462         function __call($f, $a) { $this->_throw(); }
463         function __get($f)      { $this->_throw(); }
464         function __set($f, $v)  { $this->_throw(); }
465         function __isset($f)    { $this->_throw(); }
466         function __unset($f)    { $this->_throw(); }
467         function __toString()   { return 'null'; }
468         static $inst;
469 }
470
471 _hx_nullob::$inst = new _hx_nullob();
472
473 function _hx_nullob() { return _hx_nullob::$inst; }
474
475 function _hx_qtype($n) {
476         return isset(php_Boot::$qtypes[$n]) ? php_Boot::$qtypes[$n] : null;
477 }
478
479 function _hx_register_type($t) {
480         php_Boot::$qtypes[$t->__qname__] = $t;
481         php_Boot::$ttypes[$t->__tname__] = $t;
482         if($t->__path__ !== null)
483                 php_Boot::$tpaths[$t->__tname__] = $t->__path__;
484 }
485
486 function _hx_set_method($o, $field, $func) {
487         $value[0]->scope = $o;
488         $o->$field = $func;
489 }
490
491 function _hx_shift_right($v, $n) {
492         $z = 0x80000000;
493         if ($z & $v) {
494                 $v = ($v>>1);
495                 $v &= (~$z);
496                 $v |= 0x40000000;
497                 $v = ($v>>($n-1));
498         } else $v = ($v>>$n);
499         return $v;
500 }
501
502 function _hx_string_call($s, $method, $params) {
503         if(!is_string($s)) return call_user_func_array(array($s, $method), $params);
504         switch($method) {
505                 case 'toUpperCase': return strtoupper($s);
506                 case 'toLowerCase': return strtolower($s);
507                 case 'charAt'     : return substr($s, $params[0], 1);
508                 case 'charCodeAt' : return _hx_char_code_at($s, $params[0]);
509                 case 'indexOf'    : return _hx_index_of($s, $params[0], (count($params) > 1 ? $params[1] : null));
510                 case 'lastIndexOf': return _hx_last_index_of($s, (count($params) > 1 ? $params[1] : null), null);
511                 case 'split'      : return _hx_explode($params[0], $s);
512                 case 'substr'     : return _hx_substr($s, $params[0], (count($params) > 1 ? $params[1] : null));
513                 case 'toString'   : return $s;
514                 default           : throw new HException('Invalid Operation: ' . $method);
515         }
516 }
517
518 function _hx_string_rec($o, $s) {
519         if($o === null)                return 'null';
520         if(strlen($s) >= 5)            return '<...>';
521         if(is_int($o) || is_float($o)) return '' . $o;
522         if(is_bool($o))                return $o ? 'true' : 'false';
523         if(is_object($o)) {
524                 $c = get_class($o);
525                 if($o instanceof Enum) {
526                         $b = $o->tag;
527                         if(!empty($o->params)) {
528                                 $s .= " ";
529                                 $b .= '(';
530                                 for($i = 0; $i < count($o->params); $i++) {
531                                         if($i > 0)
532                                                 $b .= ',' . _hx_string_rec($o->params[$i], $s);
533                                         else
534                                                 $b .= _hx_string_rec($o->params[$i], $s);
535                                 }
536                                 $b .= ')';
537                         }
538                         return $b;
539                 } else {
540                         if ($o instanceof _hx_anonymous) {
541                                 if ($o->toString && is_callable($o->toString)) {
542                                         return call_user_func($o->toString);
543                                 }
544                                 $rfl = new ReflectionObject($o);
545                                 $b2 = "{
546 ";
547                                 $s .= " ";
548                                 $properties = $rfl->getProperties();
549
550                                 for($i = 0; $i < count($properties); $i++) {
551                                         $prop = $properties[$i];
552                                         $f = $prop->getName();
553                                         if($i > 0)
554                                                 $b2 .= ", 
555 ";
556                                         $b2 .= $s . $f . ' : ' . _hx_string_rec($o->$f, $s);
557                                 }
558                                 $s = substr($s, 1);
559                                 $b2 .= "
560 " . $s . '}';
561                                 return $b2;
562                         } else {
563                                 if($o instanceof _hx_type)
564                                         return $o->__qname__;
565                                 else {
566                                         if(is_callable(array($o, 'toString')))
567                                                 return $o->toString();
568                                         else {
569                                                 if(is_callable(array($o, '__toString')))
570                                                         return $o->__toString();
571                                                 else
572                                                         return '[' . _hx_ttype($c) . ']';
573                                         }
574                                 }
575                         }
576                 }
577         }
578         if(is_string($o)) {
579                 if(_hx_is_lambda($o)) return '«function»';
580 //              if(strlen($s) > 0)    return '"' . str_replace('"', '\"', $o) . '"';
581                 else                  return $o;
582         }
583         if(is_array($o)) {
584                 if(is_callable($o)) return '«function»';
585                 $str = '[';
586                 $s .= " ";
587                 $first = true;
588                 $assoc = true;
589                 foreach($o as $k => $v)
590                 {
591                         if ($first && $k === 0)
592                                 $assoc = false;
593                         $str .= ($first ? '' : ',') . ($assoc
594                                 ? _hx_string_rec($k, $s) . '=>' . _hx_string_rec($o[$k], $s)
595                                 : _hx_string_rec($o[$k], $s)
596                         );
597                         $first = false;
598                 }
599                 $str .= ']';
600                 return $str;
601         }
602         return '';
603 }
604
605 function _hx_substr($s, $pos, $len) {
606         if($pos !== null && $pos !== 0 && $len !== null && $len < 0) return '';
607         if($len === null) $len = strlen($s);
608         if($pos < 0) {
609                 $pos = strlen($s) + $pos;
610                 if($pos < 0) $pos = 0;
611         } else if($len < 0 )
612                 $len = strlen($s) + $len - $pos;
613         $s = substr($s, $pos, $len);
614         if($s === false)
615                 return '';
616         else
617                 return $s;
618 }
619
620 function _hx_trace($v, $i) {
621         $msg = $i !== null ? $i->fileName.':'.$i->lineNumber.': ' : '';
622         echo $msg._hx_string_rec($v, '')."
623 ";
624 }
625
626 function _hx_ttype($n) {
627         return isset(php_Boot::$ttypes[$n]) ? php_Boot::$ttypes[$n] : null;
628 }
629
630 function _hx_make_var_args() {
631         $args = func_get_args();
632         $f = array_shift($args);
633         return call_user_func($f, new _hx_array($args));
634 }
635
636 class _hx_anonymous extends stdClass {
637         public function __call($m, $a) {
638                 return call_user_func_array($this->$m, $a);
639         }
640
641         public function __set($n, $v) {
642                 $this->$n = $v;
643         }
644
645         public function &__get($n) {
646                 if(isset($this->$n))
647                         return $this->$n;
648                 $null = null;
649                 return $null;
650         }
651
652         public function __isset($n) {
653                 return isset($this->$n);
654         }
655
656         public function __unset($n) {
657                 unset($this->$n);
658         }
659
660         public function __toString() {
661                 $rfl = new ReflectionObject($this);
662                 $b = '{ ';
663                 $properties = $rfl->getProperties();
664                 $first = true;
665                 while(list(, $prop) = each($properties)) {
666                         if($first)
667                                 $first = false;
668                         else
669                                 $b .= ', ';
670                         $f = $prop->getName();
671                         $b .= $f . ' => ' . $this->$f;
672                 }
673                 $b .= ' }';
674                 return $b;
675         }
676 }
677
678 class _hx_type {
679         public $__tname__;
680         public $__qname__;
681         public $__path__;
682         public function __construct($cn, $qn, $path = null) {
683                 $this->__tname__ = $cn;
684                 $this->__qname__ = $qn;
685                 $this->__path__ = $path;
686                 if(property_exists($cn, '__meta__'))
687                         $this->__meta__ =  eval($cn.'::$__meta__');
688         }
689
690         public function toString()   { return $this->__toString(); }
691
692         public function __toString() {
693                 return $this->__qname__;
694         }
695
696         private $rfl = false;
697         public function __rfl__() {
698                 if($this->rfl !== false) return $this->rfl;
699                 if(class_exists($this->__tname__) || interface_exists($this->__tname__))
700                         $this->rfl = new ReflectionClass($this->__tname__);
701                 else
702                         $this->rfl = null;
703                 return $this->rfl;
704         }
705
706         public function __call($n, $a) {
707                 return call_user_func_array(array($this->__tname__, $n), $a);
708         }
709
710         public function __get($n) {
711                 if(($r = $this->__rfl__())==null) return null;
712                 if($r->hasProperty($n))
713                         return $r->getStaticPropertyValue($n);
714                 else if($r->hasMethod($n))
715                         return array($r, $n);
716                 else
717                         return null;
718         }
719
720         public function __set($n, $v) {
721                 if(($r = $this->__rfl__())==null) return null;
722                 return $r->setStaticPropertyValue($n, $v);
723         }
724
725         public function __isset($n) {
726                 if(($r = $this->__rfl__())==null) return null;
727                 return $r->hasProperty($n) || $r->hasMethod($n);
728         }
729 }
730
731 class _hx_class extends _hx_type {}
732
733 class _hx_enum extends _hx_type {}
734
735 class _hx_interface extends _hx_type {}
736
737 class HException extends Exception {
738         public function __construct($e, $message = null, $code = null, $p = null) {
739                 $message = _hx_string_rec($e, '') . $message;
740                 parent::__construct($message,$code);
741                 $this->e = $e;
742                 $this->p = $p;
743         }
744         public $e;
745         public $p;
746         public function setLine($l) {
747                 $this->line = $l;
748         }
749         public function setFile($f) {
750                 $this->file = $f;
751         }
752 }
753
754 class _hx_lambda {
755         public function __construct($locals, $func) {
756                 $this->locals = $locals;
757                 $this->func = $func;
758         }
759         public $locals;
760         public $func;
761
762         public function execute() {
763                 // if use $this->locals directly in array_merge it works only if I make the assignement loop,
764                 // so I've decided to reference $arr
765                 $arr = array();
766                 for ($i = 0; $i<count($this->locals);$i++)
767                         $arr[] = & $this->locals[$i];
768                 $args = func_get_args();
769                 return call_user_func_array($this->func, array_merge($arr, $args));
770         }
771 }
772
773 class Enum {
774         public function __construct($tag, $index, $params = null) { $this->tag = $tag; $this->index = $index; $this->params = $params; }
775         public $tag;
776         public $index;
777         public $params;
778
779         public function __toString() {
780                 return $this->tag;
781         }
782 }
783
784 error_reporting(E_ALL & ~E_STRICT);
785 set_error_handler('_hx_error_handler', E_ALL);
786 set_exception_handler('_hx_exception_handler');
787
788 php_Boot::$qtypes = array();
789 php_Boot::$ttypes = array();
790 php_Boot::$tpaths = array();
791
792 _hx_register_type(new _hx_class('String',  'String'));
793 _hx_register_type(new _hx_class('_hx_array', 'Array'));
794 _hx_register_type(new _hx_class('Int',     'Int'));
795 _hx_register_type(new _hx_class('Float',   'Float'));
796 _hx_register_type(new _hx_class('Class',   'Class'));
797 _hx_register_type(new _hx_class('Enum',    'Enum'));
798 _hx_register_type(new _hx_class('Dynamic', 'Dynamic'));
799 _hx_register_type(new _hx_enum('Bool',     'Bool'));
800 _hx_register_type(new _hx_enum('Void',     'Void'));
801
802
803 $_hx_libdir = dirname(__FILE__) . '/..';
804 $_hx_autload_cache_file = $_hx_libdir . '/../cache/haxe_autoload.php';
805 if(!file_exists($_hx_autload_cache_file)) {
806         function _hx_build_paths($d, &$_hx_types_array, $pack, $prefix) {
807                 $h = opendir($d);
808                 while(false !== ($f = readdir($h))) {
809                         $p = $d.'/'.$f;
810                         if($f == '.' || $f == '..')
811                                 continue;
812                                 if (is_file($p) && substr($f, -4) == '.php') {
813                                 $bn = basename($f, '.php');
814                                 if ($prefix)
815                                 {
816                                         if ($prefix != substr($bn, 0, $lenprefix = strlen($prefix)))
817                                                 continue;
818                                         $bn = substr($bn, $lenprefix);
819                                 }
820                                 if(substr($bn, -6) == '.class') {
821                                         $bn = substr($bn, 0, -6);
822                                         $t = 0;
823                                 } else if(substr($bn, -5) == '.enum') {
824                                         $bn = substr($bn, 0, -5);
825                                         $t = 1;
826                                 } else if(substr($bn, -10) == '.interface') {
827                                         $bn = substr($bn, 0, -10);
828                                         $t = 2;
829                                 } else if(substr($bn, -7) == '.extern') {
830                                         $bn = substr($bn, 0, -7);
831                                         $t = 3;
832                                 } else
833                                         continue;
834                                 $qname = ($bn == 'HList' && empty($pack)) ? 'List' : join(array_merge($pack, array($bn)), '.');
835                                 $_hx_types_array[] = array(
836                                         'path' => $p,
837                                         'name' => $prefix . $bn,
838                                         'type' => $t,
839                                         'qname' => $qname,
840                                         'phpname' => join(array_merge($pack, array($prefix . $bn)), '_')
841                                 );
842                         } else if(is_dir($p))
843                                 _hx_build_paths($p, $_hx_types_array, array_merge($pack, array($f)), $prefix);
844                 }
845                 closedir($h);
846         }
847
848         $_hx_cache_content = '<?php
849
850 ';
851         $_hx_types_array = array();
852
853         _hx_build_paths($_hx_libdir, $_hx_types_array, array(), $_hx_class_prefix);
854
855         for($i=0;$i<count($_hx_types_array);$i++) {
856                 $_hx_cache_content .= '_hx_register_type(new ';
857                 $t = null;
858                 if($_hx_types_array[$i]['type'] == 0) {
859                         $t = new _hx_class($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
860                         $_hx_cache_content .= '_hx_class';
861                 } else if($_hx_types_array[$i]['type'] == 1) {
862                         $t = new _hx_enum($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
863                         $_hx_cache_content .= '_hx_enum';
864                 } else if($_hx_types_array[$i]['type'] == 2) {
865                         $t = new _hx_interface($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
866                         $_hx_cache_content .= '_hx_interface';
867                 } else if($_hx_types_array[$i]['type'] == 3) {
868                         $t = new _hx_class($_hx_types_array[$i]['name'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
869                         $_hx_cache_content .= '_hx_class';
870                 }
871                 _hx_register_type($t);
872                 $_hx_cache_content .= '(\''.($_hx_types_array[$i]['type'] == 3 ? $_hx_types_array[$i]['name'] : $_hx_types_array[$i]['phpname']).'\', \''.$_hx_types_array[$i]['qname'].'\', \''.$_hx_types_array[$i]['path'].'\'));
873 ';
874         }
875         try {
876                 file_put_contents($_hx_autload_cache_file, $_hx_cache_content);
877         } catch(Exception $e) {}
878         unset($_hx_types_array);
879         unset($_hx_cache_content);
880 } else {
881         require($_hx_autload_cache_file);
882 }
883
884 function _hx_autoload($name) {
885         if(!isset(php_Boot::$tpaths[$name])) return false;
886         require_once(php_Boot::$tpaths[$name]);
887         return true;
888 }
889
890 if(!ini_get('date.timezone'))
891         date_default_timezone_set('UTC');
892
893 spl_autoload_register('_hx_autoload');
894 }