jasny / fqcn-reader
Library to extract PHP class name from source file
Installs: 5 234
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 2
Open Issues: 3
Requires
- php: >=7.1.0
Requires (Dev)
- jasny/php-code-quality: ^2.0
- phpstan/phpstan: ^0.10.2
- phpstan/phpstan-strict-rules: ^0.10.1
This package is auto-updated.
Last update: 2023-06-09 12:03:28 UTC
README
Library to extract fully qualified class name (FQCN) from a PHP source file.
Caveat; only considers one class per source file.
Installation
composer require jasny/fqcn-reader
Usage
Single source file
The FQCNReader
allows extracting a class name from a PHP source file.
use Jasny\FQCN\FQCNReader; $reader = new FQCNReader(); $class = $reader->getClass("path/to/source.php");
Interator
The FQCNIterator
is an OuterIterator
, meaning it will iterate
over an iterator applying logic. The iterator expects to traverse over source files.
With the file names in an array, use ArrayIterator
.
use Jasny\FQCN\FQCNIterator; $sourceFiles = glob('path/to/directory/*.php'); $sourceIterator = new ArrayIteractor($sourceFiles); $fqcnIterator = new FQCNIterator($sourceIterator); foreach ($fqcnIteractor as $file => $class) { // do something with $class }
Alternatively use SPL Iterators like DirectoryIterator
,
RecursiveDirectoryIterator
or
GlobIterator
.
use Jasny\FQCN\FQCNIterator; $directoryIterator = new RecursiveDirectoryIterator('path/to/project/'); $recursiveIterator = new RecursiveIteratorIterator($directoryIterator); $sourceIterator = new RegexIterator($recursiveIterator, '/^.+\.php$/i', RegexIterator::GET_MATCH); $fqcnIterator = new FQCNIterator($sourceIterator); foreach ($fqcnIteractor as $file => $class) { // do something with $class }
Files that do not define a class are skipped.