Download the PHP package cmatosbc/ananke without Composer
On this page you can find all versions of the php package cmatosbc/ananke. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cmatosbc/ananke
More information about cmatosbc/ananke
Files in cmatosbc/ananke
Package ananke
Short Description A flexible PHP service container that supports conditional service instantiation, relying on PHP 8+ match expression.
License gpl-3.0-or-later
Informations about the package ananke
Ananke
A flexible PHP 8+ service container that supports conditional service instantiation. This package allows you to register services with multiple conditions that must be met before the service can be instantiated.
Requirements
- PHP 8.0 or higher
Features
- Register services with their class names and constructor parameters
- Define conditions as callable functions
- Associate multiple conditions with services
- Dynamic service instantiation based on condition evaluation
- Clear error handling with specific exceptions
Installation
Basic Usage
Multiple Conditions
Services can have multiple conditions that must ALL be satisfied before instantiation:
Condition Decorators
Ananke provides a powerful set of condition decorators that allow you to compose complex condition logic:
Not Condition
Negate any condition:
Cached Condition
Cache expensive condition evaluations:
AND/OR Conditions
Combine multiple conditions with logical operators:
XOR/NOR Conditions
For more complex logical operations, you can use XOR (exclusive OR) and NOR conditions:
Complex Condition Compositions
Combine decorators for complex logic:
Service Types
Ananke supports two types of service instantiation: Singleton and Prototype.
Singleton Services
Singleton services are instantiated only once and the same instance is returned for subsequent requests. This is useful for services that maintain state or are resource-intensive to create.
You can also clear singleton instances when needed:
Prototype Services
Prototype services create a new instance every time they are requested. This is the default behavior and is ideal for services that should not share state.
Changing Service Types
You can change a service's type after registration:
Best Practices
-
Caching: Use
CachedCondition
for:- External API calls
- Database queries
- File system checks
- Any expensive operations
-
Composition: Build complex conditions gradually:
- Start with simple conditions
- Combine them using AND/OR
- Add negation where needed
- Cache at appropriate levels
-
Naming: Use clear, descriptive names:
- Negated: prefix with 'not-'
- Cached: prefix with 'cached-'
- Combined: use descriptive action names
- Testing: Test complex conditions thoroughly:
- Verify each sub-condition
- Test boundary cases
- Ensure proper short-circuit evaluation
- Validate cache behavior
Real-World Use Cases
1. Environment-Specific Services
Control debug tools based on environment:
2. Feature Flags and A/B Testing
Implement feature toggles with multiple conditions:
3. Database Connection Management
Safe handling of database-dependent services:
4. License-Based Feature Access
Control access to premium features:
Error Handling
The service container throws specific exceptions:
ServiceNotFoundException
: When trying to create a non-registered serviceClassNotFoundException
: When registering a service with a non-existent classInvalidArgumentException
: When a condition is not met or invalid
Testing
Run the test suite:
The tests provide detailed output showing the state of conditions and service creation:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the GPL-3.0-or-later License - see the LICENSE file for details.