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

Laravel Bakery Laravel Package

simlux/laravel-bakery

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require simlux/laravel-bakery
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="Simlux\LaravelBakery\BakeryServiceProvider"
    
  2. Basic Usage The package provides a Bakery facade to interact with "recipes" (likely a customizable workflow or data transformation system). Start by defining a simple recipe in a migration or service provider:

    use Simlux\LaravelBakery\Facades\Bakery;
    
    // Register a recipe (example: transform user data)
    Bakery::recipe('user_prep', function ($data) {
        return [
            'name' => strtoupper($data['name']),
            'email' => strtolower($data['email']),
        ];
    });
    
  3. First Use Case Apply the recipe to input data:

    $transformed = Bakery::apply('user_prep', [
        'name' => 'John Doe',
        'email' => 'JOHN@EXAMPLE.COM',
    ]);
    // Returns: ['name' => 'JOHN DOE', 'email' => 'john@example.com']
    

Where to Look First

  • Facade: Simlux\LaravelBakery\Facades\Bakery (core API).
  • Config: config/bakery.php (if published) for default settings.
  • Service Provider: Simlux\LaravelBakery\BakeryServiceProvider for registration logic.

Implementation Patterns

1. Recipe Workflows

  • Chaining Recipes: Define sequential transformations:

    Bakery::recipe('user_flow', function ($data) {
        return Bakery::apply('user_prep', $data);
    })->then(function ($data) {
        return ['processed' => true, ...$data];
    });
    
  • Dynamic Recipes: Register recipes conditionally (e.g., based on user roles):

    if (auth()->user()->isAdmin()) {
        Bakery::recipe('admin_prep', fn($data) => [...]);
    }
    

2. Integration with Eloquent

  • Model Observers: Use recipes to sanitize or transform data before/after model events:

    use Simlux\LaravelBakery\Facades\Bakery;
    
    class UserObserver {
        public function saving(User $user) {
            $user->attributes = Bakery::apply('user_prep', $user->attributes);
        }
    }
    
  • API Resources: Apply recipes in toArray() or toResponse():

    public function toArray($request) {
        return Bakery::apply('api_response', parent::toArray($request));
    }
    

3. CLI Automation

  • Artisan Commands: Use recipes in scheduled tasks or commands:
    use Simlux\LaravelBakery\Facades\Bakery;
    
    class ProcessUsersCommand extends Command {
        protected $signature = 'users:process';
        public function handle() {
            $users = User::all();
            foreach ($users as $user) {
                $user->update(Bakery::apply('user_prep', $user->toArray()));
            }
        }
    }
    

4. Testing

  • Mock Recipes: Override recipes in tests:
    Bakery::shouldReceive('apply')
        ->once()
        ->with('user_prep', ['name' => 'Test'])
        ->andReturn(['name' => 'TEST']);
    

Gotchas and Tips

Pitfalls

  1. Recipe Overwriting:

    • Recipes registered later overwrite earlier ones with the same name. Use unique names or namespaces (e.g., user_prep_v2).
    • Fix: Check registered recipes with Bakery::recipes().
  2. Circular Dependencies:

    • Recipes calling each other recursively may cause infinite loops.
    • Fix: Use Bakery::once() to cache results:
      Bakery::recipe('safe_recipe', fn($data) => Bakery::once('safe_recipe', $data, fn() => [...]));
      
  3. Data Mutability:

    • Recipes modify data by reference if not careful. Always return new arrays/objects.
    • Fix: Use array_merge or spread operator:
      return [...$data, 'new_field' => 'value'];
      

Debugging

  • Log Recipes: Enable debug mode in config:

    'debug' => env('BAKERY_DEBUG', false),
    

    Logs recipe calls to storage/logs/laravel.log.

  • Inspect Recipes: Dump all registered recipes:

    dd(Bakery::recipes());
    

Extension Points

  1. Custom Recipe Storage:

    • Override the default recipe storage (e.g., use a database):
      Bakery::setStorage(app(CustomRecipeStorage::class));
      
  2. Recipe Events:

    • Listen for recipe execution:
      event(new RecipeExecuted('user_prep', $data, $result));
      
  3. Recipe Validation:

    • Validate input data before processing:
      Bakery::recipe('validated_prep', function ($data) {
          $validator = Validator::make($data, ['name' => 'required|string']);
          if ($validator->fails()) throw new \Exception($validator->errors());
          return [...];
      });
      

Performance Tips

  • Cache Recipes: Cache recipe results for immutable inputs:

    $cacheKey = md5(serialize($data));
    return Cache::remember($cacheKey, 60, fn() => Bakery::apply('recipe_name', $data));
    
  • Lazy Loading: Defer recipe registration until first use:

    if (!Bakery::hasRecipe('lazy_recipe')) {
        Bakery::recipe('lazy_recipe', fn($data) => [...]);
    }
    
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