highperapp/container

High-performance PSR-11 container

1.0 2025-07-06 03:25 UTC

This package is auto-updated.

Last update: 2025-07-06 06:30:33 UTC


README

PHP Version PSR-11 Performance Tests

Standalone high-performance PSR-11 container for any PHP project. Can be used independently or integrated with HighPer framework. Features object pooling, memory optimization, and zero-overhead service resolution.

๐Ÿš€ Features

โšก Build-Time Compilation

  • ContainerCompiler: Compile service definitions to optimized PHP code
  • O(1) Resolution: Zero-overhead service lookups in production
  • Dependency Analysis: Compile-time dependency validation
  • Cache Generation: Persistent compiled container caching

๐ŸŽฏ Performance Optimizations

  • PSR-11 Compatible: Fully compliant with PSR-11 Container Interface
  • Object Pooling: Advanced object pool for memory optimization
  • C10M Ready: Optimized for extreme concurrency scenarios
  • Auto-wiring: Lightning-fast automatic dependency injection
  • Memory Efficient: Real-time memory usage tracking and optimization
  • Framework Agnostic: Works standalone or integrates seamlessly with any framework
  • HighPer Framework: First-class integration with HighPer framework

Installation

composer require highperapp/container

Usage Scenarios

๐Ÿ”ง Standalone Library

Use in any PHP project (Laravel, Symfony, CodeIgniter, or vanilla PHP):

// In any PHP project
use HighPerApp\HighPer\Container\Container;

$container = new Container();
$container->bind(DatabaseInterface::class, MySQLDatabase::class);
$database = $container->get(DatabaseInterface::class);

๐Ÿ—๏ธ HighPer Framework Integration

Automatically available as the default container in HighPer framework with enhanced features and zero configuration.

Quick Start

<?php

use HighPerApp\HighPer\Container\Container;

// Create container instance
$container = new Container();

// Register services
$container->bind('database', PDO::class);
$container->singleton('logger', MyLogger::class);

// Register with factory
$container->factory('redis', function() {
    return new Redis(['host' => 'localhost']);
});

// Register instance
$container->instance('config', $configObject);

// Resolve services
$database = $container->get('database');
$logger = $container->get('logger');

Advanced Usage

Service Binding

// Bind with concrete implementation
$container->bind(LoggerInterface::class, FileLogger::class);

// Bind as singleton
$container->singleton(DatabaseInterface::class, MySQLDatabase::class);

// Bind with factory function
$container->factory('cache', function($container) {
    $redis = $container->get('redis');
    return new CacheService($redis);
});

Aliases

$container->alias('db', 'database');
$container->alias('log', LoggerInterface::class);

Auto-wiring

The container automatically resolves constructor dependencies:

class UserService 
{
    public function __construct(
        private DatabaseInterface $database,
        private LoggerInterface $logger
    ) {}
}

// Register dependencies
$container->bind(DatabaseInterface::class, MySQLDatabase::class);
$container->bind(LoggerInterface::class, FileLogger::class);

// Auto-wire UserService
$userService = $container->get(UserService::class);

Memory Optimization

// Get memory usage statistics
$stats = $container->getStats();

// Object pool statistics
$poolStats = $stats['object_pool_stats'];

Configuration

The container is designed to work out of the box with zero configuration, but you can customize the object pool:

use HighPerApp\HighPer\Container\ObjectPool;

$container = new Container();

// Access and use object pool
$objectPool = new ObjectPool();
$objectPool->populate(ExpensiveObject::class, 10);

๐Ÿงช Testing

Run Container Tests

# All tests
composer test

# Unit tests only
vendor/bin/phpunit --testsuite=Unit

# Integration tests only
vendor/bin/phpunit --testsuite=Integration

# Performance tests
vendor/bin/phpunit --testsuite=Performance

Test Coverage

  • Container Core: Complete PSR-11 functionality
  • Build-Time Compilation: Compiler validation and performance
  • Framework Integration: HighPer Framework compatibility
  • Memory Optimization: Object pooling and memory efficiency

๐Ÿ“Š Performance Benchmarks

Performance Metrics

Operation Speed
Service Resolution <0.001ms
Compilation 50 services/sec
Memory Usage <5MB for 100 services
Dependency Injection Instant

Build-Time Compilation Benefits

  • Production Speed: Compiled containers eliminate runtime overhead
  • Dependency Validation: Catch dependency issues at build time
  • Memory Efficiency: Optimized service instantiation patterns
  • Cache Performance: Persistent compiled container storage
  • Framework Agnostic: Same performance benefits in any environment

โœจ Major Features

  • ContainerCompiler: Build-time compilation for maximum performance
  • Enhanced Object Pooling: Advanced memory management strategies
  • Standalone Library: Use in any PHP project without framework dependencies
  • Framework Integration: Optional deep integration with HighPer Framework
  • Performance Monitoring: Real-time container performance metrics

๐Ÿš€ Performance Improvements

  • 40-60% Faster Resolution: Optimized service lookup algorithms
  • Build-Time Validation: Catch errors before production
  • Memory Optimization: Reduced memory footprint and leaks
  • Caching Strategy: Intelligent compiled container caching

๐Ÿ”ง Requirements

  • PHP: 8.3+ (8.4 recommended for latest optimizations)
  • Extensions: OPcache (recommended), APCu (optional)
  • Memory: 64MB+ for compilation, minimal for runtime
  • Framework: Any PHP framework or standalone (HighPer Framework integration optional)

๐Ÿค Contributing

This container serves both standalone and HighPer framework users:

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/container-feature)
  3. Run tests (composer test)
  4. Ensure compatibility with both standalone and framework usage
  5. Commit changes (git commit -m 'Add container feature')
  6. Push to branch (git push origin feature/container-feature)
  7. Open Pull Request

๐Ÿ“„ License

MIT License - see the LICENSE file for details.

HighPer DI Container - Standalone library with build-time compilation for any PHP project