0 follower

Final Class Yiisoft\Classifier\Classifier

InheritanceYiisoft\Classifier\Classifier

Classifier traverses file system to find classes by a certain criteria.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $directory, string $directories )
$directory string

Directory to traverse.

$directories string

Extra directories to traverse.

                public function __construct(string $directory, string ...$directories)
{
    $this->directories = [$directory, ...array_values($directories)];
}

            
find() public method

public string[] find ( )
return string[]

Classes found.

                public function find(): iterable
{
    $countInterfaces = count($this->interfaces);
    $countAttributes = count($this->attributes);
    if ($countInterfaces === 0 && $countAttributes === 0 && $this->parentClass === null) {
        return [];
    }
    $this->scanFiles();
    $classesToFind = get_declared_classes();
    $isWindows = DIRECTORY_SEPARATOR === '\\';
    $directories = $this->directories;
    if ($isWindows) {
        /** @var string[] $directories */
        $directories = str_replace('/', '\\', $directories);
    }
    foreach ($classesToFind as $className) {
        $reflection = new ReflectionClass($className);
        if (!$reflection->isUserDefined()) {
            continue;
        }
        $matchedDirs = array_filter(
            $directories,
            static fn($directory) => str_starts_with($reflection->getFileName(), $directory)
        );
        if (count($matchedDirs) === 0) {
            continue;
        }
        if ($countInterfaces > 0) {
            $interfaces = $reflection->getInterfaces();
            $interfaces = array_map(static fn(ReflectionClass $class) => $class->getName(), $interfaces);
            if (count(array_intersect($this->interfaces, $interfaces)) !== $countInterfaces) {
                continue;
            }
        }
        if ($countAttributes > 0) {
            $attributes = $reflection->getAttributes();
            $attributes = array_map(
                static fn(ReflectionAttribute $attribute) => $attribute->getName(),
                $attributes
            );
            if (count(array_intersect($this->attributes, $attributes)) !== $countAttributes) {
                continue;
            }
        }
        if (($this->parentClass !== null) && !is_subclass_of($className, $this->parentClass)) {
            continue;
        }
        yield $className;
    }
}

            
withAttribute() public method

public self withAttribute ( string $attributes )
$attributes string

                public function withAttribute(string ...$attributes): self
{
    $new = clone $this;
    array_push($new->attributes, ...array_values($attributes));
    return $new;
}

            
withInterface() public method

public self withInterface ( string $interfaces )
$interfaces string

Interfaces to search for.

                public function withInterface(string ...$interfaces): self
{
    $new = clone $this;
    array_push($new->interfaces, ...array_values($interfaces));
    return $new;
}

            
withParentClass() public method

public self withParentClass ( string $parentClass )
$parentClass string

Parent class to search for.

                public function withParentClass(string $parentClass): self
{
    $new = clone $this;
    $new->parentClass = $parentClass;
    return $new;
}