ajout func::get_all(), call_all()
This commit is contained in:
parent
8cfcaae5b8
commit
6a41b72e95
@ -545,6 +545,92 @@ class func {
|
||||
else return cl::withn($value);
|
||||
}
|
||||
|
||||
const MASK_PS = ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC;
|
||||
const MASK_P = ReflectionMethod::IS_PUBLIC;
|
||||
const METHOD_PS = ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_STATIC;
|
||||
const METHOD_P = ReflectionMethod::IS_PUBLIC;
|
||||
|
||||
private static function match_name(string $name, array $includes, array $excludes): bool {
|
||||
if ($includes) {
|
||||
$matches = false;
|
||||
foreach ($includes as $include) {
|
||||
if (substr($include, 0, 1) == "/") {
|
||||
# expression régulière
|
||||
if (preg_match($include, $name)) {
|
||||
$matches = true;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
# tester la présence de la sous-chaine
|
||||
if (strpos($name, $include) !== false) {
|
||||
$matches = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$matches) return false;
|
||||
}
|
||||
foreach ($excludes as $exclude) {
|
||||
if (substr($exclude, 0, 1) == "/") {
|
||||
# expression régulière
|
||||
if (preg_match($exclude, $name)) return false;
|
||||
} else {
|
||||
# tester la présence de la sous-chaine
|
||||
if (strpos($name, $exclude) !== false) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return self[]
|
||||
*/
|
||||
static function get_all($class_or_object, ?array $params=null): array {
|
||||
$prefix = $params["prefix"] ?? "";
|
||||
$length = strlen($prefix);
|
||||
$args = $params["args"] ?? null;
|
||||
$staticOnly = $params["static_only"] ?? false;
|
||||
$includes = cl::with($params["include"] ?? null);
|
||||
$excludes = cl::with($params["exclude"] ?? null);
|
||||
|
||||
if (is_string($class_or_object)) {
|
||||
# lister les méthodes publiques statiques de la classe
|
||||
$mask = self::MASK_PS;
|
||||
$expected = self::METHOD_PS;
|
||||
$c = new ReflectionClass($class_or_object);
|
||||
} elseif (is_object($class_or_object)) {
|
||||
# lister les méthodes publiques de la classe
|
||||
$c = new ReflectionClass($class_or_object);
|
||||
$mask = $staticOnly? self::MASK_PS: self::MASK_P;
|
||||
$expected = $staticOnly? self::METHOD_PS: self::METHOD_P;
|
||||
} else {
|
||||
throw new ValueException("$class_or_object: vous devez spécifier une classe ou un objet");
|
||||
}
|
||||
$methods = [];
|
||||
foreach ($c->getMethods() as $m) {
|
||||
if (($m->getModifiers() & $mask) != $expected) continue;
|
||||
$name = $m->getName();
|
||||
if (substr($name, 0, $length) != $prefix) continue;
|
||||
if (!self::match_name($name, $includes, $excludes)) continue;
|
||||
$method = [$class_or_object, $name];
|
||||
$methods[] = self::with($method, $args);
|
||||
}
|
||||
return $methods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appeler toutes les méthodes publiques de $object_or_class et retourner un
|
||||
* tableau [$method_name => $return_value] des valeurs de retour.
|
||||
*/
|
||||
static function call_all($class_or_object, ?array $params=null) {
|
||||
$methods = self::get_all($class_or_object, $params);
|
||||
$values = [];
|
||||
foreach ($methods as $method) {
|
||||
$values[$method->getName()] = $method->invoke();
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
#############################################################################
|
||||
|
||||
protected function __construct(int $type, $func, ?array $args=null, bool $bound=false, ?string $reason=null) {
|
||||
@ -598,6 +684,10 @@ class func {
|
||||
|
||||
protected ?array $func;
|
||||
|
||||
function getName(): ?string {
|
||||
return $this->func[1] ?? null;
|
||||
}
|
||||
|
||||
protected bool $bound;
|
||||
|
||||
protected ?string $reason;
|
||||
|
Loading…
x
Reference in New Issue
Block a user