Reflection

Type Finder

PHP does not provide any native way of finding types (eg classes and interfaces) from files unless you've already autoloaded them into memory. Aphiria's TypeFinder can do just that, though. You simply pass a directory or list of directories, and it will scan the directories for any types defined within. It does this by tokenizing all PHP files and scanning for type definitions.

Finding Classes

Let's look at an example that finds all classes defined in a particular path.

use Aphiria\Reflection\TypeFinder;

$typeFinder = new TypeFinder();
$classes = $typeFinder->findAllClasses('PATH_TO_SCAN');

Or, pass in a list of directories:

$classes = $typeFinder->findAllClasses(['PATH1_TO_SCAN', 'PATH2_TO_SCAN']);

You can also recursively scan the input directories:

$classes = $typeFinder->findAllClasses('PATH_TO_SCAN', true);

By default, abstract classes are not returned in the results, but you may include them:

$classes = $typeFinder->findAllClasses('PATH_TO_SCAN', false, true);

Finding Interfaces

Just like you can scan for interfaces, you may also scan for interfaces.

$interfaces = $typeFinder->findAllInterfaces('PATH_TO_SCAN');

Or recursively:

$interfaces = $typeFinder->findAllInterfaces('PATH_TO_SCAN', true);

Finding All Types

If you're simply trying to grab all types, regardless of whether they're a class, abstract class, or interface, there's a method for that:

$types = $typeFinder->findAllTypes('PATH_TO_SCAN');

Or recursively:

$types = $typeFinder->findAllTypes('PATH_TO_SCAN', true);

Finding Sub Types

You can grab all sub-types of a parent type:

$subTypesOfFoo = $typeFinder->findAllSubtypesOfType(Foo::class, 'PATH_TO_SCAN');

Or recursively:

$subTypesOfFoo = $typeFinder->findAllSubtypesOfType(Foo::class, 'PATH_TO_SCAN', true);