1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184:
<?php
namespace Nethgui\Utility;
class PhpWrapper implements \Nethgui\Log\LogConsumerInterface
{
private $log;
private $identifier;
public function __construct($identifier = __CLASS__)
{
$this->identifier = $identifier;
}
public function class_exists($className)
{
$warnings = array();
$this->wrapBegin($warnings, E_NOTICE);
$retval = class_exists($className);
$this->wrapEnd($warnings);
return $retval;
}
public function __call($name, $arguments)
{
$warnings = array();
$this->wrapBegin($warnings);
$exitCode = call_user_func_array($name, $arguments);
$this->wrapEnd($warnings);
return $exitCode;
}
protected function wrapBegin(&$messages, $forceErrno = 0)
{
set_error_handler(function ($errno, $errstr) use (&$messages, $forceErrno) {
$messages[] = array($forceErrno > 0 ? $forceErrno : $errno, $errstr);
}, E_WARNING | E_NOTICE);
}
protected function wrapEnd($messages)
{
restore_error_handler();
$log = $this->getLog();
if ($log instanceof \Nethgui\Log\Nullog) {
return;
}
if (count($messages) > 0) {
$message = '';
foreach ($messages as $msg) {
if ($msg[0] & E_WARNING) {
$level = 'warning';
} else {
$level = 'notice';
if(NETHGUI_DEBUG === FALSE) {
continue;
}
}
$log->$level(sprintf("%s: %s", $this->identifier, $msg[1]));
}
}
}
public function exec($command, &$output, &$retval)
{
$warnings = array();
$this->wrapBegin($warnings);
$lastLine = exec($command, $output, $retval);
$this->wrapEnd($warnings);
return $lastLine;
}
public function fsockopen($host, $port=-1, &$errno=NULL, &$errstr=NULL)
{
$warnings = array();
$this->wrapBegin($warnings);
$resource = fsockopen($host, $port, $errno, $errstr);
$this->wrapEnd($warnings);
return $resource;
}
public function phpInclude($filePath, $vars)
{
extract($vars, EXTR_REFS);
$_nethgui_warnings = array();
$this->wrapBegin($_nethgui_warnings);
$include = include($filePath);
$this->wrapEnd($_nethgui_warnings);
return $include;
}
public function phpExit($code)
{
exit($code);
}
public function phpCreateInstance($className, $constructorArgs)
{
$r = new \ReflectionClass($className);
return $r->newInstanceArgs($constructorArgs);
}
public function phpReadGlobalVariable($varName, $varIndex = NULL)
{
if (isset($varIndex)) {
return isset($GLOBALS[$varName], $GLOBALS[$varName][$varIndex]) ? $GLOBALS[$varName][$varIndex] : NULL;
}
return isset($GLOBALS[$varName]) ? $GLOBALS[$varName] : NULL;
}
public function phpWriteGlobalVariable($value, $varName, $varIndex = NULL)
{
if (isset($varIndex)) {
$GLOBALS[$varName][$varIndex] = $value;
} else {
$GLOBALS[$varName] = $value;
}
}
public function file_get_contents_extended($filePath, &$meta = NULL)
{
ob_start();
readfile($filePath);
if (is_array($meta)) {
$meta['size'] = ob_get_length();
}
return ob_get_clean();
}
public function getLog()
{
if ( ! isset($this->log)) {
$this->log = new \Nethgui\Log\Nullog();
}
return $this->log;
}
public function setLog(\Nethgui\Log\LogInterface $log)
{
$this->log = $log;
return $this;
}
}