ClassFileLocator

Zend\File\ClassFileLocator is a PHP FilterIterator for use with locating files containing PHP classes, interfaces, abstracts, or traits. As such, it should be used in conjunction with a DirectoryIterator or RecursiveDirectoryIterator.

Use cases include building class maps for autoloading.

Usage

The ClassFileLocator constructor can take one of:

In each case, once constructed, iteration will result in a list of files containing PHP clases, interfaces, abstracts, or traits.

Instead of returning standard SplFileInfo instances, the ClassFileLocator is configured to cast to Zend\File\PhpClassFile instances, which extend SplFileInfo, and provide the following additional methods:

Tokenization

The ClassFileLocator uses the tokenizer extension in order to locate items of interest; as such, its operations will not execute PHP files it finds.

Example

The following will spit out a PHP file that returns a class map for the src/ directory in which it is run:

<?php
use Zend\File\ClassFileLocator;

$path = realpath(getcwd() . '/src');

$locator = new ClassFileLocator($path);
$map = [];

foreach ($locator as $file) {
    $filename = str_replace($path . '/', '', $file->getRealPath());
    foreach ($file->getClasses() as $class) {
        $map[$class] = $filename;
    }
}

printf("<?php\nreturn %s;", var_export($map, true));