microphp/Core/Reflection.php

161 lines
5.0 KiB
PHP

<?php
trait ReflectionHook
{
public static function __callStatic($name, $arguments): mixed
{
$formarly = "static" . ucfirst($name);
if(method_exists(__CLASS__, $formarly))
{
return (new static)->{$formarly}(...$arguments);
}
else if(method_exists(__CLASS__, "call$name"))
{
return (new static)->{"call$name"}($name, ...$arguments);
}
else
{
if(method_exists(__CLASS__, $name))
{
var_dump($callback);
exit;
}
try{
return static::{$name}(...$arguments);
}catch(Exception $e){
}
return null;
}
}
public function __call($name, $arguments)
{
$formarlyA = "call" . ucfirst($name);
$formarlyB = "static" . ucfirst($name);
if(method_exists($this, $formarlyA))
{
return $this->{$formarlyA}(...$arguments);
}else if(method_exists(__CLASS__, $formarlyB))
{
return (new static)->{$formarlyB}(...$arguments);
}
else if(method_exists(__CLASS__, "call$name"))
{
return (new static)->{"call$name"}($name, ...$arguments);
}
}
public function __get($name)
{
$formarly = ucfirst($name);
if(method_exists(__CLASS__, "get" . $formarly . "Attribute"))
{
return $this->{$formarly};
}
else if(method_exists(__CLASS__, "getAttribute"))
{
return $this->getAttribute($name);
}
else if(property_exists(__CLASS__, "_attributes"))
{
return $this->_attributes[$name] ?? null;
}
}
public function __set($name, $value)
{
$formarly = ucfirst($name);
if(method_exists($this, "get" . $formarly . "Attribute"))
{
$this->{$formarly} = $value;
}
else if(method_exists($this, "setAttribute"))
{
return $this->setAttribute($name, $value);
}
else if(property_exists($this, "_attributes") && isset($this->_attributes[$name]))
{
$this->_attributes[$name] = $value;
}
}
public function __isset($name)
{
$formarly = ucfirst($name);
if(method_exists($this, "get" . $formarly . "Attribute"))
{
return true;
}
else
{
return isset($this->_attributes[$name]);
}
}
public function __unset($name)
{
$formarly = ucfirst($name);
if(method_exists($this, "set" . $formarly . "Attribute"))
{
$this->{"set" . $formarly . "Attribute"}();
}
else if(method_exists($this, "setAttribute"))
{
return $this->setAttribute($name, null);
}
else if(method_exists($this, "remoteAttribute"))
{
return $this->remoteAttribute($name);
}
else if(property_exists($this, "_attributes") && isset($this->_attributes[$name]))
{
unset($this->_attributes[$name]);
}
}
public function __invoke(...$args)
{
if(method_exists($this, "reflectionCall"))
{
return $this->reflectionCall(...$args);
}
else
{
if(property_exists(__CLASS__, "reflectionCallException"))
{
if(!(new static)->reflectionCallException)
{
return;
}
}
throw new Exception("\"".__CLASS__."()\" not callable");
}
}
public function __toString()
{
if(method_exists($this, "toString"))
{
return $this->toString();
}
else
{
if(property_exists(__CLASS__, "reflectionCallException"))
{
if(!(new static)->reflectionCallException)
{
return "";
}
}
throw new Exception("\"".__CLASS__."()\" not callable");
}
}
public function __serialize()
{
if(method_exists($this, "getProperties"))
{
return $this->getProperties();
}
}
public function __unserialize($datas)
{
if(method_exists($this, "setProperties"))
{
$this->setProperties($datas);
}
}
}