齿轮/类查找器

该包已被弃用,不再维护。没有建议的替代包。

实用类,帮助您根据 composer 自动加载器发现其他类/命名空间。

v1.0.2 2018-03-28 10:37 UTC

This package is not auto-updated.

Last update: 2020-01-24 16:37:22 UTC


README

正在寻找维护者,我现在几乎不做PHP开发,我已经转向其他领域,现在主要工作在 dotnet core、node.js & golang 上。如果有人有兴趣接管这些项目,请与我联系 - brad@bjc.id.au

类查找器,类似于 Symfony\Finder,但用于类。

Build Status Latest Stable Version Total Downloads License Coverage Status Scrutinizer Code Quality

实用类,帮助您根据 composer 自动加载器发现其他类/命名空间。

如何安装

通过 composer 安装很简单

composer require gears/class-finder

示例

// Make sure you grab the composer ClassLoader instance, the class finder needs it.
$composer = require('vendor/autoload.php');

// Create a new finder. You may reuse this as much as you like.
// Right now caching is not performed but could be in the future.
$finder = new Gears\ClassFinder($composer);

// Find all classes inside a namespace
$classes = $finder->namespace('Foo\\Bar')->search();

// Returns an array like:
$classes =
[
    '/home/user/project/vendor/foo/src/Bar/Baz.php' => 'Foo\\Bar\\Baz',
    '/home/user/project/vendor/foo/src/Bar/Qux.php' => 'Foo\\Bar\\Qux',
    'etc...'
];

// Find all classes inside a namespace that implement an interface.
$classes = $finder->namespace('Foo\\Bar')->implements('SomeInterface')->search();

// OR you can use the PHP 5.5 ::class operator
$classes = $finder->namespace('Foo\\Bar')->implements(SomeInterface::class)->search();

// Or filter by parent classes
$classes = $finder->namespace('Foo\\Bar')->extends(SomeParent::class)->search();

// NOTE: You can't do both out of the box.
$classes = $finder->namespace('Foo\\Bar')

// This is now allowed!
->implements(SomeInterface::class)
->extends(SomeParent::class)

->search();

// Although you could supply your own custom filter that implemented whatever filtering you like.
$classes = $finder->namespace('Foo\\Bar')->filterBy(function($rClass ReflectionClass)
{
    
    /* custom logic goes here, must return true or false */
    
})->search();

// ClassFinder also implements the IteratorAggregate & Countable interfaces.
$number = $finder->namespace('Foo\\Bar')->count();

foreach ($finder->namespace('Foo\\Bar') as $filepath => $fqcn)
{
    
}

由 Brad Jones 开发 - brad@bjc.id.au