Add package outputs, colorize motd

This commit is contained in:
crazywhalecc
2025-12-09 16:34:43 +08:00
parent ac01867e9c
commit b0f630f95f
8 changed files with 98 additions and 26 deletions

View File

@@ -26,6 +26,10 @@ class CallbackInvoker
* 4. Default value
* 5. Null (if nullable)
*
* Note: For object values in context, the invoker automatically registers
* the object under all its parent classes and interfaces, allowing type hints
* to match any type in the inheritance hierarchy.
*
* @param callable $callback The callback to invoke
* @param array $context Context parameters (type => value or name => value)
*
@@ -35,6 +39,9 @@ class CallbackInvoker
*/
public function invoke(callable $callback, array $context = []): mixed
{
// Expand context to include all parent classes and interfaces for objects
$context = $this->expandContextHierarchy($context);
$reflection = new \ReflectionFunction(\Closure::fromCallable($callback));
$args = [];
@@ -95,4 +102,43 @@ class CallbackInvoker
'void', 'null', 'false', 'true', 'never',
], true);
}
/**
* Expand context to include all parent classes and interfaces for object values.
* This allows type hints to match any type in the object's inheritance hierarchy.
*
* @param array $context Original context array
* @return array Expanded context with all class hierarchy mappings
*/
private function expandContextHierarchy(array $context): array
{
$expanded = [];
foreach ($context as $key => $value) {
// Keep the original key-value pair
$expanded[$key] = $value;
// If value is an object, add mappings for all parent classes and interfaces
if (is_object($value)) {
$reflection = new \ReflectionClass($value);
// Add concrete class
$expanded[$reflection->getName()] = $value;
// Add all parent classes
while ($parent = $reflection->getParentClass()) {
$expanded[$parent->getName()] = $value;
$reflection = $parent;
}
// Add all interfaces
$interfaces = (new \ReflectionClass($value))->getInterfaceNames();
foreach ($interfaces as $interface) {
$expanded[$interface] = $value;
}
}
}
return $expanded;
}
}