vaened / php-tag-finder

v1.0 2024-08-03 19:06 UTC

This package is auto-updated.

Last update: 2024-09-03 19:20:14 UTC


README

Build Status Software License

创建的库,用于简化基于父类/接口的PHP类的搜索。

安装

您可以使用composer安装此库。

composer require vaened/php-tag-finder

用法

在此库中,对tag的定义理解为接口抽象类

考虑到这一点,例如,要获取系统的所有handler,就足够搜索所有实现了Handler接口的PHP类。

$finder  = new ClassFinder(...);
$classes = $finder->instancesOf(Handler::class);

这将返回一个关联数组,其中键是磁盘上的物理文件位置,值是项目中所引用的类。

配置

配置包括如何创建ClassFinder,为此,我们必须理解2个概念,即搜索的source和表示该位置的namespace

  • source:将开始查找带标签类的位置。
  • namespace:在项目中对该位置的表示。

示例

假设项目名为Ecommerce,其命名空间也是Ecommerce\\

src
├── App
│ ├── Categories
│ │ └── CreateCategoryHandler.php
│ ├─── Products
│ │ └── CreateProductHandler.php
│ ├── Handler.php

因此,为了仅定位与应用程序对应的类,在这种情况下,您需要实例化ClassFinder以在项目的根目录/App中进行搜索,其命名空间等效。

$paths  = [$rootProyectPath, 'src', 'App'];
$finder = new ClassFinder(
    implode(DIRECTORY_SEPARATOR, $paths),
    'Ecommerce\\App'
);

许可

此库根据MIT许可证许可。有关更多信息,请参阅license文件。