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

User Commands Bundle Laravel Package

dwo/user-commands-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require dwo/user-commands-bundle:@dev
    

    Register the bundle in config/bundles.php:

    Dwo\UserCommandsBundle\DwoUserCommandsBundle::class => ['all' => true],
    
  2. First Use Case: Run the list command to verify functionality:

    php artisan dwo:user:list
    
    • This should display a table of existing users (if any) in your database.
    • Confirm the command works with your existing User model (assumes Eloquent).
  3. Where to Look First:

    • Command Structure: Check src/Command/ in the bundle for core logic.
    • Configuration: Look for config/user_commands.php (if provided) or check the bundle’s Resources/config/ for defaults.
    • Service Container: Inspect how the bundle binds services (e.g., UserRepository or UserManager).

Implementation Patterns

1. Command Workflows

Create User

php artisan dwo:user:create --name="John Doe" --email="john@example.com" --password="secure123"
  • Pattern: Use interactive prompts if arguments are omitted (e.g., --interactive flag).
  • Validation: The bundle likely validates inputs (e.g., unique email, password strength). Extend via events or custom validators.
  • Post-Creation: Hook into UserCreated events (if supported) or override the command class to add logic (e.g., send welcome email).

Update User

php artisan dwo:user:update 1 --name="John Updated"
  • Pattern: Pass the user ID as an argument. Use --field=value pairs for partial updates.
  • Integration: Chain updates with other services (e.g., update a profile service after user data changes).

List Users

php artisan dwo:user:list --limit=10 --sort=-created_at
  • Pattern: Use --limit, --offset, and --sort for pagination/sorting.
  • Extension: Filter by role/active status with --filter=role:admin.

2. Integration Tips

Customize the User Model

  • Override the bundle’s default User model by binding your model in AppServiceProvider:
    public function register()
    {
        $this->app->bind(
            \Dwo\UserCommandsBundle\Model\UserInterface::class,
            \App\Models\User::class
        );
    }
    

Add Custom Fields

  • Extend the User model with additional fields (e.g., avatar). Update the create/update commands by:
    1. Creating a custom command class (e.g., app/Console/Commands/CustomUserCreateCommand).
    2. Extending the original command and overriding handle():
      public function handle()
      {
          parent::handle();
          $this->user->avatar = $this->option('avatar');
          $this->user->save();
      }
      

API Integration

  • Use the commands in automated scripts (e.g., CI/CD) or wrap them in a service:
    $user = (new CreateUserCommand())
        ->setName('API User')
        ->setEmail('api@example.com')
        ->handle();
    

Testing

  • Mock the UserRepository or use a test database:
    $this->artisan('dwo:user:create', [
        '--name' => 'Test User',
        '--email' => 'test@example.com',
        '--password' => 'password',
    ])->assertExitCode(0);
    

Gotchas and Tips

Pitfalls

  1. No Default Config:

    • The bundle may lack a config/user_commands.php. Check for hardcoded values (e.g., password rules) in the command classes.
    • Fix: Create a config file and publish it:
      php artisan vendor:publish --tag=user-commands-config
      
  2. Model Assumptions:

    • Assumes a User model with specific fields (name, email, password). Mismatches will cause errors.
    • Fix: Override the model binding (see Implementation Patterns).
  3. Interactive Mode Quirks:

    • Interactive prompts might not work in non-TTY environments (e.g., CI).
    • Fix: Add a --non-interactive flag or use --force for scripts.
  4. No Transaction Support:

    • Commands may not wrap operations in transactions, risking partial updates.
    • Fix: Extend the command to use DB::transaction():
      DB::transaction(function () use ($user) {
          $user->update($data);
          // Other operations...
      });
      
  5. Password Handling:

    • Plain-text passwords might be logged or exposed in command output.
    • Fix: Use Laravel’s Hash::make() and avoid logging sensitive data.

Debugging Tips

  1. Command Output:

    • Enable verbose mode for debugging:
      php artisan dwo:user:create --verbose
      
    • Check for errors in the handle() method of the command class.
  2. Database Issues:

    • Ensure the users table matches the expected schema. Run:
      php artisan schema:dump
      
      to compare.
  3. Service Binding:

    • If commands fail silently, verify bindings:
      php artisan container:list | grep User
      

Extension Points

  1. Events:

    • Listen for UserCreated, UserUpdated (if the bundle dispatches them). Add to EventServiceProvider:
      protected $listen = [
          \Dwo\UserCommandsBundle\Events\UserCreated::class => [
              \App\Listeners\SendWelcomeEmail::class,
          ],
      ];
      
  2. Custom Validators:

    • Override validation logic by binding a custom validator to the container:
      $this->app->bind(
          \Dwo\UserCommandsBundle\Validator\UserValidator::class,
          \App\Validators\CustomUserValidator::class
      );
      
  3. Console Output:

    • Customize output tables by extending the command and overriding configure():
      protected function configure()
      {
          parent::configure();
          $this->tableHeaders = ['ID', 'Name', 'Email', 'Role'];
      }
      
  4. Localization:

    • Translate command messages by publishing the bundle’s language files:
      php artisan vendor:publish --tag=user-commands-translations
      
      Then add translations to resources/lang/.
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky