Changeset 1073459
- Timestamp:
- 01/22/2015 04:39:49 PM (11 years ago)
- File:
-
- 1 edited
-
enzymes/trunk/src/Enzymes3.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
enzymes/trunk/src/Enzymes3.php
r1068480 r1073459 299 299 $this->init_expressions(); 300 300 $this->options = new EnzymesOptions(); 301 302 $this->has_eval_recovered = true; 303 register_shutdown_function(array($this, 'echo_last_eval_error')); 301 304 } 302 305 … … 324 327 * Echo a script HTML tag to write data to the javascript console of the browser. 325 328 * 326 * @param mixed $data 327 */ 328 protected 329 function console_log( $data ) 330 { 331 $json = json_encode((is_array($data) || is_object($data)) 332 ? $data 333 : trim($data)); 334 $output = "<script>if(window.console){if(window.console.log){window.console.log($json);}}</script>"; 329 * @param mixed $logs 330 */ 331 protected 332 function console_log( $logs ) 333 { 334 if ( count($logs) == 0 ) { 335 return; 336 } 337 $lines = array(); 338 foreach ($logs as $data) { 339 $json = json_encode((is_array($data) || is_object($data)) 340 ? $data 341 : trim($data)); 342 $lines[] = "window.console.log($json);"; 343 } 344 $lines = implode('', $lines); 345 $output = "<script>if(window.console){if(window.console.log){$lines}}</script>"; 335 346 echo $output; 336 347 } 337 348 349 protected $has_eval_recovered; 350 351 protected 352 function decorate( $title, $output ) 353 { 354 $logs[] = $title; 355 $logs[] = sprintf(__('Post: %1$s - Enzyme: %3$s - Injection: {[%2$s]}'), $this->injection_post->ID, 356 $this->current_sequence, $this->current_enzyme); 357 $logs[] = $output; 358 return $logs; 359 } 360 361 public 362 function echo_last_eval_error() 363 { 364 // Only execute after a bad eval. 365 if ( $this->has_eval_recovered ) { 366 return; 367 } 368 // We are shutting down, so $error is really the last (fatal) error. 369 $error = error_get_last(); 370 echo "\n"; 371 $this->console_log($this->decorate(__('ENZYMES FATAL ERROR'), 372 sprintf(__('Fatal error: %1$s on line %2$s'), $error['message'], $error['line']))); 373 echo "\n"; 374 } 375 338 376 /** 339 377 * Handle an error in eval. 340 378 * 341 * @param $type 342 * @param $message 343 * @param $file 344 * @param $line 379 * @param int $type 380 * @param string $message 381 * @param string $file 382 * @param int $line 383 * @param array $context 345 384 * 346 385 * @return bool 347 386 */ 348 387 protected 349 function set_last_eval_error( $type, $message, $file, $line ) 350 { 351 $this->last_eval_error = array('type' => $type, 'message' => $message, 'file' => $file, 'line' => $line); 352 return true; 388 function set_last_eval_error( $type = null, $message = null, $file = null, $line = null, $context = null ) 389 { 390 $this->last_eval_error = array( 391 'type' => $type, 392 'message' => $message, 393 'file' => $file, 394 'line' => $line, 395 'context' => $context 396 ); 397 return true; // True to consider the error handled and suppress bubbling. 353 398 } 354 399 … … 375 420 376 421 $result = trim($result); 377 $result = str_replace( $filename, 'enzyme code', $result);378 $result = str_replace("\nErrors parsing enzyme code", '', $result);422 $result = str_replace("in $filename on", 'on', $result); 423 $result = str_replace("\nErrors parsing $filename", '', $result); 379 424 return $result; 380 425 } … … 395 440 function safe_eval( $code, array $arguments = array() ) 396 441 { 397 $error = $output = $exception = null;398 $previous_ini = array();399 400 442 $previous_ini['scream.enabled'] = ini_set('scream.enabled', false); 401 set_error_handler(array($this, 'set_last_eval_error'), E_ALL );443 set_error_handler(array($this, 'set_last_eval_error'), E_ALL | E_STRICT); 402 444 ob_start(); 445 $this->has_eval_recovered = false; 446 $this->last_eval_error = null; 447 // ------------------------------------------------------------------------------------------------------------- 403 448 try { 404 $this->last_eval_error = null;405 // ---------------------------------------------------------------------------------------------------------406 449 $result = @eval($code); 407 // --------------------------------------------------------------------------------------------------------- 408 $error = $this->last_eval_error; 409 $this->last_eval_error = null; 450 $error = $this->last_eval_error; 410 451 } catch ( Exception $e ) { 411 $result = false; // Let's force the same error treatment 412 $exception = $e; // but remember the exception now. 413 } 414 $output = ob_get_clean(); 452 $result = false; // Let's force the same error treatment 453 $error = $e; // and take the exception as the error. 454 } 455 // ------------------------------------------------------------------------------------------------------------- 456 $this->last_eval_error = null; 457 $this->has_eval_recovered = true; 458 $output = ob_get_clean(); 415 459 restore_error_handler(); 416 460 ini_set('scream.enabled', $previous_ini['scream.enabled']); 417 461 418 462 if ( false === $result ) { 419 if ( null === $error && null === $exception ) {463 if ( ! $error instanceof Exception ) { 420 464 $error = $this->php_lint($code); 421 }422 if ( null === $error ) {423 $error = $exception;424 465 } 425 466 }
Note: See TracChangeset
for help on using the changeset viewer.