Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 3 months 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 (over 1 year ago)
- Last Synced: 2024-08-15T00:58:47.754Z (5 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
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fjerowork%2Ffile-class-reflector%2Fbadge%3Fref%3Dmain&style=flat-square)](https://github.com/jerowork/file-class-reflector/actions)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/jerowork/file-class-reflector.svg?style=flat-square)](https://scrutinizer-ci.com/g/jerowork/file-class-reflector/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/g/jerowork/file-class-reflector.svg?style=flat-square)](https://scrutinizer-ci.com/g/jerowork/file-class-reflector)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Packagist Version](https://img.shields.io/packagist/v/jerowork/file-class-reflector.svg?style=flat-square&include_prereleases)](https://packagist.org/packages/jerowork/file-class-reflector)
[![PHP Version](https://img.shields.io/badge/php-%5E8.1-8892BF.svg?style=flat-square)](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: ~
```