https://github.com/jerowork/file-class-reflector
Get fully-qualified class names based on directory and file paths.
https://github.com/jerowork/file-class-reflector
class-name fqcn reflection
Last synced: about 1 month ago
JSON representation
Get fully-qualified class names based on directory and file paths.
- Host: GitHub
- URL: https://github.com/jerowork/file-class-reflector
- Owner: jerowork
- License: mit
- Created: 2021-08-29T08:56:11.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-19T12:59:09.000Z (almost 2 years ago)
- Last Synced: 2025-03-24T09:41:26.638Z (2 months ago)
- Topics: class-name, fqcn, reflection
- Language: PHP
- Homepage:
- Size: 102 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# file-class-reflector
[](https://github.com/jerowork/file-class-reflector/actions)
[](https://scrutinizer-ci.com/g/jerowork/file-class-reflector/code-structure)
[](https://scrutinizer-ci.com/g/jerowork/file-class-reflector)
[](LICENSE)
[](https://packagist.org/packages/jerowork/file-class-reflector)
[](http://www.php.net)Get fully-qualified classnames based on directory and file paths.
## Installation
Install via [Composer](https://getcomposer.org):
```bash
composer require jerowork/file-class-reflector
```## Usage
The `ClassReflector` makes use of the [nikic/php-parser](https://github.com/nikic/php-parser)
package to retrieve the fully-qualified class name from a file.Basic usage:
```php
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.
```php
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:
```php
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:
```yaml
services:
_defaults:
autowire: true
autoconfigure: trueJerowork\FileClassReflector\ClassReflectorFactory:
class: Jerowork\FileClassReflector\NikicParser\NikicParserClassReflectorFactoryJerowork\FileClassReflector\FileFinder\FileFinder:
class: Jerowork\FileClassReflector\FileFinder\RegexIterator\RegexIteratorFileFinderPhpParser\ParserFactory: ~
PhpParser\Parser:
factory: ['@PhpParser\ParserFactory', 'create']
arguments: [1] # 1 = ParserFactory::PREFER_PHP7PhpParser\NodeTraverser: ~
```