Installation
Add the bundle to your composer.json:
composer require ano/system-bundle
Register it in config/bundles.php:
return [
// ...
Ano\SystemBundle\AnoSystemBundle::class => ['all' => true],
];
First Use Case: Basic Anonymization
The bundle provides core anonymization utilities. Start by injecting the AnoSystemBundle services into a controller or command:
use Ano\SystemBundle\Service\Anonymizer;
class UserController extends Controller
{
public function anonymize(Request $request, Anonymizer $anonymizer)
{
$data = $request->request->all();
$anonymized = $anonymizer->anonymize($data);
return response()->json($anonymized);
}
}
Key Classes to Explore
Ano\SystemBundle\Service\Anonymizer: Core service for anonymizing data.Ano\SystemBundle\Strategy\* (e.g., EmailStrategy, PhoneStrategy): Predefined anonymization strategies.Ano\SystemBundle\Event\AnonymizationEvent: For extending anonymization logic via events.Define Strategies
Extend or use built-in strategies (e.g., EmailStrategy for masking emails):
$anonymizer->addStrategy('email', new EmailStrategy());
Anonymize Data
Pass an array or object to the anonymizer->anonymize() method:
$userData = [
'email' => '[email protected]',
'phone' => '1234567890',
];
$anonymized = $anonymizer->anonymize($userData);
// Output: ['email' => '*****@example.com', 'phone' => '****1890']
Integrate with Forms/Requests Use middleware to anonymize request data before processing:
namespace App\Http\Middleware;
use Ano\SystemBundle\Service\Anonymizer;
use Closure;
class AnonymizeRequestData
{
public function __construct(private Anonymizer $anonymizer) {}
public function handle($request, Closure $next)
{
$request->merge($this->anonymizer->anonymize($request->all()));
return $next($request);
}
}
Database Anonymization
Use the Anonymizer in a repository or query builder:
$queryBuilder->select(['id', 'anonymized_email' => 'email']);
$results = $queryBuilder->getQuery()->getResult();
$anonymizedResults = $this->anonymizer->anonymize($results);
AnonymizationEvent to customize anonymization:
$eventDispatcher->addListener(AnonymizationEvent::ANONYMIZATION, function (AnonymizationEvent $event) {
if ($event->getKey() === 'phone') {
$event->setValue('****' . substr($event->getValue(), -2));
}
});
config/packages/ano_system.yaml:
ano_system:
strategies:
email: App\Strategy\CustomEmailStrategy
Strategy Registration
addStrategy() or configure via YAML before anonymizing.Nested Data
anonymizer->anonymizeRecursively($data) or extend the Anonymizer class.Symfony Version Mismatch
composer.json and adapting usage patterns (e.g., using Symfony’s HttpFoundation components in Laravel via symfony/http-foundation).Event Dispatcher Dependency
EventDispatcher. If not autowired, you’ll need to manually inject it:
$eventDispatcher = $container->get('event_dispatcher');
$anonymizer->setDebug(true);
$anonymizer->getStrategies(); // Returns array of registered strategies
Custom Strategies
Create a new strategy by implementing Ano\SystemBundle\Strategy\StrategyInterface:
class CustomStrategy implements StrategyInterface
{
public function anonymize($value): string
{
return '*****' . substr($value, -3);
}
}
Override Default Behavior
Extend the Anonymizer class to modify core logic:
class CustomAnonymizer extends Anonymizer
{
protected function defaultStrategy($key)
{
return new CustomStrategy(); // Override default strategy resolution
}
}
Event Listeners
Use AnonymizationEvent to dynamically alter values:
$event->setValue(strtoupper($event->getValue())); // Convert to uppercase
ano_system.yaml is loaded after the bundle’s default config.Anonymizer is tagged as a service:
services:
Ano\SystemBundle\Service\Anonymizer:
tags: ['ano.anonymizer']
How can I help you explore Laravel packages today?