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

Gears Laravel Package

cosmologist/gears

Handy helper library for PHP and Symfony with utility functions for arrays, strings, objects, numbers, files, cache, callables, classes, and Guzzle, plus integrations for Doctrine, Symfony components (Forms, Messenger, Security, Twig, Validator) and value objects (identifiers/UUID).

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add via Composer:

    composer require cosmologist/gears
    

    No Laravel-specific setup is required—it’s a standalone library.

  2. First Use Case Use the Gear class to create a simple state machine or workflow:

    use Cosmologist\Gears\Gear;
    
    $gear = new Gear('my_gear');
    $gear->addState('pending')
         ->addState('processing')
         ->addState('completed')
         ->addTransition('pending', 'processing')
         ->addTransition('processing', 'completed');
    
    $gear->setState('pending');
    $gear->transition('processing'); // Returns true/false
    
  3. Where to Look First

    • Documentation: Check the GitHub README (if available) for core concepts.
    • Source: Browse src/Gear.php for state/transition logic.
    • Symfony Integration: If using Symfony, explore GearsBundle for DI/routing helpers.

Implementation Patterns

Common Workflows

  1. State Machines Model business processes (e.g., order lifecycle):

    $orderGear = new Gear('order');
    $orderGear->addStates(['draft', 'submitted', 'paid', 'shipped']);
    $orderGear->addTransitions([
        ['from' => 'draft', 'to' => 'submitted'],
        ['from' => 'submitted', 'to' => 'paid'],
    ]);
    
  2. Workflow Validation Enforce rules in transitions:

    $gear->addTransition('pending', 'processing', function () {
        return auth()->user()->can('process_items');
    });
    
  3. Event Hooks Trigger actions on state changes:

    $gear->onStateChange(function ($oldState, $newState) {
        event(new StateChanged($oldState, $newState));
    });
    
  4. Laravel Integration Store gears in a database (e.g., gears table) and hydrate them:

    $gear = Gear::fromArray($storedData);
    
  5. Dependency Injection Bind Gear to Laravel’s container:

    $app->bind('gear.my_workflow', function () {
        return new Gear('my_workflow')->configure(...);
    });
    

Gotchas and Tips

Pitfalls

  1. State/Transition Mismatches

    • Issue: Calling transition() with invalid states silently fails (returns false).
    • Fix: Validate transitions explicitly:
      if (!$gear->canTransition('pending', 'processing')) {
          throw new \InvalidArgumentException("Invalid transition");
      }
      
  2. Circular Dependencies

    • Issue: Infinite loops if transitions reference each other.
    • Fix: Use addTransition() with guards or external validation.
  3. Symfony vs. Laravel

    • Issue: GearsBundle may not work out-of-the-box in Laravel.
    • Fix: Use the standalone library or adapt Symfony components manually.
  4. Serialization

    • Issue: Gears aren’t natively JSON-serializable.
    • Fix: Implement JsonSerializable or use toArray():
      $gear->toArray(); // Returns state/transitions as associative array
      

Debugging Tips

  • Log State Changes:
    $gear->onStateChange(function ($old, $new) {
        \Log::debug("State changed from {$old} to {$new}");
    });
    
  • Inspect Gear State:
    $gear->getState(); // Current state
    $gear->getTransitions(); // All allowed transitions
    

Extension Points

  1. Custom Guards Override canTransition() to add logic:

    class CustomGear extends Gear {
        protected function canTransition($from, $to) {
            if ($from === 'pending' && $to === 'processing') {
                return $this->hasEnoughStock();
            }
            return parent::canTransition($from, $to);
        }
    }
    
  2. Database Backend Extend Gear to persist to Eloquent models:

    class DatabaseGear extends Gear {
        public function save() {
            $this->model->state = $this->state;
            $this->model->save();
        }
    }
    
  3. Event Dispatching Use Laravel events for broader integration:

    $gear->onStateChange(function ($old, $new) {
        event(new GearStateChanged($old, $new, $gear->getName()));
    });
    
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