1. Go to this page and download the library: Download cmatosbc/ananke library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cmatosbc / ananke example snippets
use Ananke\ServiceFactory;
$factory = new ServiceFactory();
// Register a service with constructor parameters
$factory->register('logger', Logger::class, ['debug']);
// Register conditions
$factory->registerCondition('is-development', fn() => getenv('APP_ENV') === 'development');
$factory->registerCondition('has-permissions', fn() => is_writable('/var/log'));
// Associate multiple conditions with service
$factory->associateCondition('logger', 'is-development');
$factory->associateCondition('logger', 'has-permissions');
// Create service (only works if ALL conditions are met)
if ($factory->has('logger')) {
$logger = $factory->create('logger');
}
// Premium feature example
$factory->register('premium.feature', PremiumFeature::class);
// Register all erCondition('feature-enabled', fn() => $featureFlags->isEnabled('new-feature'));
$factory->registerCondition('has-valid-license', fn() => $license->isValid());
// Associate ALL conditions with the service
$factory->associateCondition('premium.feature', 'is-premium-user');
$factory->associateCondition('premium.feature', 'feature-enabled');
$factory->associateCondition('premium.feature', 'has-valid-license');
// Service will only be created if ALL conditions are met
if ($factory->has('premium.feature')) {
$feature = $factory->create('premium.feature');
}
use Ananke\Conditions\{NotCondition, CallableCondition};
// Basic condition
$factory->registerCondition('is-maintenance',
new CallableCondition('is-maintenance', fn() => $maintenance->isActive()));
// Negate it
$factory->registerCondition('not-maintenance',
new NotCondition($factory->getCondition('is-maintenance')));
// Use in service
$factory->register('api', APIService::class);
$factory->associateCondition('api', 'not-maintenance');
use Ananke\Conditions\CachedCondition;
// Cache an expensive API check for 1 hour
$factory->registerCondition('api-status',
new CachedCondition(
new CallableCondition('api-check', fn() => $api->checkStatus()),
3600 // Cache for 1 hour
));
use Ananke\Conditions\{AndCondition, OrCondition};
// Premium access: User must be premium OR have a trial subscription
$factory->registerCondition('can-access-premium',
new OrCondition([
new CallableCondition('is-premium', fn() => $user->isPremium()),
new CallableCondition('has-trial', fn() => $user->hasTrial())
]));
// Database write: Need both connection AND proper permissions
$factory->registerCondition('can-write-db',
new AndCondition([
new CallableCondition('is-connected', fn() => $db->isConnected()),
new CallableCondition('has-permissions', fn() => $user->canWrite())
]));
use Ananke\Conditions\{XorCondition, NorCondition};
// XOR: Feature must be enabled in EXACTLY one environment (dev XOR prod)
$factory->registerCondition('feature-enabled-single-env',
new XorCondition([
new CallableCondition('dev-enabled', fn() => $featureFlags->isEnabled('dev')),
new CallableCondition('prod-enabled', fn() => $featureFlags->isEnabled('prod'))
]));
// NOR: Service is available only when NONE of the maintenance modes are active
$factory->registerCondition('all-systems-available',
new NorCondition([
new CallableCondition('db-maintenance', fn() => $maintenance->isDatabaseMaintenance()),
new CallableCondition('api-maintenance', fn() => $maintenance->isApiMaintenance()),
new CallableCondition('ui-maintenance', fn() => $maintenance->isUiMaintenance())
]));
// ((isPremium OR hasTrial) AND notMaintenance) AND (hasQuota OR isUnlimited)
$factory->registerCondition('can-use-service',
new AndCondition([
// Premium access check
new OrCondition([
new CallableCondition('premium', fn() => $user->isPremium()),
new CallableCondition('trial', fn() => $user->hasTrial())
]),
// Not in maintenance
new NotCondition(
new CallableCondition('maintenance', fn() => $maintenance->isActive())
),
// Resource availability
new OrCondition([
new CallableCondition('has-quota', fn() => $user->hasQuota()),
new CallableCondition('unlimited', fn() => $user->isUnlimited())
])
])
);
// Cache the entire complex condition
$factory->registerCondition('cached-access-check',
new CachedCondition(
$factory->getCondition('can-use-service'),
300 // Cache for 5 minutes
)
);
use Ananke\ServiceFactory;
$factory = new ServiceFactory();
// Register a service as singleton
$factory->register('database', DatabaseConnection::class);
$factory->registerAsSingleton('database');
// Both variables will reference the same instance
$db1 = $factory->create('database');
$db2 = $factory->create('database');
assert($db1 === $db2); // true
// Clear all singleton instances
$factory->clearSingletons();
// Now you'll get a new instance
$db3 = $factory->create('database');
assert($db3 !== $db1); // true
use Ananke\ServiceFactory;
$factory = new ServiceFactory();
// Register a service (prototype by default)
$factory->register('transaction', Transaction::class);
// Or explicitly register as prototype
$factory->registerAsPrototype('transaction');
// Each call creates a new instance
$tx1 = $factory->create('transaction');
$tx2 = $factory->create('transaction');
assert($tx1 !== $tx2); // true