Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Sylius User Command Bundle Laravel Package

dos/sylius-user-command-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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],
    ];
    
  2. 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).

  3. 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
    

Implementation Patterns

Workflow Integration

  1. Command-Based User Management Use the bundle’s commands for repetitive tasks:

    • Bulk User Creation: Export a CSV, then loop through rows to create users:
      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
      
    • Role Assignment: Update existing users:
      php bin/console sylius:user-command:update --id=1 --role=ROLE_SUPER_ADMIN
      
  2. 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
    
  3. 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;
        }
    }
    
  4. 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()]);
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Role Validation

    • The bundle assumes roles exist in Sylius’ role table. If roles are missing, commands will fail silently. Fix: Pre-populate roles or validate roles exist before execution.
    • Example validation in a custom command:
      $role = $this->roleRepository->findOneBy(['role' => $input->getOption('role')]);
      if (!$role) {
          throw new \RuntimeException('Role not found');
      }
      
  2. Password Hashing

    • The bundle uses Symfony’s 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
      
  3. Email Uniqueness

    • By default, the bundle does not enforce email uniqueness. Add a unique constraint in your User entity or validate manually:
      $existingUser = $this->userRepository->findOneBy(['email' => $email]);
      if ($existingUser) {
          throw new \RuntimeException('Email already exists');
      }
      
  4. Command Output Parsing

    • CLI commands return raw output. For programmatic use, parse output or use --format=json if supported (extend the bundle if needed).

Tips

  1. 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"]
    
  2. 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());
    }
    
  3. Performance

    • Batch user creation to reduce database load:
      php bin/console sylius:user-command:batch-create --file=users.csv --batch-size=50
      
    • Optimize queries by adding indexes to email and role fields in the user table.
  4. Extension Points

    • Override the UserCommandFactory to customize user creation logic:
      // config/services.yaml
      Dos\SyliusUserCommandBundle\Factory\UserCommandFactory:
          arguments:
              $customLogic: '@app.custom_user_logic'
      
    • Extend the UserCommandInterface to add custom fields (e.g., phoneNumber).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle