componenta/reflection

Reflection helpers for Componenta

Maintainers

Package info

github.com/componenta/reflection

pkg:composer/componenta/reflection

Statistics

Installs: 7

Dependents: 7

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-06-15 11:05 UTC

This package is auto-updated.

Last update: 2026-06-15 12:06:53 UTC


README

Small reflection helper for Componenta internals.

Installation

composer require componenta/reflection

Requirements

  • PHP 8.4+

Related Packages

This package is standalone and is mostly an internal helper for Componenta libraries.

Package Why it uses reflection
componenta/di Reads parameters, properties, and attributes during dependency resolution.
componenta/interceptor Reads method attributes for attribute-based interceptors.
componenta/class-finder Attribute and inheritance filters can inspect discovered classes.
componenta/policy Attribute policies can be read through reflection providers or app compilation.

What It Provides

  • Cached reflection for classes, objects, closures, functions, methods, and invokable objects.
  • Attribute lookup helpers for a single reflector.
  • Deep attribute lookup across a class, its methods, properties, and constants.
  • WeakMap-backed metadata cache for attribute instances.

Reflecting Values

use Componenta\Reflection\Reflection;

$class = Reflection::class(App\Service\UserService::class);
$object = Reflection::object($service);
$closure = Reflection::callable(static fn(): string => 'ok');
$method = Reflection::callable([App\Service\UserService::class, 'handle']);

Reflection::reflect() accepts mixed input and returns the matching PHP reflector or null.

$reflector = Reflection::reflect($value);

Reading Attributes

use Componenta\Reflection\Reflection;

$class = Reflection::class(App\Command\CreatePostCommand::class);

$policies = Reflection::getMetadata($class, PermissionPolicy::class);
$policy = Reflection::getFirstMetadata($class, PermissionPolicy::class);
$hasPolicy = Reflection::hasMetadata($class, PermissionPolicy::class);

getMetadata() returns array<object>|null: null means no matching attributes.

Deep Attribute Lookup

$attributes = Reflection::getDeepMetadata($class, SomeAttribute::class);

foreach ($attributes as $path => $items) {
    // $path examples:
    // App\Command\CreatePostCommand
    // App\Command\CreatePostCommand::handle
    // App\Command\CreatePostCommand::$title
    // App\Command\CreatePostCommand::STATUS
}

Cache Management

Reflection::clearReflectors();

Use this in tests when a test mutates reflector state or needs isolation.