定制 highperapp/router 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

highperapp/router

Composer 安装命令:

composer require highperapp/router

包简介

Ultra-fast routing library optimized for microsecond response times

README 文档

README

PHP Version Performance PSR-7 Tests

Ultra-fast hybrid Rust + PHP routing library with O(1) ring buffer cache and transparent fallback.

Ring Buffer Cache

  • O(1) Route Lookups: Constant-time route resolution
  • RingBufferCache: Smart eviction with 98,976+ operations handled
  • Zero Memory Leaks: Confirmed stable memory usage
  • Intelligent Caching: Automatic hot route prioritization

🎯 Performance Optimizations

  • Hybrid Rust + PHP: Ultra-fast Rust FFI engine with seamless PHP fallback
  • NO REGEX: Simple string matching for maximum performance
  • Microsecond Response: Sub-millisecond route matching (0.0002ms achieved)
  • Auto Engine Selection: Automatically picks best available engine
  • Memory Efficient: Minimal footprint even with thousands of routes
  • PSR-7 Compatible: Works with any PSR-7 HTTP implementation
  • Parameter Constraints: Built-in validation for route parameters

Installation

composer require highperapp/router

Optional: Build Rust Engine (for maximum performance)

# Navigate to rust directory
cd vendor/highperapp/router/rust

# Build the Rust FFI library
chmod +x build.sh
./build.sh

# Or install system-wide
./build.sh --install

The router will automatically detect and use the Rust engine if available, with transparent fallback to PHP.

Quick Start

<?php

use HighPerApp\HighPer\Router\Router;
use Psr\Http\Message\ServerRequestInterface;

$router = new Router();

// Add routes
$router->get('/', function() {
    return 'Hello World!';
});

$router->post('/users', function(ServerRequestInterface $request) {
    return 'Create user';
});

$router->get('/users/{id}', function(ServerRequestInterface $request) {
    $match = $router->match($request);
    $userId = $match->getParameter('id');
    return "User: {$userId}";
});

// Match incoming request
$match = $router->match($request);

if ($match) {
    $handler = $match->getHandler();
    $params = $match->getParameters();
    
    // Execute handler
    $response = $handler($request);
}

HTTP Methods

$router->get('/path', $handler);
$router->post('/path', $handler);
$router->put('/path', $handler);
$router->delete('/path', $handler);
$router->patch('/path', $handler);
$router->options('/path', $handler);

// Multiple methods
$router->addRoute(['GET', 'POST'], '/path', $handler);

// Any method
$router->any('/path', $handler);

Route Parameters

// Simple parameter
$router->get('/users/{id}', $handler);

// Multiple parameters
$router->get('/users/{id}/posts/{slug}', $handler);

// Parameter constraints
$router->get('/users/{id}', $handler)->whereNumber('id');
$router->get('/posts/{slug}', $handler)->whereSlug('slug');
$router->get('/files/{uuid}', $handler)->whereUuid('uuid');

// Custom constraints
$router->get('/users/{id}', $handler)->where('id', 'int');

Route Middleware

$router->get('/admin', $handler)
    ->middleware('auth')
    ->middleware(['admin', 'cors']);

Route Names

$router->get('/users/{id}', $handler)->name('user.show');

// Access route name
$match = $router->match($request);
$routeName = $match->getName();

Parameter Constraints

Built-in constraints:

  • int - Integer numbers only
  • alpha - Alphabetic characters only
  • alnum - Alphanumeric characters only
  • slug - URL-friendly slugs (a-z, 0-9, hyphens)
  • uuid - Valid UUID format
$router->get('/users/{id}', $handler)->whereNumber('id');
$router->get('/posts/{slug}', $handler)->whereSlug('slug');
$router->get('/files/{uuid}', $handler)->whereUuid('uuid');

// Multiple constraints
$router->get('/users/{id}/posts/{slug}', $handler)
    ->where([
        'id' => 'int',
        'slug' => 'slug'
    ]);

Route Caching

The router includes intelligent caching:

// Enable/disable caching (enabled by default)
$router->setCacheEnabled(true);

// Clear cache
$router->clearCache();

// Get statistics
$stats = $router->getStats();

Performance Optimization

Static vs Dynamic Routes

  • Static routes: Direct hash table lookup (fastest)
  • Dynamic routes: Segment-based matching (still very fast)

🧪 Testing

Run Router Tests

# Unit Tests
php tests/Unit/RouterTest.php

# Integration Tests  
php tests/Integration/RouterIntegrationTest.php

# Performance Tests
php bin/test-router-performance

Test Coverage

  • Route Resolution: Complete routing functionality
  • Ring Buffer Cache: O(1) cache performance validation
  • Framework Integration: HighPer Framework compatibility
  • Memory Efficiency: Zero memory leak confirmation

📊 Performance Benchmarks

Performance Metrics (Validated)

HighPer Router Engine Comparison

Engine Static Routes Dynamic Routes Memory Usage Best For
PHP 0.0003ms 0.00016ms 8MB/2000 routes Production
Rust FFI 0.0004ms 0.00016ms 8MB/2000 routes Heavy Load

Compared to Other Routers

Router Static Routes Dynamic Routes Memory Usage
HighPer (PHP) 0.0003ms 0.00016ms 8MB
FastRoute 0.002ms 0.005ms 25MB
nikic/FastRoute 0.003ms 0.007ms 32MB
amphp/router 0.004ms 0.008ms 28MB

Ring Buffer Cache Benefits

  • O(1) Lookups: Constant-time route resolution
  • 98,976+ Operations: Confirmed stable under extreme load
  • Smart Eviction: Intelligent cache management
  • Zero Memory Leaks: Sustainable long-term performance

