This document provides practical, real-world usage examples for all Phase 3 drivers:
- MySQLSecurityGuard
- RedisSecurityGuard
- MongoSecurityGuard
It covers:
- Pure PHP usage
- Slim Framework integration (basic + advanced)
- Laravel integration (basic + Service Provider binding)
For a simple, beginner-friendly introduction, see:
📘 docs/examples/phase3_simple_examples.md
For internal behavior specifications, see:
📘 docs/specs/phase3_driver_behavior.md
Every example begins with the following initialization:
<?php
require __DIR__ . '/../../../vendor/autoload.php';
// ---------------------------------------------------------------------
// 🔧 Load adapter configuration
// ---------------------------------------------------------------------
$config = new EnvironmentConfig(__DIR__ . '/../../../');
$resolver = new DatabaseResolver($config);
// ---------------------------------------------------------------------
// 📌 Shared identifier used across all drivers
// ---------------------------------------------------------------------
$identifier = 'login.guard';Phase 3 supports three storage backends:
$mysqlAdapter = $resolver->resolve('mysql.security', autoConnect: true);
$redisAdapter = $resolver->resolve('redis.security', autoConnect: true);
$mongoAdapter = $resolver->resolve('mongo.security', autoConnect: true);Each one maps to a different Security Guard driver.
$mysqlGuard = new MySQLSecurityGuard(
adapter: $mysqlAdapter,
identifier: $identifier
);$redisGuard = new RedisSecurityGuard(
adapter: $redisAdapter,
identifier: $identifier
);$mongoGuard = new MongoSecurityGuard(
adapter: $mongoAdapter,
identifier: $identifier
);These examples work identically across MySQL, Redis, and Mongo.
$dto = new LoginAttemptDTO(
ip: '10.0.0.5',
subject: 'john@example.com',
resetAfter: 600,
userAgent: 'Mozilla/5.0'
);
$count = $redisGuard->recordFailure($dto);
echo "Failure count: {$count}\n";$mysqlGuard->resetAttempts(
ip: '10.0.0.5',
subject: 'john@example.com'
);$block = new SecurityBlockDTO(
ip: '10.0.0.5',
subject: 'john@example.com',
type: BlockTypeEnum::AUTO,
createdAt: time(),
expiresAt: time() + 1800
);
$mongoGuard->block($block);$redisGuard->unblock(
ip: '10.0.0.5',
subject: 'john@example.com'
);$stats = $mysqlGuard->getStats(
ip: '10.0.0.5',
subject: 'john@example.com'
);
print_r($stats);Slim offers powerful DI and routing, making it ideal for Security Guard integration.
$container->set(SecurityGuardService::class, function () use ($redisAdapter, $identifier) {
return new SecurityGuardService(
new RedisSecurityGuard($redisAdapter, $identifier)
);
});Usage inside a Slim route:
$app->post('/login', function ($request, $response) {
$guard = $this->get(SecurityGuardService::class);
// ...
});$container->set(MySQLSecurityGuard::class, function () use ($mysqlAdapter, $identifier) {
return new MySQLSecurityGuard($mysqlAdapter, $identifier);
});
$container->set(RedisSecurityGuard::class, function () use ($redisAdapter, $identifier) {
return new RedisSecurityGuard($redisAdapter, $identifier);
});
$container->set(MongoSecurityGuard::class, function () use ($mongoAdapter, $identifier) {
return new MongoSecurityGuard($mongoAdapter, $identifier);
});
$container->set(SecurityGuardService::class, function ($c) {
return new SecurityGuardService($c->get(RedisSecurityGuard::class));
});
$app->post('/login', function ($request, $response) {
$guard = $this->get(SecurityGuardService::class);
// security logic here
return $response;
});Laravel's container makes driver binding extremely clean.
$guard = app(RedisSecurityGuard::class);
$svc = new SecurityGuardService($guard);Create:
📄 app/Providers/SecurityGuardServiceProvider.php
class SecurityGuardServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(RedisSecurityGuard::class, function () {
$config = new EnvironmentConfig(base_path());
$resolver = new DatabaseResolver($config);
$redis = $resolver->resolve('redis.security', autoConnect: true);
return new RedisSecurityGuard($redis, 'login.guard');
});
$this->app->bind(SecurityGuardService::class, function ($app) {
return new SecurityGuardService(
$app->make(RedisSecurityGuard::class)
);
});
}
}Usage:
$svc = resolve(SecurityGuardService::class);$this->app->bind(MySQLSecurityGuard::class, function () {
$config = new EnvironmentConfig(base_path());
$resolver = new DatabaseResolver($config);
$adapter = $resolver->resolve('mysql.security', autoConnect: true);
return new MySQLSecurityGuard($adapter, 'login.guard');
});
$this->app->bind(MongoSecurityGuard::class, function () {
$config = new EnvironmentConfig(base_path());
$resolver = new DatabaseResolver($config);
$adapter = $resolver->resolve('mongo.security', autoConnect: true);
return new MongoSecurityGuard($adapter, 'login.guard');
});This file covered:
- Bootstrap + adapters
- How to instantiate all drivers (MySQL, Redis, Mongo)
- Common operations for all storage backends
- Slim (minimal + complete DI)
- Laravel (light + full service provider binding)
For deeper behavior rules, internal details, and determinism guarantees, see:
📄 docs/specs/phase3_driver_behavior.md