jerowork/file-class-reflector

Get fully-qualified class names based on directory and file paths.

0.3.0 2023-02-23 19:07 UTC

This package is auto-updated.

Last update: 2024-12-19 16:04:46 UTC


README

Build Status Coverage Status Quality Score Software License Packagist Version PHP Version

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: ~