dos/sylius-user-command-bundle
Installation
Add the bundle to your composer.json:
composer require dos/sylius-user-command-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Dos\SyliusUserCommandBundle\DosSyliusUserCommandBundle::class => ['all' => true],
];
Configuration Publish the default configuration:
php bin/console sylius:user-command:config:dump
Update config/packages/dos_sylius_user_command.yaml as needed (e.g., adjust command paths, default roles, or email templates).
First Use Case Trigger a user creation command via CLI:
php bin/console sylius:user-command:create [email protected] --password=secure123 --role=ROLE_ADMIN
Verify the user exists in the database:
php bin/console sylius:user-command:list
Command-Based User Management Use the bundle’s commands for repetitive tasks:
php bin/console sylius:user-command:create [email protected] --password=pass1 --role=ROLE_CUSTOMER
php bin/console sylius:user-command:create [email protected] --password=pass2 --role=ROLE_CUSTOMER
php bin/console sylius:user-command:update --id=1 --role=ROLE_SUPER_ADMIN
Event-Driven Triggers
Hook into Sylius events (e.g., sylius.user.created) to automate post-creation actions (e.g., sending welcome emails):
# config/packages/dos_sylius_user_command.yaml
dos_sylius_user_command:
events:
on_user_created: App\EventListener\SendWelcomeEmailListener
Custom Command Extensions Extend the bundle’s commands by creating your own:
// src/Command/CustomUserCommand.php
namespace App\Command;
use Dos\SyliusUserCommandBundle\Command\AbstractUserCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CustomUserCommand extends AbstractUserCommand
{
protected function configure(): void
{
$this->setName('sylius:user-command:custom-action');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
// Custom logic (e.g., validate email domains)
return Command::SUCCESS;
}
}
API-Driven User Management
Expose commands via API using Symfony’s CommandHandler or create a custom controller:
// src/Controller/UserCommandController.php
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Process\Process;
class UserCommandController extends AbstractController
{
public function createUser(Request $request): JsonResponse
{
$process = new Process(['php', 'bin/console', 'sylius:user-command:create', '--email=' . $request->get('email')]);
$process->run();
return new JsonResponse(['status' => $process->isSuccessful()]);
}
}
Role Validation
role table. If roles are missing, commands will fail silently. Fix: Pre-populate roles or validate roles exist before execution.$role = $this->roleRepository->findOneBy(['role' => $input->getOption('role')]);
if (!$role) {
throw new \RuntimeException('Role not found');
}
Password Hashing
UserPasswordHasherInterface. If you override the hasher (e.g., for custom password policies), ensure compatibility:
# config/packages/security.yaml
services:
App\Security\UserPasswordHasher:
arguments:
$userClass: App\Entity\User
Email Uniqueness
User entity or validate manually:
$existingUser = $this->userRepository->findOneBy(['email' => $email]);
if ($existingUser) {
throw new \RuntimeException('Email already exists');
}
Command Output Parsing
--format=json if supported (extend the bundle if needed).Logging Enable command logging for debugging:
# config/packages/monolog.yaml
handlers:
sylius_user_command:
type: stream
path: "%kernel.logs_dir%/sylius_user_command.log"
level: debug
channels: ["sylius_user_command"]
Testing
Mock the command bus or use Process to test CLI interactions:
// tests/Command/UserCommandTest.php
public function testCreateUserCommand()
{
$process = new Process(['php', 'bin/console', 'sylius:user-command:create', '[email protected]']);
$process->run();
$this->assertStringContainsString('User created', $process->getOutput());
}
Performance
php bin/console sylius:user-command:batch-create --file=users.csv --batch-size=50
email and role fields in the user table.Extension Points
UserCommandFactory to customize user creation logic:
// config/services.yaml
Dos\SyliusUserCommandBundle\Factory\UserCommandFactory:
arguments:
$customLogic: '@app.custom_user_logic'
UserCommandInterface to add custom fields (e.g., phoneNumber).How can I help you explore Laravel packages today?