codememory / reflection

该库与内置的 Reflection API 类相似,但唯一的不同是,该库缓存整个结果,并且未来不再与内置的 Reflection 一起工作,这提高了性能

v3.0 2024-08-27 02:42 UTC

This package is auto-updated.

Last update: 2024-09-27 02:52:03 UTC


README

此库将使 PHP 的内置 Reflection API 感到满意,但增加了一个优点,它可以完全缓存

安装

$ composer require codememory/reflection

用法示例

<?php

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Codememory\Reflection\ReflectorManager;
use Attribute;

// Let's create a cache adapter
// Read more about the cache in the documentation https://symfony.com.cn/doc/current/components/cache.html
$cache = new FilesystemAdapter('codememory', directory: 'cache');

// Create a reflection manager
$rm = new ReflectorManager($cache, false); // The second argument specifies the application environment (isDev), dev mode is enabled by default

#[Attribute(Attribute::TARGET_PROPERTY)]
final class MyAttribute {
    public function __construct(
        public readonly mixed $value
    ) {}
}

// Let's create a class from which we will receive information
class First {
    private ?string $name = null;
    
    #[MyAttribute('example')]
    private ?string $surname = null;
}

// Get class reflection
$classReflector = $rm->getReflector(First::class); // Codememory\Reflection\Reflectors\ClassReflector

echo $classReflector->getName(); // First

结果

image info

缓存

0 第一次 a:12:{s:4:"name";s:5:"First";s:10:"short_name";s:5:"First";s:9:"namespace";s:0:"";s:11:"is_abstract";b:0;s:8:"is_final";b:0;s:11:"is_iterable";b:0;s:8:"is_trait";b:0;s:12:"is_interface";b:0;s:12:"is_anonymous";b:0;s:7:"methods";a:0:{}s:10:"properties";a:2:{i:0;a:5:{s:4:"name";s:4:"name";s:8:"modifier";i:4;s:4:"type";a:3:{s:4:"name";s:6:"string";s:14:"allow_nullable";b:1;s:10:"is_builtin";b:1;}s:13:"default_value";N;s:10:"attributes";a:0:{}}i:1;a:5:{s:4:"name";s:7:"surname";s:8:"modifier";i:4;s:4:"type";a:3:{s:4:"name";s:6:"string";s:14:"allow_nullable";b:1;s:10:"is_builtin";b:1;}s:13:"default_value";N;s:10:"attributes";a:1:{i:0;a:5:{s:4:"name";s:11:"MyAttribute";s:9:"arguments";a:1:{i:0;s:7:"example";}s:6:"target";i:8;s:11:"is_repeated";b:0;s:8:"instance";O:11:"MyAttribute":1:{s:5:"value";s:7:"example";}}}}}s:10:"attributes";a:0:{}}