VOOZH about

URL: https://deepwiki.com/hypervel/router/6.2-routehandler-and-supporting-classes

⇱ RouteHandler and Supporting Classes | hypervel/router | DeepWiki


Loading...
Menu

RouteHandler and Supporting Classes

This document provides reference documentation for the supporting classes used in the Hypervel Router routing system. These classes encapsulate internal data structures and provide utilities for route file management and route callback handling.

The two primary classes covered here are:

  • RouteHandler: Encapsulates route callback information and provides methods for inspecting and parsing controller actions
  • RouteFileCollector: Manages the collection of route definition files to be loaded by the routing system

For information about how routes are collected and processed, see Route Collection and Registration. For information about how the dispatcher factory uses these components, see Route Dispatching and Initialization.


RouteHandler Class

The RouteHandler class extends Hyperf\HttpServer\Router\Handler and serves as an enhanced wrapper around route callbacks. It provides introspection capabilities to determine whether a route uses a closure or controller action, extract controller information, and access route metadata such as names and middleware.

Purpose and Role

RouteHandler is created by the RouteCollector during route registration. Each registered route is associated with a RouteHandler instance that encapsulates:

  • The callback (closure or controller string/array)
  • The route path pattern
  • Route options (name, middleware, etc.)

The class provides a clean interface for other components to inspect route handlers without needing to understand the underlying callback structure.

Class Structure


Sources: src/RouteHandler.php1-117

Callback Inspection Methods

The RouteHandler provides methods to determine the type of callback associated with a route:

MethodReturn TypeDescription
getCallback()array|callable|stringReturns the raw callback value
isClosure()boolReturns true if the callback is a Closure instance
isControllerAction()boolReturns true if the callback is a controller (not a closure)

Example Usage Pattern:


Sources: src/RouteHandler.php19-40

Route Metadata Access

The class provides convenient access to route metadata stored in the options array:

MethodReturn TypeDescription
getRoute()stringReturns the route path pattern
getOptions()arrayReturns all route options
getName()string|nullReturns the route name from options['as']
getMiddleware()arrayReturns middleware array from options['middleware']

These methods abstract away direct array access and provide a consistent interface for accessing route properties throughout the routing system.

Sources: src/RouteHandler.php42-72

Controller Callback Parsing

The RouteHandler includes logic to parse controller strings into a standardized [class, method] array format. This parsing is lazy-loaded and cached in the parsedControllerCallback property.

Supported Controller Formats

The parser handles multiple controller specification formats:


Sources: src/RouteHandler.php97-115

Parsing Logic Details

The parseControllerCallback() method src/RouteHandler.php99-115 implements the following logic:

  1. String with @ separator: Split into [class, method] using explode('@', $this->callback) src/RouteHandler.php102-103
  2. String with :: separator: Split into [class, method] using explode('::', $this->callback) src/RouteHandler.php105-106
  3. Single class string: Assume invokable controller, return [$this->callback, '__invoke'] src/RouteHandler.php108
  4. Array format: Validate and return directly if it has two elements src/RouteHandler.php110-112
  5. Invalid format: Throw RuntimeException src/RouteHandler.php114

Controller Class Extraction

The getControllerClass() method src/RouteHandler.php77-82 provides a convenient way to extract just the controller class name:

  • Returns null for closure-based routes
  • Returns the first element of the parsed controller callback for controller actions

The getControllerCallback() method src/RouteHandler.php87-94 implements lazy loading with caching via the parsedControllerCallback property src/RouteHandler.php16

Sources: src/RouteHandler.php74-115


RouteFileCollector Class

The RouteFileCollector class manages the collection of route definition file paths. It provides a centralized location for configuring which PHP files should be loaded to register routes with the application.

Purpose and Role

The DispatcherFactory uses RouteFileCollector to determine which files to load during route initialization. This separation allows for flexible route file configuration and makes it easy to add routes from multiple sources (e.g., package routes, application routes, module routes).

