1. Go to this page and download the library: Download bermudaphp/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/ */
bermudaphp / finder example snippets
use Bermuda\ClassFinder\ClassFinder;
// Create finder instance
$finder = new ClassFinder();
// Find all PHP classes in directories
$classes = $finder->find(['src/', 'app/']);
foreach ($classes as $filename => $classInfo) {
echo "Found: $classInfo->fullQualifiedName in $filename\n";
}
use Bermuda\ClassFinder\ClassFinder;
use Bermuda\ClassFinder\Filter\{
InstantiableFilter,
ImplementsFilter,
AttributeSearchFilter
};
// Find instantiable classes that implement specific interfaces
$finder = new ClassFinder([
new InstantiableFilter(),
new ImplementsFilter(['Serializable', 'Countable']),
new AttributeSearchFilter(['Route'])
]);
$results = $finder->find('src/');
use Bermuda\ClassFinder\Filter\InstantiableFilter;
$filter = new InstantiableFilter();
// Matches: class UserService { ... }
// Excludes: abstract class BaseService { ... }
use Bermuda\ClassFinder\Filter\IsAbstractFilter;
$filter = new IsAbstractFilter();
// Matches: abstract class BaseController { ... }
use Bermuda\ClassFinder\Filter\IsFinalFilter;
$filter = new IsFinalFilter();
// Matches: final class UserService { ... }
use Bermuda\ClassFinder\Filter\ImplementsFilter;
// Single interface
$filter = new ImplementsFilter('Serializable');
// Multiple interfaces (ALL le', 'Countable']);
use Bermuda\ClassFinder\Filter\SubclassFilter;
$filter = new SubclassFilter('App\\Controller\\AbstractController');
// Finds all classes extending AbstractController
use Bermuda\ClassFinder\Filter\CallableFilter;
$filter = new CallableFilter();
// Matches: class MyClass { public function __invoke() { ... } }
use Bermuda\ClassFinder\Filter\PatternFilter;
// Search in class names
$filter = new PatternFilter('*Controller'); // Classes ending with "Controller"
$filter = new PatternFilter('Abstract*'); // Classes starting with "Abstract"
$filter = new PatternFilter('*Test*'); // Classes containing "Test"
// Search in namespaces
$filter = new PatternFilter('App\\Controllers\\*'); // Classes in Controllers namespace
// Search in full qualified names
$filter = new PatternFilter('*\\Api\\*Controller'); // API controllers
// Static helpers for common patterns
$filter = PatternFilter::exactMatch('UserController');
$filter = PatternFilter::contains('Service');
$filter = PatternFilter::startsWith('Abstract');
$filter = PatternFilter::endsWith('Controller');
$filter = PatternFilter::namespace('App\\Services');
// Example class with attributes at different levels
class UserController
{
#[Inject]
private UserService $userService;
#[Route('/users')]
#[Auth('admin')]
public function index(): Response
{
// method implementation
}
#[Deprecated]
public const STATUS_ACTIVE = 1;
}
use Bermuda\ClassFinder\Filter\AttributeSearchFilter;
// Basic search - only class-level attributes
$filter = new AttributeSearchFilter(['Route', 'Controller']);
// Deep search - h
$filter = new AttributeSearchFilter(['Route', '*Test*', 'Api*'], deepSearch: true);
// AND logic with deep search - must have ALL attributes (anywhere in class)
$filter = new AttributeSearchFilter(['Route', 'Auth'], matchAll: true, deepSearch: true);
// Will find UserController because it has both #[Route] and #[Auth] on methods
// Comparison of search scopes:
// Without deep search - only finds classes with Route attribute on class declaration
$classOnlyFilter = new AttributeSearchFilter(['Route'], deepSearch: false);
// With deep search - finds classes with Route on class OR on any method/property/constant
$deepFilter = new AttributeSearchFilter(['Route'], deepSearch: true);
// Static helpers with deep search
$filter = AttributeSearchFilter::hasAttribute('Inject', deepSearch: true);
$filter = AttributeSearchFilter::hasAnyAttribute(['Route', 'Controller'], deepSearch: true);
$filter = AttributeSearchFilter::hasAllAttributes(['Route', 'Middleware'], deepSearch: true);
use Bermuda\ClassFinder\Filter\AttributePatternFilter;
// Basic pattern matching - only class-level attributes
$filter = new AttributePatternFilter('*Route*');
$filter = new AttributePatternFilter('Api*');
// Deep search - serController because $userService property has #[Inject]
// Comparison of search scopes:
// Without deep search - only finds classes with Http* attributes on class
$classOnlyFilter = new AttributePatternFilter('Http*', deepSearch: false);
// With deep search - finds classes with Http* on class OR members
$deepFilter = new AttributePatternFilter('Http*', deepSearch: true);
// Optimized static helpers with deep search
$filter = AttributePatternFilter::exactAttribute('HttpGet', deepSearch: true);
$filter = AttributePatternFilter::anyAttribute(['Route', 'HttpGet'], deepSearch: true);
$filter = AttributePatternFilter::attributePrefix('Http', deepSearch: true);
use Bermuda\Filter\ChainableFilter;
use Bermuda\ClassFinder\Filter\{
InstantiableFilter,
PatternFilter,
AttributeSearchFilter
};
// All conditions must be true
$chainFilter = new ChainableFilter([
new InstantiableFilter(), // AND: must be instantiable
new PatternFilter('*Controller'), // AND: must end with "Controller"
new AttributeSearchFilter(['Route']) // AND: must have Route attribute
]);
$finder = new ClassFinder([$chainFilter]);
$strictControllers = $finder->find('src/Controllers/');
use Bermuda\Filter\OneOfFilter;
use Bermuda\ClassFinder\Filter\{
PatternFilter,
AttributeSearchFilter,
ImplementsFilter
};
// Any condition can be true
$orFilter = new OneOfFilter([
new PatternFilter('*Controller'), // OR: ends with "Controller"
new PatternFilter('*Service'), // OR: ends with "Service"
new AttributeSearchFilter(['Component']), // OR: has Component attribute
new ImplementsFilter('App\\Contracts\\HandlerInterface') // OR: implements HandlerInterface
]);
$finder = new ClassFinder([$orFilter]);
$flexibleResults = $finder->find('src/');
use Bermuda\Filter\{ChainableFilter, OneOfFilter};
use Bermuda\ClassFinder\Filter\{
InstantiableFilter,
PatternFilter,
AttributeSearchFilter,
ImplementsFilter
};
// Complex logic: Must be instantiable AND (Controller OR Service OR has Route attribute)
$complexFilter = new ChainableFilter([
new InstantiableFilter(), // Must be instantiable
new OneOfFilter([ // AND any of these:
new PatternFilter('*Controller'), // - ends with "Controller"
new PatternFilter('*Service'), // - ends with "Service"
new AttributeSearchFilter(['Route']) // - has Route attribute
])
]);
$finder = new ClassFinder([$complexFilter]);
$results = $finder->find('src/');
use Bermuda\ClassFinder\ClassFinder;
use Bermuda\ClassFinder\Filter\{
InstantiableFilter,
PatternFilter,
AttributeSearchFilter
};
$finder = new ClassFinder([
new InstantiableFilter(), // Only instantiable classes
new PatternFilter('*Controller'), // Class names ending with "Controller"
new AttributeSearchFilter(['Route']) // Must have Route attribute
]);
$controllers = $finder->find('src/Controllers/');
use Bermuda\ClassFinder\ClassFinder;
use Psr\Container\ContainerInterface;
// Create from container
$finder = ClassFinder::createFromContainer($container);
use Bermuda\ClassFinder\{ClassFinder, ClassNotifier};
use Bermuda\ClassFinder\ClassFoundListenerInterface;
use Bermuda\Tokenizer\ClassInfo;
class MyListener implements ClassFoundListenerInterface
{
public function handle(ClassInfo $info): void
{
echo "Found class: {$info->fullQualifiedName}\n";
}
}
// Create notifier with listener
$notifier = new ClassNotifier([new MyListener()]);
// Find and notify
$finder = new ClassFinder();
$classes = $finder->find('src/');
$notifier->notify($classes);
use Bermuda\ClassFinder\ClassFinder;
use Bermuda\Tokenizer\TokenizerInterface;
$finder = new ClassFinder();
// Search only for classes
$results = $finder->find('src/', [], TokenizerInterface::SEARCH_CLASSES);
// Search only for interfaces
$results = $finder->find('src/', [], TokenizerInterface::SEARCH_INTERFACES);
// Search only for traits
$results = $finder->find('src/', [], TokenizerInterface::SEARCH_TRAITS);
// Search only for enums
$results = $finder->find('src/', [], TokenizerInterface::SEARCH_ENUMS);
// Combine search modes with bitwise OR
$results = $finder->find('src/', [],
TokenizerInterface::SEARCH_CLASSES | TokenizerInterface::SEARCH_INTERFACES
);
// Search for everything (default) - classes, interfaces, traits, enums
$results = $finder->find('src/', [], TokenizerInterface::SEARCH_ALL);
$finder = new ClassFinder([
new InstantiableFilter(),
new PatternFilter('*Controller'),
new SubclassFilter('App\\Controller\\BaseController')
]);
$controllers = $finder->find('src/Controllers/');
$finder = new ClassFinder([
new InstantiableFilter(),
new AttributeSearchFilter(['Service'], deepSearch: true),
new PatternFilter('*Service')
]);
$services = $finder->find('src/Services/');
// Find all classes that use dependency injection anywhere in the class
$finder = new ClassFinder([
new AttributeSearchFilter(['Inject', 'Autowired'], deepSearch: true)
]);
$diClasses = $finder->find('src/');
// Find API-related classes by checking for routing attributes in methods
$finder = new ClassFinder([
new InstantiableFilter(),
new AttributePatternFilter('*Route*', deepSearch: true) // Checks class AND method attributes
]);
$apiClasses = $finder->find('src/');
$finder = new ClassFinder([
new PatternFilter('*Test'),
new SubclassFilter('PHPUnit\\Framework\\TestCase')
]);
$tests = $finder->find('tests/');
$finder = new ClassFinder([
new InstantiableFilter(),
new AttributeSearchFilter(['Route', 'ApiResource'], deepSearch: true)
]);
$endpoints = $finder->find('src/Api/');
use Bermuda\Filter\{ChainableFilter, OneOfFilter};
// Find classes that are either Controllers OR Services, but must be instantiable
$finder = new ClassFinder([
new InstantiableFilter(), // Must be instantiable
new OneOfFilter([ // AND (Controller OR Service)
new PatternFilter('*Controller'),
new PatternFilter('*Service')
])
]);
$handleableClasses = $finder->find('src/');
use Bermuda\Filter\OneOfFilter;
// Find any component-like classes using multiple criteria
$componentFilter = new OneOfFilter([
new AttributeSearchFilter(['Component', 'Service', 'Repository']),
new ImplementsFilter(['App\\Contracts\\ComponentInterface']),
new PatternFilter('App\\Components\\*')
]);
$finder = new ClassFinder([$componentFilter]);
$components = $finder->find('src/');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.