daycry/class-finder

Namespace Class Finder

v2.2.1 2023-02-14 15:17 UTC

This package is auto-updated.

Last update: 2024-12-14 19:02:49 UTC


README

Donate

ClassFinder

===========

A dead simple utility to identify classes in a given namespace for Codeigniter 4

Build Status Coverage Status Downloads GitHub release (latest by date) GitHub stars GitHub license

Requirements

  • Application is using Composer.
  • Classes can be autoloaded with Composer.
  • PHP >= 7.4.0

Installing

Installing is done by requiring it with Composer.

$ composer require daycry/class-finder

Examples

Standard Mode

<?php

$classes = (new \Daycry\ClassFinder\ClassFinder())->getClassesInNamespace('Daycry');

/**
 * array(
 *   'TestApp1\Foo\Bar',
 *   'TestApp1\Foo\Baz',
 *   'TestApp1\Foo\Foo'
 * )
 */
var_dump($classes);

Recursive Mode

<?php

$classes = (new \Daycry\ClassFinder\ClassFinder())->getClassesInNamespace('Daycry', \Daycry\ClassFinder\ClassFinder::RECURSIVE_MODE);

/**
 * array(
 *   'TestApp1\Foo\Bar',
 *   'TestApp1\Foo\Baz',
 *   'TestApp1\Foo\Foo',
 *   'TestApp1\Foo\Box\Bar',
 *   'TestApp1\Foo\Box\Baz',
 *   'TestApp1\Foo\Box\Foo',
 *   'TestApp1\Foo\Box\Lon\Bar',
 *   'TestApp1\Foo\Box\Lon\Baz',
 *   'TestApp1\Foo\Box\Lon\Foo',
 * )
 */
var_dump($classes);

If you want to modify the configuration, you can modify the file Config/ClassFinder.php

or

Edit the configuration and pass it to the constructor

<?php
$config = config('ClassFinder');

$config->finder['classMap'] = false;
$config->finder['files'] = false;

$classes = (new \Daycry\ClassFinder\ClassFinder($config))->getClassesInNamespace('Daycry', \Daycry\ClassFinder\ClassFinder::RECURSIVE_MODE);

You can customize the search engine indicating if you want to search for classes, interfaces, traits or functions.

This library also integrates the Autoload.php class from the Config folder to perform searches.

<?php
$config = config('ClassFinder');

$config->finder['classMap'] = false;
$config->finder['files'] = false;

$classes = (new \Daycry\ClassFinder\ClassFinder($config))->getClassesInNamespace('App', \Daycry\ClassFinder\ClassFinder::RECURSIVE_MODE);

$classes = (new \Daycry\ClassFinder\ClassFinder($config))->getClassesInNamespace('Config', \Daycry\ClassFinder\ClassFinder::RECURSIVE_MODE);
<?php
$config = config('ClassFinder');

$config->finder['classMap'] = false;
$config->finder['files'] = false;

$classes = (new \Daycry\ClassFinder\ClassFinder($config))->getClassesInNamespace('Daycry', \Daycry\ClassFinder\ClassFinder::RECURSIVE_MODE | \Daycry\ClassFinder\ClassFinder::ALLOW_CLASSES | \Daycry\ClassFinder\ClassFinder::ALLOW_INTERFACES | \Daycry\ClassFinder\ClassFinder::ALLOW_TRAITS | \Daycry\ClassFinder\ClassFinder::ALLOW_FUNCTIONS );