How aniket11prajapati/laravel-wallet works
Project type and purpose
• This is a Laravel package (composer library) that adds wallet functionality to any Laravel application.
• Its purpose is to provide credit, debit, refund operations on a “wallet” attached to an existing User model, with transaction history, safe concurrent updates via cache locks, and full configurability.
File structure
• Top-level files: README.md, composer.json/lock, indicating a PHP package.
• config/wallet.php: package configuration published into the host app.
• database/migrations/*.php: migration(s) to create wallet tables.
• src/Contracts/WalletInterface.php: interface defining wallet operations.
• src/Models/{Transaction,TransactionHistory}.php: Eloquent models for the ledger.
• src/Services/WalletManagerService.php: the service implementing business logic.
• src/Traits/HasWallet.php: a trait that injects wallet methods into the User class.
• src/WalletServiceProvider.php: registers bindings, publishes config and migrations.
• vendor/: external dependencies (Laravel framework, cache drivers).
README for additional insights
• Key features: credit/debit/refund, transaction tracking, cache locks, extensible schema, DB‐agnostic.
• Usage: You add use HasWallet; to your User model. The package then wires up everything via the service provider.
Components
a. Host Laravel Application
• User Model (Illuminate\Auth\User)
• Laravel Container & Facades (Cache, DB, Config)
b. Wallet Package
• WalletServiceProvider
• Config: wallet.php
• Contracts: WalletInterface
• Trait: HasWallet
• Service: WalletManagerService (implements WalletInterface)
• Models: Transaction, TransactionHistory
• Migrations: create_wallet_tables
c. External Services & Storage
• Database (relational or non‐relational, table schemas for transactions & history)
• Cache Store (Redis, Memcached, etc.) for locks
Relationships and interactions
• On app boot, Laravel Container loads WalletServiceProvider → publishes config & migrations → binds WalletInterface to WalletManagerService.
• Developer’s User model uses HasWallet trait → trait methods (credit(), debit(), refund()) delegate to WalletManagerService via the container binding.
• WalletManagerService obtains a cache lock (Laravel Cache facade) to enforce concurrency safety → executes DB transaction (Eloquent models) to write to transactions & transaction_histories tables → releases lock.
• Config values (e.g., table names, cache lock keys) are read from config/wallet.php.
Architectural patterns & design principles
• Service Provider / Inversion of Control (IoC): decouples WalletInterface from implementation.
• Trait injection: composable extension of the User model without modifying core.
• Repository/Service pattern: business logic in WalletManagerService.
• Database migrations for schema management.
• Cache lock pattern for safe concurrent updates.
Diagramming recommendations
• App boot → WalletServiceProvider → publishes config & migrations
• User.credit() → HasWallet → WalletManagerService → Cache Lock → DB Transaction → Transaction Model
• Green for host app components (User Model, Container)
• Blue for package internals (Contracts, Service, Trait)
• Orange for external systems (Database, Cache)
Detail level
• Don’t oversimplify: show all four major package directories (Contracts, Models, Services, Traits) and the ServiceProvider.
• Published config and migrations as separate elements, since they’re key integration points.
• Both synchronous flows (method calls, DB writes) and asynchronous or lock patterns (cache lock acquisition/release).