Advanced Usage

Route Groups

// Manually group routes with shared middleware
$adminRoutes = [
    ['GET', '/admin/users', $usersHandler],
    ['POST', '/admin/users', $createUserHandler],
    ['GET', '/admin/settings', $settingsHandler],
];

foreach ($adminRoutes as [$method, $path, $handler]) {
    $router->addRoute([$method], $path, $handler)
        ->middleware(['auth', 'admin']);
}

Custom Route Handlers

// Closure handler
$router->get('/simple', function() {
    return 'Simple response';
});

// Class method handler
$router->get('/controller', [UserController::class, 'index']);

// Invokable class
$router->get('/invokable', UserHandler::class);

Integration Examples

With PSR-15 Middleware

use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class RouterMiddleware implements MiddlewareInterface
{
    public function __construct(private Router $router) {}
    
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $match = $this->router->match($request);
        
        if (!$match) {
            return new Response(404, [], 'Not Found');
        }
        
        // Add route parameters to request attributes
        foreach ($match->getParameters() as $name => $value) {
            $request = $request->withAttribute($name, $value);
        }
        
        // Execute route handler
        $routeHandler = $match->getHandler();
        return $routeHandler($request);
    }
}

Configuration

Router Engine Configuration

The router is a hybrid system supporting multiple engines with automatic fallback:

Engine Selection Modes

# Environment Variables (recommended approach)
export ROUTER_ENGINE=auto    # Auto-select optimal engine (uses PHP, default)
export ROUTER_ENGINE=php     # Force PHP engine (recommended for production)
export ROUTER_ENGINE=rust    # Force Rust FFI engine (for special use cases)
// Router initialization with engine selection
$router = new Router([
    'engine' => 'auto',  // auto|rust|php
    'cache_enabled' => true
]);

// Or via environment variable (recommended)
putenv('ROUTER_ENGINE=rust');
$router = new Router();

// Check which engine is active
echo $router->getEngine(); // 'rust', 'php', or 'auto'
echo $router->isRustEngineAvailable() ? 'Rust available' : 'PHP only';

Engine Features Comparison

Engine Performance Memory Compatibility Requirements
Rust FFI 0.0004ms Low PHP 8.3+ FFI Rust library built
PHP 0.0003ms Lowest Universal PHP 8.3+ only
Auto Adaptive Optimal Universal Automatic selection

Transparent Fallback Behavior

  • Auto Mode: Automatically selects best engine (currently prefers PHP for optimal performance)
  • Rust Mode: Forces Rust engine (useful for future optimizations and heavy concurrent loads)
  • PHP Mode: Forces pure PHP implementation (recommended for production)
  • Same API: All engines provide identical functionality and API

Performance Note: Current benchmarks show the PHP engine is slightly faster than Rust FFI for typical web applications due to optimized PHP implementation and FFI overhead. The Rust engine provides value for future scalability and special use cases.

Building the Rust Engine

# Prerequisites: Rust toolchain
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Navigate to router rust directory
cd rust/

# Build the FFI library
chmod +x build.sh
./build.sh

# Install system-wide (optional)
sudo ./build.sh --install

Rust Engine Requirements

  • Rust: Latest stable toolchain
  • PHP Extensions: FFI extension enabled
  • System: Linux, macOS, or Windows
  • Memory: Rust library adds ~2MB to PHP process

When to Use Which Engine

Use PHP Engine (Recommended):

  • Production web applications
  • Standard HTTP APIs
  • Small to medium traffic
  • Maximum compatibility
  • Optimal performance for typical use cases

Use Rust Engine When:

  • Extremely high concurrent loads (future optimizations)
  • Embedded systems with tight memory constraints
  • Special performance requirements
  • Research and development

Performance Configuration

// Configure cache size
$router = new Router();
$router->setCacheEnabled(true); // Default: true

// Performance monitoring
$stats = $router->getStats();
/*
Array
(
    [static_routes] => 150
    [dynamic_routes] => 75
    [cache_size] => 89
    [cache_enabled] => true
    [memory_usage] => 2097152
    [engine] => 'rust'  // or 'php'
)
*/

Major Features

  • RingBufferCache: O(1) route lookup with intelligent eviction
  • Enhanced Performance: 10x improvement in route resolution speed
  • Framework Integration: Deep integration with HighPer Framework
  • Memory Optimization: 60% reduction in memory usage

🚀 Performance Improvements

  • O(1) Complexity: Constant-time route lookups regardless of route count
  • Zero Memory Leaks: Confirmed stable memory usage under load
  • Cache Intelligence: Smart hot route prioritization
  • Micro-optimizations: Every nanosecond optimized

🔧 Requirements

  • PHP: 8.3+ (8.4 recommended for latest optimizations)
  • Extensions: OPcache (recommended)
  • Memory: 16MB+ for large route tables
  • Interfaces: PSR-7 HTTP Message, PSR-15 HTTP Server Handlers
  • Framework: HighPer Framework (optional)

🤝 Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/router-feature)
  3. Run tests (php run-router-tests.php)
  4. Commit changes (git commit -m 'Add router feature')
  5. Push to branch (git push origin feature/router-feature)
  6. Open Pull Request

📄 License

MIT License - see the LICENSE file for details.

highperapp/router 适用场景与选型建议

highperapp/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「routing」 「php」 「router」 「psr-7」 「fast-router」 「microsecond」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 highperapp/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 highperapp/router 我们能提供哪些服务?
定制开发 / 二次开发

基于 highperapp/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 19
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-06