phramz/doctrine-annotation-scanner

一个用于扫描文件和文件夹,查找使用 doctrine 注解的类的库

v1.0.1 2013-10-29 20:21 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:17:11 UTC


README

Annotation Scanner 是一个库,用于扫描文件和文件夹,查找使用 doctrine 注解的类。

安装

如果你使用 composer,那就很容易了!

编辑你的 composer.json

"require": {
    "phramz/doctrine-annotation-scanner": "dev-master"
}

或通过命令行

php composer.phar require phramz/doctrine-annotation-scanner

许可协议

此库采用 MIT 许可协议。有关更多信息,请参阅 LICENSE 文件。

已知限制

你的代码必须满足以下条件才能使此库正常工作

  • 遵循 PSR-0 规范。
    • 一个类文件恰好包含一个类。
    • 类文件以它包含的类的名称命名,并加上 .php 后缀。
  • 由于此库不会进行任何自动加载,因此确保所有类都自动加载或手动加载(例如,使用 require_once)的责任在于你。
  • 确保你通过 AnnotationRegistry 注册了你的自定义注解。有关详细信息,请参阅 Doctrine Common 文档。

示例

该库提供了两种不同的方式来查找注解类文件

  • 如果你只想找出哪些文件包含注解类,你可以利用 Finder
  • 如果你需要找到类文件,但还想访问具体的注解,那么 Scanner 就是你的好朋友。

现在,看看以下示例 ...

Finder

以下示例将在 /tests 下的任何类文件中查找包含 @Foo@Bar 注解的属性、方法或类文档。你可以通过遍历 Finder 实例来访问搜索结果。对于每个类文件,你将得到一个 Symfony\Component\Finder\SplFileInfo 实例。有关 Finder 的更多信息,请参阅 Symfony2 Finder 文档。

<?php

use Phramz\Doctrine\Annotation\Scanner\Finder;
use Doctrine\Common\Annotations\AnnotationReader;

$reader = new AnnotationReader(); // get an instance of the doctrine annotation reader
$finder = new Finder();
$finder->containsAtLeastOneOf('Phramz\Doctrine\Annotation\Fixtures\Annotations\Foo')
    ->containsAtLeastOneOf('Phramz\Doctrine\Annotation\Fixtures\Annotations\Bar')
    ->setReader($reader)
    ->in('/tests');

/** @var Symfony\Component\Finder\SplFileInfo $file */
foreach ($finder as $file) {
    echo "Found: " . $file->getFilename(); // will output for example "Found: AnnotatedClass.php"
}

Scanner

以下示例将在 /tests 下的任何类文件中查找包含 @Foo@Bar 注解的属性、方法或类文档。你可以通过遍历 Scanner 实例来访问搜索结果。对于每个类文件,你将得到一个继承自 Symfony\Component\Finder\SplFileInfoPhramz\Doctrine\Annotation\Scanner\ClassFileInfo 实例,它还提供了访问类注解的功能。

<?php

use Phramz\Doctrine\Annotation\Scanner\Scanner;
use Doctrine\Common\Annotations\AnnotationReader;

$reader = new AnnotationReader(); // get an instance of the doctrine annotation reader
$scanner = new Scanner($reader);

$scanner->scan(array(
        'Phramz\Doctrine\Annotation\Fixtures\Annotations\Foo',
        'Phramz\Doctrine\Annotation\Fixtures\Annotations\Bar'
    ))
    ->in('/tests');

/** @var Phramz\Doctrine\Annotation\Scanner\ClassFileInfo $file */
foreach ($scanner as $file) {
    echo "Found: " . $file->getFilename();    // will output for example "Found: AnnotatedClass.php"
    echo "Class: " . $file->getClassName();   // will output for example
                                              // "Class: Phramz\Annotation\AnnotatedClass"
    print_r($file->getClassAnnotations());    // will give you an array of all annotations
                                              // in the class-docblock
    print_r($file->getMethodAnnotations());   // will give you an array of all methods and
                                              // annotationss in the method-docblocks
    print_r($file->getPropertyAnnotations()); // will give you an array of all properties and
                                              // annotation in the method-docblocks
}

进一步阅读