eloquent/liberator
Auto-generate RESTful CRUD controllers, routes, and API resources for Laravel apps from Eloquent models. Liberator helps you scaffold consistent endpoints quickly, reducing boilerplate while keeping generated code editable and aligned with your existing models and conventions.
Installation:
composer require eloquent/liberator
Add to composer.json under require-dev if using for testing.
First Use Case:
use Eloquent\Liberator\Liberator;
$user = new User(); // Assume this is a model with private/protected properties
$liberator = new Liberator($user);
// Access private property
$email = $liberator->get('email');
// Set private property
$liberator->set('email', '[email protected]');
Where to Look First:
src/Liberator.php for core functionalitytests/ for edge-case examplesTesting Private/Protected Logic:
public function testPrivateMethod()
{
$user = new User();
$liberator = new Liberator($user);
// Test private method
$result = $liberator->call('calculateBonus', [100]);
$this->assertEquals(10, $result);
}
Mocking in Unit Tests:
$liberator = new Liberator($this->getMockBuilder(User::class)->getMock());
$liberator->set('isAdmin', true);
Dynamic Property Access:
// Bypass visibility for dynamic properties
$liberator->set('dynamicProp', 'value');
createApplication() (Laravel):
$app->singleton(Liberator::class, function ($app) {
return new Liberator($app->make(User::class));
});
$reflection = new \ReflectionClass($user);
$property = $reflection->getProperty('privateField');
$property->setAccessible(true);
$liberator->set($property->getName(), 'value');
$factory = new Liberator(new User());
$factory->set('password', Hash::make('secret'));
Security Risks:
if (!app()->environment('testing')) {
throw new \RuntimeException('Liberator not allowed in production.');
}
Performance Overhead:
Liberator instances for repeated use:
static $cachedLiberator = null;
if (!$cachedLiberator) {
$cachedLiberator = new Liberator($user);
}
Static Properties:
$liberator->callStatic('setStaticValue', ['key', 'value']);
Magic Methods:
__get(), __set(), or __call() in the target class.if (!$liberator->has('nonExistentMethod')) {
throw new \InvalidArgumentException('Method does not exist.');
}
$liberator->getVisibility('privateField'); // Returns 'private', 'protected', or 'public'
$liberator->setDebug(true);
// Logs all access attempts to storage/logs/liberator.log
$liberator = new Liberator($user, [
'allowedProperties' => ['email', 'name'],
'allowedMethods' => ['getEmail', 'setName']
]);
$liberator->onAccess(function ($property, $value) {
// Log or validate before access
});
Liberator to add domain-specific logic:
class UserLiberator extends Liberator {
public function hashPassword($password) {
return $this->call('hashPassword', [$password]);
}
}
How can I help you explore Laravel packages today?