/** * Main * * @param string[] args */ public static function main(array $args) { $way = array_shift($args); $argc = sizeof($args); // Read sourcecode from STDIN if no further argument is given if (0 === $argc) { $code = new Code(file_get_contents('php://stdin')); } else { if ('--' === $args[0]) { $code = new Code(file_get_contents('php://stdin')); } else { $code = new Code($args[0]); } } // Perform $argv = [XPClass::nameOf(self::class)] + $args; $return = eval($code->head() . $code->expression()); switch ($way) { case '-w': Console::writeLine($return); break; case '-d': var_dump($return); break; } }
/** * Main * * @param string[] args */ public static function main(array $args) { $argc = sizeof($args); // Read sourcecode from STDIN if no further argument is given if (0 === $argc) { $code = new Code(file_get_contents('php://stdin')); } else { if ('--' === $args[0]) { $code = new Code(file_get_contents('php://stdin')); } else { $code = new Code($args[0]); } } // Perform $argv = [XPClass::nameOf(__CLASS__)] + $args; return eval($code->head() . $code->fragment()); }
public function __call($name, $args) { $t = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4); $c = PHP_VERSION >= '7.0.0' || defined('HHVM_VERSION'); $self = $t[1 - $c]['class']; $scope = isset($t[2 - $c]['class']) ? $t[2 - $c]['class'] : $t[3 - $c]['class']; // Check scope for extension methods if (null != $scope && isset(\xp::$ext[$scope])) { foreach (\xp::$ext[$scope] as $type => $class) { if (!$this instanceof $type || !method_exists($class, $name)) { continue; } array_unshift($args, $this); return call_user_func_array([$class, $name], $args); } } throw new \lang\Error('Call to undefined method ' . \lang\XPClass::nameOf($self) . '::' . $name . '() from scope ' . \lang\XPClass::nameOf($scope)); }
/** * Main * * @param string[] $args * @return int */ public static function main(array $args) { // Read sourcecode from STDIN if no further argument is given if (empty($args)) { $code = new Code(file_get_contents('php://stdin')); } else { if ('--' === $args[0]) { $code = new Code(file_get_contents('php://stdin')); } else { if (is_file($args[0])) { $code = new Code(file_get_contents($args[0])); } else { $code = new Code($args[0]); } } } // Perform $argv = [XPClass::nameOf(self::class)] + $args; return eval($code->head() . $code->fragment()); }
/** * Invokes the underlying method represented by this Method object, * on the specified object with the specified parameters. * * Example: * ```php * $method= XPClass::forName('lang.Object')->getMethod('toString'); * $str= $method->invoke(new Object()); * ``` * * Example (passing arguments) * ```php * $method= XPClass::forName('lang.types.String')->getMethod('concat'); * $str= $method->invoke(new String('Hello'), ['World']); * ``` * * Example (static invokation): * ```php * $method= XPClass::forName('util.log.Logger')->getMethod('getInstance'); * $log= $method->invoke(null); * ``` * * @param lang.Object $obj * @param var[] $args default [] * @return var * @throws lang.IllegalArgumentException in case the passed object is not an instance of the declaring class * @throws lang.IllegalAccessException in case the method is not public or if it is abstract * @throws lang.reflect.TargetInvocationException for any exception raised from the invoked method */ public function invoke($obj, $args = []) { if (null !== $obj && !$obj instanceof $this->_class) { throw new IllegalArgumentException(sprintf('Passed argument is not a %s class (%s)', XPClass::nameOf($this->_class), \xp::typeOf($obj))); } // Check modifiers. If caller is an instance of this class, allow // protected method invocation (which the PHP reflection API does // not). $m = $this->_reflect->getModifiers(); if ($m & MODIFIER_ABSTRACT) { throw new IllegalAccessException(sprintf('Cannot invoke abstract %s::%s', XPClass::nameOf($this->_class), $this->_reflect->getName())); } $public = $m & MODIFIER_PUBLIC; if (!$public && !$this->accessible) { $t = debug_backtrace(0, 2); $decl = $this->_reflect->getDeclaringClass()->getName(); if ($m & MODIFIER_PROTECTED) { $allow = $t[1]['class'] === $decl || is_subclass_of($t[1]['class'], $decl); } else { $allow = $t[1]['class'] === $decl; } if (!$allow) { throw new IllegalAccessException(sprintf('Cannot invoke %s %s::%s from scope %s', Modifiers::stringOf($this->getModifiers()), XPClass::nameOf($this->_class), $this->_reflect->getName(), $t[1]['class'])); } } try { if (!$public) { $this->_reflect->setAccessible(true); } return $this->_reflect->invokeArgs($obj, (array) $args); } catch (\lang\SystemExit $e) { throw $e; } catch (\lang\Throwable $e) { throw new TargetInvocationException(XPClass::nameOf($this->_class) . '::' . $this->_reflect->getName(), $e); } catch (\Exception $e) { throw new TargetInvocationException(XPClass::nameOf($this->_class) . '::' . $this->_reflect->getName(), new \lang\XPException($e->getMessage())); } catch (\Throwable $e) { throw new TargetInvocationException(XPClass::nameOf($this->_class) . '::' . $this->_reflect->getName(), new \lang\Error($e->getMessage())); } }
/** * Retrieve return type * * @return string */ public function getReturnTypeName() { return XPClass::nameOf($this->_class); }
/** * Changes the value of the field represented by this Field, on the * specified object. * * @param lang.Object instance * @param var value * @throws lang.IllegalArgumentException in case the passed object is not an instance of the declaring class * @throws lang.IllegalAccessException in case this field is not public */ public function set($instance, $value) { if (null !== $instance && !$instance instanceof $this->_class) { throw new IllegalArgumentException(sprintf('Passed argument is not a %s class (%s)', XPClass::nameOf($this->_class), \xp::typeOf($instance))); } // Check modifiers. If caller is an instance of this class, allow // protected method invocation (which the PHP reflection API does // not). $m = $this->_reflect->getModifiers(); $public = $m & MODIFIER_PUBLIC; if (!$public && !$this->accessible) { $t = debug_backtrace(0); $decl = $this->_reflect->getDeclaringClass()->getName(); if ($m & MODIFIER_PROTECTED) { $allow = $t[1]['class'] === $decl || is_subclass_of($t[1]['class'], $decl); } else { $allow = $t[1]['class'] === $decl; } if (!$allow) { throw new IllegalAccessException(sprintf('Cannot write %s %s::$%s from scope %s', Modifiers::stringOf($this->getModifiers()), XPClass::nameOf($this->_class), $this->_reflect->getName(), $t[1]['class'])); } } try { $public || $this->_reflect->setAccessible(true); $this->_reflect->setValue($instance, $value); } catch (\lang\Throwable $e) { throw $e; } catch (\Exception $e) { throw new \lang\XPException($e->getMessage()); } }