jerowork / file-class-reflector
Get fully-qualified class names based on directory and file paths.
Installs: 1 091
Dependents: 2
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 1
Requires
- php: ^8.1
- nikic/php-parser: ^4.15
Requires (Dev)
- ergebnis/composer-normalize: ^2.15
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.4
- phpro/grumphp-shim: ^1.4
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.5 || ^10.0
- scrutinizer/ocular: ^1.8
README
Get fully-qualified classnames based on directory and file paths.
Installation
Install via Composer:
composer require jerowork/file-class-reflector
Usage
The ClassReflector
makes use of the nikic/php-parser
package to retrieve the fully-qualified class name from a file.
Basic usage:
use Jerowork\FileClassReflector\NikicParser\NikicParserClassReflectorFactory; // Create a new ClassReflector instance directly via a static factory method $reflector = NikicParserClassReflectorFactory::createInstance(); // Add necessary directories and/or files and reflect $reflector ->addDirectory(__DIR__ . '/some/directory') ->reflect(); // Get all \ReflectionClass found in files $classes = $reflector->getClasses();
The ClassReflectorFactory
can also be instantiated via the constructor.
In this way the factory can be added to a DI container.
use Jerowork\FileClassReflector\FileFinder\RegexIterator\RegexIteratorFileFinder; use Jerowork\FileClassReflector\NikicParser\NikicParserClassReflectorFactory; use PhpParser\NodeTraverser; use PhpParser\ParserFactory; // Create the factory $factory = new NikicParserClassReflectorFactory( new RegexIteratorFileFinder(), (new ParserFactory())->create(ParserFactory::PREFER_PHP7), new NodeTraverser(), ); // Create a new ClassReflector instance $reflector = $factory->create(); // ...
DI service definition
As a good practice we should always 'program to interfaces, not implementations', you should add this to your DI container.
PSR-11 Container example:
use Jerowork\FileClassReflector\ClassReflectorFactory; use Jerowork\FileClassReflector\FileFinder\FileFinder; use Jerowork\FileClassReflector\FileFinder\RegexIterator\RegexIteratorFileFinder; use Jerowork\FileClassReflector\NikicParser\NikicParserClassReflectorFactory; use PhpParser\NodeTraverser; use PhpParser\ParserFactory; use Psr\Container\ContainerInterface; return [ ClassReflectorFactory::class => static function (ContainerInterface $container): ClassReflectorFactory { return new NikicParserClassReflectorFactory( new RegexIteratorFileFinder(), (new ParserFactory())->create(ParserFactory::PREFER_PHP7), new NodeTraverser(), ); }, FileFinder::class => static fn (): FileFinder => new RegexIteratorFileFinder(), ];
Symfony YAML-file example:
services: _defaults: autowire: true autoconfigure: true Jerowork\FileClassReflector\ClassReflectorFactory: class: Jerowork\FileClassReflector\NikicParser\NikicParserClassReflectorFactory Jerowork\FileClassReflector\FileFinder\FileFinder: class: Jerowork\FileClassReflector\FileFinder\RegexIterator\RegexIteratorFileFinder PhpParser\ParserFactory: ~ PhpParser\Parser: factory: ['@PhpParser\ParserFactory', 'create'] arguments: [1] # 1 = ParserFactory::PREFER_PHP7 PhpParser\NodeTraverser: ~