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).
Installation Add via Composer:
composer require cosmologist/gears
No Laravel-specific setup is required—it’s a standalone library.
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
Where to Look First
src/Gear.php for state/transition logic.GearsBundle for DI/routing helpers.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'],
]);
Workflow Validation Enforce rules in transitions:
$gear->addTransition('pending', 'processing', function () {
return auth()->user()->can('process_items');
});
Event Hooks Trigger actions on state changes:
$gear->onStateChange(function ($oldState, $newState) {
event(new StateChanged($oldState, $newState));
});
Laravel Integration
Store gears in a database (e.g., gears table) and hydrate them:
$gear = Gear::fromArray($storedData);
Dependency Injection
Bind Gear to Laravel’s container:
$app->bind('gear.my_workflow', function () {
return new Gear('my_workflow')->configure(...);
});
State/Transition Mismatches
transition() with invalid states silently fails (returns false).if (!$gear->canTransition('pending', 'processing')) {
throw new \InvalidArgumentException("Invalid transition");
}
Circular Dependencies
addTransition() with guards or external validation.Symfony vs. Laravel
GearsBundle may not work out-of-the-box in Laravel.Serialization
JsonSerializable or use toArray():
$gear->toArray(); // Returns state/transitions as associative array
$gear->onStateChange(function ($old, $new) {
\Log::debug("State changed from {$old} to {$new}");
});
$gear->getState(); // Current state
$gear->getTransitions(); // All allowed transitions
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);
}
}
Database Backend
Extend Gear to persist to Eloquent models:
class DatabaseGear extends Gear {
public function save() {
$this->model->state = $this->state;
$this->model->save();
}
}
Event Dispatching Use Laravel events for broader integration:
$gear->onStateChange(function ($old, $new) {
event(new GearStateChanged($old, $new, $gear->getName()));
});
How can I help you explore Laravel packages today?