bentools/user-aware-command-bundle
Installation
composer require bentools/user-aware-command-bundle
Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3/5):
return [
// ...
BenTools\UserAwareCommandBundle\UserAwareCommandBundle::class => ['all' => true],
];
Prerequisites
Ensure your entities use Gedmo Blameable (e.g., createdBy, updatedBy fields) and Doctrine Extensions are configured.
First Use Case
Create a command implementing UserAwareInterface:
namespace App\Command;
use BenTools\UserAwareCommandBundle\Model\UserAwareInterface;
use Symfony\Component\Console\Command\Command;
class TestCommand extends Command implements UserAwareInterface
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
// Entity operations here will auto-populate `createdBy`/`updatedBy`
return Command::SUCCESS;
}
}
Automatic User Binding
BenTools\UserAwareCommandBundle\Provider\SystemUserProvider) into Blameable fields.Dynamic User Override
php bin/console app:test-command --user=admin@example.com
$this->getApplication()->getKernel()->getContainer()
->get('user_aware.command.user_provider')->setUser($userEntity);
Custom User Providers
BenTools\UserAwareCommandBundle\Provider\UserProviderInterface to fetch users dynamically (e.g., from API tokens):
class ApiTokenUserProvider implements UserProviderInterface
{
public function getUser(): ?User
{
return User::findByToken($_ENV['API_TOKEN']);
}
}
services.yaml:
BenTools\UserAwareCommandBundle\Provider\UserProviderInterface: '@app.api_token_user_provider'
Entity-Specific Logic
UserAwareInterface for commands that modify Blameable entities.$user = $this->getContainer()->get('user_aware.command.user_provider')->getUser();
$entity->setCreatedBy($user);
Blameable Not Configured
createdBy/updatedBy fields are missing or misconfigured, the bundle silently fails. Verify Gedmo Blameable setup:
# config/packages/doctrine.yaml
doctrine:
orm:
mappings:
gedmo_blameable:
type: attribute
prefix: Gedmo\Blameable\Entity
dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Blameable/Entity"
alias: GedmoBlameable
is_bundle: false
User Provider Conflicts
php bin/console cache:clear
Non-Entity Commands
Check User Binding Log the current user in your command:
$user = $this->getContainer()->get('user_aware.command.user_provider')->getUser();
$this->output->writeln(sprintf('Bound user: %s', $user?->getEmail()));
Verify Provider Inspect the active provider:
$provider = $this->getContainer()->get('user_aware.command.user_provider');
$this->output->writeln(get_class($provider));
Custom Default User
Override the system user in config/packages/user_aware_command.yaml:
user_aware_command:
default_user: { id: 1, username: 'default_user', email: 'default@example.com' }
Event Listeners
Subscribe to UserAwareCommandEvent to modify user logic pre-execution:
// src/EventListener/UserAwareListener.php
use BenTools\UserAwareCommandBundle\Event\UserAwareCommandEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserAwareListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
UserAwareCommandEvent::NAME => 'onUserAwareCommand',
];
}
public function onUserAwareCommand(UserAwareCommandEvent $event): void
{
if ($event->getCommandName() === 'app:critical_command') {
$event->setUser($this->fetchCriticalUser());
}
}
}
Testing Mock the user provider in tests:
$container = $this->getContainer();
$container->set('user_aware.command.user_provider', $this->createMock(UserProviderInterface::class));
How can I help you explore Laravel packages today?