Class Structure and Data Flow


Sources: src/RouteFileCollector.php1-37

Constructor and Default Configuration

The constructor src/RouteFileCollector.php11-15 accepts an optional array of route file paths. If no files are provided, it defaults to a single file: BASE_PATH . '/config/routes.php'.


This default provides convention-over-configuration for standard Hyperf applications while allowing complete customization when needed.

Sources: src/RouteFileCollector.php11-15

File Management Methods

The class provides three methods for managing the route file collection:

MethodParametersBehaviorUse Case
addRouteFile()string $routeFileAdds a single file to the collectionAdding one additional route file
addRouteFiles()array $routeFilesMerges array of files, removes duplicatesAdding multiple files from a package
setRouteFiles()array $routeFilesReplaces entire collectionComplete override of route files

addRouteFile() Method

The addRouteFile() method src/RouteFileCollector.php17-20 is a convenience wrapper that delegates to addRouteFiles():


addRouteFiles() Method

The addRouteFiles() method src/RouteFileCollector.php22-25 merges new files with existing ones and ensures no duplicates:


The use of array_unique() ensures that the same file path is never loaded twice, even if it's added multiple times by different components.

setRouteFiles() Method

The setRouteFiles() method src/RouteFileCollector.php27-30 provides a complete replacement of the route file collection:


This is useful for scenarios where you want complete control over which route files are loaded, ignoring any defaults or previously added files.

Sources: src/RouteFileCollector.php17-30

Retrieval Method

The getRouteFiles() method src/RouteFileCollector.php32-35 returns the current array of route file paths:


This method is called by DispatcherFactory during the route initialization process to determine which files to load and execute for route registration.

Sources: src/RouteFileCollector.php32-35


Integration with Routing System

The following diagram shows how RouteHandler and RouteFileCollector integrate with the broader routing system:


Sources: src/RouteHandler.php1-117 src/RouteFileCollector.php1-37

Usage in DispatcherFactory

The DispatcherFactory retrieves route files from RouteFileCollector and requires each file. These files typically contain calls to Router methods that ultimately create RouteHandler instances via RouteCollector.

RouteHandler Lifecycle


Sources: src/RouteHandler.php1-117


Implementation Details and Design Patterns

Lazy Parsing Strategy

The RouteHandler implements lazy parsing for controller callbacks through the parsedControllerCallback property src/RouteHandler.php16 This property is initialized to null and only populated when getControllerCallback() is first called src/RouteHandler.php89-94

Benefits:

  • Avoids unnecessary parsing for routes that are never inspected
  • Ensures parsing only happens once per handler instance
  • Reduces memory overhead for closure-based routes

Defensive Array Operations

The RouteFileCollector uses defensive array operations to ensure data integrity:

Inheritance from Hyperf Handler

By extending Hyperf\HttpServer\Router\Handler src/RouteHandler.php11 the RouteHandler maintains compatibility with Hyperf's routing internals while adding additional functionality specific to Hypervel's needs. This allows Hypervel routes to work seamlessly with Hyperf's HTTP server while providing enhanced introspection capabilities.

Sources: src/RouteHandler.php11-117 src/RouteFileCollector.php22-25


Summary

The supporting classes documented here provide essential infrastructure for the Hypervel Router:

  • RouteHandler encapsulates route callbacks with rich introspection capabilities, supporting both closures and multiple controller string formats
  • RouteFileCollector manages route file paths with a simple, flexible API that supports both default conventions and custom configurations

These classes are used internally by the routing system and are generally not accessed directly by application code. Understanding their functionality is primarily useful for:

  • Contributors working on the routing system internals
  • Developers debugging routing issues
  • Package developers extending the routing system

For practical usage of the routing system, refer to Router Class and Route Collection and Registration.

Sources: src/RouteHandler.php1-117 src/RouteFileCollector.php1-37