Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laminas Servicemanager Laravel Package

laminas/laminas-servicemanager

Powerful dependency injection and service container for PHP. Manage factories, abstract factories, delegators, aliases, and shared services, with PSR-11 interoperability and robust configuration for complex applications.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing via Composer: composer require laminas/laminas-servicemanager. Then create a basic configuration file (config/services.php) returning an associative array with services, aliases, and factories keys. Define your first service as an invokable or factory, e.g., 'logger' => \App\Logger\StdoutLogger::class, and instantiate the container with new ServiceManager($config). Retrieve services via $container->get(LoggerInterface::class). This enables immediate inversion of control—ideal for bootstrapping middleware, repository classes, or domain services in frameworks like Expressive or custom apps.

Implementation Patterns

  • Factory classes: Prefer explicit factories (e.g., class LoggerFactory implements FactoryInterface) for complex dependencies, especially when constructor injection requires non-type-hinted parameters or external configuration.
  • Plugin managers: Use AbstractPluginManager to expose controlled sets of同类 services (e.g., CachePluginManager for Redis/Memcached implementations), enforcing type safety and preventing arbitrary classes from being instantiated.
  • Delegators: Wrap existing services (e.g., database connections) with DelegatorFactoryInterface implementations to inject cross-cutting concerns like profiling, logging, or retry logic without modifying the original class.
  • Initializers: Leverage InitializerInterface to auto-inject common dependencies (e.g., injecting ContainerInterface into all classes implementing ServiceConsumerInterface)—great for traits or BC layers.
  • Config-driven overrides: Use environment-specific config (e.g., config/config.local.php) to swap dbal implementations in tests (InMemoryDatabase::class) vs production.

Gotchas and Tips

  • Circular dependencies are silently not detected by default—enable strict mode via ServiceManager::setThrowExceptionInCreation(true) or use ContainerInterface::has() to guard before get().
  • Autowiring pitfalls: When using automaticInjection, ensure constructor types are strictly typed and hintable—scalar参数 (e.g., string $dsn) won’t resolve unless config['parameters'] is provided.
  • Factory precedence: Explicit factories > abstract factories > autowiring. If your SomeServiceFactory isn’t used, check for a conflicting AbstractFactory or misconfigured alias.
  • Cache config: Always compile config in production (ServiceManager::compile()) or use ProxyManager integration for lazy-loading heavy services—avoid runtime compilation in CLI scripts or CLI-to-web switches.
  • Testability hack: In unit tests, prefer ContainerInterface type-hint and getContainer()->set(Service::class, $mock) over mocking ServiceManager directly—simpler, safer, and more aligned with DI best practices.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport