jlabno/annotations-scanner

注释文件扫描器 - 查找带有提供注释的方法,输出文件路径或目录的数组。

v1.2.1 2022-07-20 13:20 UTC

This package is auto-updated.

Last update: 2024-09-18 12:45:22 UTC


README

扫描器查找带有提供注释的方法,输出文件路径或目录的数组。

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

安装

步骤 1:使用 composer 下载库

通过 composer 需求包

composer require jlabno/annotations-scanner

步骤 2:如何使用

默认情况下,库使用 Apcu 缓存,您可以通过传递自己的 Psr\SimpleCache 缓存实现。

<?php

declare(strict_types=1);

use AnnotationsScanner\Scanner\ScannerFactory;
use AnnotationsScanner\Scanner\ScanResult;
use MyAnnotations\YourAnnotation;
use MyAnnotations\AnotherAnnotation;

$scanner = ScannerFactory::createWithDefaultReader(
    '/app',
    YourAnnotation::class,
    AnotherAnnotation::class,
);

/**
 * @returns ScanResult
 */
$result = $scanner->scan();

$foundFilePaths = $result->getFilePaths();
//['/app/src/classes/MyClass.php', '/app/src/classes/deeper/MyAnotherClass.php']

$foundDirectories = $result->getFileDirs();
//['/app/src/classes', '/app/src/classes/deeper']

2. 可选功能

您可以使用实现 Psr\SimpleCache 的自己的缓存。

$cache = new \Symfony\Component\Cache\Psr16Cache(new \Symfony\Component\Cache\Adapter\NullAdapter());
$scanner = ScannerFactory::createWithDefaultReaderAndCache('/app', $cache);

您可以使用符合 Doctrine\Common\Annotations\Reader 的自己的注释读取器

$scanner = ScannerFactory::createWithAnnotationReader( '/app', $reader);

或自定义缓存

$scanner = ScannerFactory::createWithAnnotationReaderAndCache( '/app', $reader, $cache);