Skip to content

Commit b8ea4a5

Browse files
committed
cache get object props
1 parent 7b46f0d commit b8ea4a5

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

src/functions.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -505,24 +505,33 @@ function has_used_trait(string|object $object, string $trait, bool $autoload = t
505505

506506
if (!function_exists('\Windwalker\get_object_dump_props')) {
507507
/**
508-
* @param object $object
509-
* @param int|null $filter
508+
* @param object|string $object
509+
* @param int|null $filter
510510
*
511511
* @return array<\ReflectionProperty>
512+
* @throws \ReflectionException
512513
*/
513514
function get_object_dump_props(
514-
object $object,
515+
object|string $object,
515516
?int $filter = null
516517
): array {
518+
static $cache = [];
519+
$isObject = is_object($object);
520+
$key = ($isObject ? get_class($object) : $object) . '|' . $filter;
521+
522+
if (isset($cache[$key])) {
523+
return $cache[$key];
524+
}
525+
517526
$values = [];
518527

519-
$ref = new \ReflectionObject($object);
528+
$ref = new \ReflectionClass($object);
520529
$props = $ref->getProperties($filter);
521530

522531
foreach ($props as $prop) {
523532
$name = $prop->getName();
524533

525-
if (!$prop->isInitialized($object)) {
534+
if ($isObject && !$prop->isInitialized($object)) {
526535
continue;
527536
}
528537

@@ -542,7 +551,7 @@ function get_object_dump_props(
542551
$values[$name] = $prop;
543552
}
544553

545-
return $values;
554+
return $cache[$key] = $values;
546555
}
547556
}
548557

0 commit comments

Comments
 (0)