ahjdev/class-finder

Find all classes from composer

1.0.1 2024-04-25 11:44 UTC

This package is auto-updated.

Last update: 2024-04-25 12:07:48 UTC


README

Find all classes from composer

Table of Contents
  1. Installation
  2. Usage
    1. Get classes
    2. Filter classes
    3. Filter classes by self
    4. Easy Methods

Installation

Install the package using composer:

composer require ahjdev/class-finder

(back to top)

Usage

  • Create an Instance of ClassFinder (if you need to set path of vendor folder do it in __construct)
<?php
use AhjDev\ClassFinder\ClassFinder;

$finder = new ClassFinder();
$finder = new ClassFinder('dir/to/vendor');

Getting Classes

  • You can use getClasses method to get classes from namespace:
<?php
use AhjDev\ClassFinder\ClassFinder;

$finder = new ClassFinder;
$finder->getClasses('AhjDev');

Filtering

  • Filter result with FindType enum wich classes should return :
<?php

namespace AhjDev\ClassFinder;

enum FindType
{
    const INTERFACE = 1;
    const TRAIT     = 2;
    const ENUM      = 4;
    const FINAL     = 8;
    const ABSTRACT  = 16;
    const SIMPLE_CLASS  = 32;
    const CLASSES   = self::FINAL | self::ABSTRACT | self::SIMPLE_CLASS;
    const ALL       = self::INTERFACE | self::TRAIT | self::ENUM | self::CLASSES;
}
<?php
use AhjDev\ClassFinder\FindType;
use AhjDev\ClassFinder\ClassFinder;

$finder = new ClassFinder;
$finder->getClasses(
    'AhjDev',
    FindType::TRAIT | FindType::INTERFACE
);

Using Callbacks

<?php

use RefelctionClass;
use AhjDev\ClassFinder\FindType;
use AhjDev\ClassFinder\ClassFinder;

$finder = new ClassFinder;
$finder->getClasses(
    'AhjDev',
    FindType::TRAIT | FindType::INTERFACE,
    fn (RefelctionClass $v) => $v->isInteranl()
);

Easy methods

There is some easy methods that can use :

<?php
use AhjDev\ClassFinder\FindType;
use AhjDev\ClassFinder\ClassFinder;

$finder = new ClassFinder;
// Whether class is readonly
$finder->isReadonly('foo', FindType::ALL);

// Whether class use this trait
$finder->hasTraitClass('trait', 'foo', FindType::ABSTRACT);

// Whether class implements an interface
$finder->implementsClass('interface', 'foo', FindType::ENUM);

// Whether class is subclass of this class
$finder->isSubClassOf('parent', 'namespace', FindType::FINAL);

// Whether class is of this class or has this class as one of its parents
$finder->isAOf('class', 'namespace', FindType::FINAL);

// Just like `FindType` filters
$finder->isInterface(string $namespace);
$finder->isEnum(string $namespace);
$finder->isTrait(string $namespace);
$finder->isAbstract(string $namespace);
$finder->isFinalClasses(string $namespace);

(back to top)