pixielity/laravel-container
Composer 安装命令:
composer require pixielity/laravel-container
包简介
Dependency injection container utilities and extensions
README 文档
README
Automatic dependency injection and tagged class registration for Laravel applications
The Container package provides utilities for automatic class discovery and registration using PHP attributes, eliminating manual service provider configuration.
Table of Contents
- Overview
- Installation
- Features
- Quick Start
- Core Concepts
- Usage
- API Reference
- Advanced Usage
- Best Practices
- Performance
- Testing
- Troubleshooting
Overview
The Container package extends Laravel's service container with automatic class discovery and registration capabilities. It uses PHP 8 attributes to mark classes for automatic registration, eliminating boilerplate code in service providers.
Key Benefits
- ✅ Zero Configuration: Classes register themselves automatically
- ✅ Type Safe: Uses PHP attributes and reflection
- ✅ Performance: Leverages Composer's cached attribute data
- ✅ Monorepo Friendly: Discovers classes across packages
- ✅ Clean Code: Eliminates manual registration boilerplate
Installation
The Container package is part of the Framework meta-package and is automatically available:
composer require pixielity/laravel-framework
Features
🏷️ Tagged Class Registration
Automatically discover and register classes with custom tags using the #[Tagged] attribute.
🔍 Automatic Discovery
Uses the Discovery package to find tagged classes across your entire application.
📦 Bulk Registration
Efficiently registers multiple classes with a single container operation.
🎯 Service Provider Integration
Seamlessly integrates with Laravel's service provider system.
Quick Start
1. Mark Classes with Tags
use Pixielity\Container\Attributes\Tagged;
#[Tagged('payment.processors')]
class StripePaymentProcessor implements PaymentProcessor{
public function process(Payment $payment): Result
{
// Implementation
}
}
#[Tagged('payment.processors')]
class PayPalPaymentProcessor implements PaymentProcessor{
public function process(Payment $payment): Result
{
// Implementation
}
}
2. Enable Discovery in Service Provider
use Pixielity\Container\Concerns\HasDiscovery;
use Pixielity\Support\ServiceProvider;
class PaymentServiceProvider extends ServiceProvider
{
use HasDiscovery;
public function register(): void
{
parent::register();
// Automatically discovers and registers all tagged classes
$this->discoverTaggedClasses();
}
}
3. Retrieve Tagged Classes
// Get all payment processors
$processors = app()->tagged('payment.processors');
foreach ($processors as $processor) {
$processor->process($payment);
}
Core Concepts
Tagged Classes
Classes marked with the #[Tagged] attribute are automatically discovered and registered with the service container under the specified tag name.
Benefits:
- No manual registration required
- Self-documenting code
- Easy to add new implementations
- Supports multiple tags per class
Automatic Discovery
The package uses the Discovery facade to find all classes with the #[Tagged] attribute:
- Scan: Composer's attribute collector finds all tagged classes
- Filter: Validates that classes exist
- Group: Organizes classes by tag name
- Register: Bulk registers with Laravel's container
Usage
Basic Tagged Class
use Pixielity\Container\Attributes\Tagged;
#[Tagged('repositories')]
class UserRepository
{
public function find(int $id): ?User
{
return User::find($id);
}
}
Multiple Tags
A class can have multiple tags:
#[Tagged('repositories')]
#[Tagged('user.services')]
class UserRepository
{
// Implementation
}
Retrieving Tagged Classes
// Get all classes with a specific tag
$repositories = app()->tagged('repositories');
// Iterate through tagged classes
foreach ($repositories as $repository) {
// Use the repository
}
// Get as array
$repositoryArray = iterator_to_array(app()->tagged('repositories'));
Service Provider Integration
use Pixielity\Container\Concerns\HasDiscovery;
use Pixielity\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
use HasDiscovery;
public function register(): void
{
parent::register();
// Discover all tagged classes
$this->discoverTaggedClasses();
}
public function boot(): void
{
// Use tagged classes
$processors = app()->tagged('payment.processors');
// Register them with a manager
foreach ($processors as $processor) {
$this->app->make(PaymentManager::class)
->register($processor);
}
}
}
API Reference
Attributes
#[Tagged]
Marks a class for automatic registration with a tag.
Parameters:
tag(string): The tag name to register the class under
Example:
#[Tagged('event.listeners')]
class UserRegisteredListener
{
// Implementation
}
Concerns
HasDiscovery
Trait that provides automatic tagged class discovery and registration.
Methods:
discoverTaggedClasses(): void
Discovers and registers all classes with the #[Tagged] attribute.
Usage:
use Pixielity\Container\Concerns\HasDiscovery;
class MyServiceProvider extends ServiceProvider
{
use HasDiscovery;
public function register(): void
{
$this->discoverTaggedClasses();
}
}
Providers
ContainerServiceProvider
The main service provider for the Container package.
Features:
- Automatically discovers tagged classes
- Registers container utilities
- Provides dependency injection helpers
Advanced Usage
Custom Discovery
You can manually discover and process tagged classes:
use Pixielity\Container\Attributes\Tagged;
use Pixielity\Discovery\Facades\Discovery;
$taggedClasses = Discovery::attribute(Tagged::class)
->get()
->filter(fn($class) => class_exists($class));
foreach ($taggedClasses as $class) {
// Custom processing
}
Conditional Registration
Register classes based on conditions:
use Pixielity\Container\Attributes\Tagged;
#[Tagged('payment.processors')]
class StripePaymentProcessor
{
public static function shouldRegister(): bool
{
return config('payment.stripe.enabled', false);
}
}
// In service provider
$processors = app()->tagged('payment.processors');
foreach ($processors as $processor) {
if (method_exists($processor, 'shouldRegister')
&& !$processor::shouldRegister()) {
continue;
}
// Register the processor
}
Best Practices
1. Use Descriptive Tag Names
// ✅ Good - Clear and descriptive
#[Tagged('payment.processors')]
#[Tagged('notification.channels')]
#[Tagged('report.generators')]
// ❌ Bad - Vague or unclear
#[Tagged('processors')]
#[Tagged('handlers')]
2. Group Related Classes
// Group by domain
#[Tagged('user.repositories')]
#[Tagged('user.services')]
#[Tagged('user.validators')]
3. Document Tag Usage
/**
* Payment processor for Stripe integration.
*
* @tagged payment.processors
*/
#[Tagged('payment.processors')]
class StripePaymentProcessor
{
// Implementation
}
4. Use Interfaces
interface PaymentProcessor{
public function process(Payment $payment): Result;
}
#[Tagged('payment.processors')]
class StripePaymentProcessor implements PaymentProcessor{
// Implementation ensures contract compliance
}
Performance
Caching
The Discovery package uses Composer's attribute collector, which caches attribute data:
- First Run: Scans all files (done during
composer dump-autoload) - Subsequent Runs: Uses cached data (instant)
- Cache Location:
vendor/attributes.php
Optimization Tips
- Run in register(): Discover classes during registration phase
- Cache Results: Store discovered classes if needed multiple times
- Use Specific Tags: More specific tags = faster filtering
// Cache discovered classes
protected array $cachedProcessors;
public function register(): void
{
$this->discoverTaggedClasses();
// Cache for later use
$this->cachedProcessors = iterator_to_array(
app()->tagged('payment.processors')
);
}
Testing
Testing Tagged Classes
use Tests\TestCase;
class TaggedClassTest extends TestCase
{
public function test_class_is_tagged(): void
{
$processors = app()->tagged('payment.processors');
$processorClasses = iterator_to_array($processors);
$this->assertContains(
StripePaymentProcessor::class,
$processorClasses
);
}
public function test_all_processors_implement_interface(): void
{
$processors = app()->tagged('payment.processors');
foreach ($processors as $processor) {
$this->assertInstanceOf(
PaymentProcessorInterface::class,
$processor
);
}
}
}
Mocking Tagged Classes
public function test_with_mocked_processor(): void
{
$mock = $this->createMock(PaymentProcessorInterface::class);
// Override tagged classes for testing
app()->tag([$mock], 'payment.processors');
// Test code that uses tagged processors
}
Troubleshooting
Classes Not Being Discovered
Problem: Tagged classes aren't being registered.
Solutions:
- Run
composer dump-autoloadto rebuild attribute cache - Ensure the class is autoloadable
- Check that
discoverTaggedClasses()is called inregister() - Verify the attribute is imported correctly
# Rebuild attribute cache
composer dump-autoload
Tag Name Typos
Problem: Can't retrieve classes due to tag name mismatch.
Solution: Use constants for tag names:
class Tags
{
public const PAYMENT_PROCESSORS = 'payment.processors';
public const NOTIFICATION_CHANNELS = 'notification.channels';
}
#[Tagged(Tags::PAYMENT_PROCESSORS)]
class StripePaymentProcessor { }
// Retrieve
$processors = app()->tagged(Tags::PAYMENT_PROCESSORS);
Performance Issues
Problem: Discovery is slow.
Solutions:
- Ensure Composer's attribute cache is built
- Don't call
discoverTaggedClasses()multiple times - Cache results if used frequently
Related Packages
- Discovery: Provides the attribute discovery functionality
- ServiceProvider: Base service provider with discovery support
- Support: Reflection utilities used by Container
License
This package is part of the Pixielity Framework and is open-sourced software licensed under the MIT license.
pixielity/laravel-container 适用场景与选型建议
pixielity/laravel-container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「ioc」 「dependency-injection」 「laravel」 「pixielity」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pixielity/laravel-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pixielity/laravel-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pixielity/laravel-container 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Supporing Symfony's DependencyInjection component for your symfony1 project
A fast and intuitive dependency injection container.
Common IoC Api Interface
Powerful class discovery system for Laravel with attribute-based scanning, directory traversal, and monorepo support
Dependency injection container for the Monolith framework.
Molajo Inversion of Control package offers a full-featured, simple-to-use dependency injection and object construction solution for PHP applications.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-08