Skip to content

feat(Middleware): Support ::class syntax in getDependencies() #405

Description

@usernane

Feature Request

getDependencies() currently returns an array of middleware name strings. The registry resolves them via MiddlewareManager::getMiddleware($name) which matches against the name passed to the constructor.

This means developers must remember arbitrary string names:

public function getDependencies(): array {
    return ['audit-log', 'start-session']; // magic strings
}

Proposed

Support class name resolution so developers can use ::class:

public function getDependencies(): array {
    return [AuditLogMiddleware::class, StartSessionMiddleware::class];
}

Suggested Implementation

In the dependency resolution step, if a dependency string is a valid class name (contains \\), instantiate or look up the registered instance by class instead of by name:

foreach ($current->getDependencies() as $dep) {
    if (class_exists($dep)) {
        // find registered middleware that is an instance of $dep
        $mw = $this->findByClass($dep);
    } else {
        $mw = $this->getMiddleware($dep);
    }
}

Benefits

  • IDE autocompletion and refactoring support
  • Compile-time checking (typos caught immediately)
  • No need to remember arbitrary name strings
  • Both approaches can coexist (string names still work)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions