PHP code example of brenoroosevelt / flex-fqcn-finder

1. Go to this page and download the library: Download brenoroosevelt/flex-fqcn-finder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brenoroosevelt / flex-fqcn-finder example snippets



use FlexFqcnFinder\Fqcn;
use FlexFqcnFinder\Filter\Filter;

$recursive = true;

$fqcns = 
    Fqcn::new()
        ->addDirectory('/path/to/dir1', $recursive)
        ->addDirectory('/path/to/dir2', !$recursive)
        ->withFilter(
            Filter::by() // or: Filter::anyOf()
                ->implementsInterface('MyInterface')
                ->hasMethod('method')
        )
        ->


namespace FlexFqcnFinder;

interface FqcnFinderInterface
{
    /**
     * @return string[] The fully qualified class names found
     */
    public function find(): array;
}


use FlexFqcnFinder\FqcnFinderComposite;
use FlexFqcnFinder\Finder\GetDeclaredClasses;
use FlexFqcnFinder\Finder\FqcnFinder;
use FlexFqcnFinder\Repository\FilesFromDir;

$myFinder = new FqcnFinderComposite(
    new GetDeclaredClasses(),
    new FqcnFinder(new FilesFromDir('path/to/dir1')),
    new FqcnFinder(new FilesFromDir('path/to/dir2'))
);

$fqcns = $myFinder->find();


use FlexFqcnFinder\Finder\Decorator\CachedFqcnFinder;
use FlexFqcnFinder\Finder\Decorator\FilteringFqcnFinder;
use FlexFqcnFinder\Filter\Specifications\IsSubClassOf;

$myFinder = /* any finder, finder composition, ... */;

$filtered = new FilteringFqcnFinder(
    $myFinder,
    new IsSubClassOf('MyBaseClass')
);

// decorating again
$cached = new CachedFqcnFinder($filtered, new MyPsr16Cache(), 'cacheKey');

$fqcns = $cached->find();


use FlexFqcnFinder\Finder\FqcnFinder;
use FlexFqcnFinder\Repository\FilesFromDir;
use FlexFqcnFinder\Finder\Decorator\FilteringFqcnFinder;
use FlexFqcnFinder\Filter\Specifications\IsSubClassOf;

$filtered = new FilteringFqcnFinder(
    new FqcnFinder(new FilesFromDir(__DIR__)),  //  first param: decorated Finder
    new IsSubClassOf('MyBaseClass')             // second param: filters to apply
);

// Or chaining filters:

$filtered = new FilteringFqcnFinder(
    new FqcnFinder(new FilesFromDir(__DIR__)),
    Filter::anyOf()
        ->implementsInterface('MyInterface')
        ->hasMethod('execute')
        ->and(
            Filter::by()
                ->usingTrait('MyTrait')
                ->apply(function($fqcn) {
                    return $fqcn === 'my_condition';
                })
        )
);

$fqcns = $filtered->find();


namespace Foo;

use FlexFqcnFinder\Filter\FqcnSpecification;

MyFilter implements FqcnSpecification
{
    public function isSatisfiedBy(string $fqcn): bool
    {
        return $fqcn === /* ... */;
    }
}


use FlexFqcnFinder\Finder\FqcnFinder;
use FlexFqcnFinder\Repository\FilesFromDir;
use FlexFqcnFinder\Finder\Decorator\FilteringFqcnFinder;
use namespace Foo\MyFilter;

$filtered = new FilteringFqcnFinder(
    new FqcnFinder(new FilesFromDir(__DIR__)),
    new MyFilter()
);

$fqcns = $filtered->find();