Installation
composer require akeneo/storage-utils
Add to composer.json under require (experimental flag noted).
First Use Case: Basic Read-Only Storage
use Akeneo\StorageUtils\Storage\StorageInterface;
use Akeneo\StorageUtils\Storage\ArrayStorage;
// Initialize with a simple array-backed storage
$storage = new ArrayStorage(['key1' => 'value1', 'key2' => 'value2']);
// Read data (only supported operation)
$value = $storage->get('key1'); // Returns 'value1'
$exists = $storage->has('key1'); // Returns true
Where to Look First
StorageInterface: Core contract defining get(), has(), and all() methods.ArrayStorage: Default implementation for testing/quick prototyping.DoctrineStorage: Example for database integration (if extended later).Dependency Injection
Bind the interface to a concrete storage (e.g., ArrayStorage, custom implementation):
$container->bind(StorageInterface::class, function () {
return new ArrayStorage(config('app.storage_data'));
});
Service Layer Integration Use in services to abstract data sources (e.g., config, cache, external APIs):
class UserService {
public function __construct(private StorageInterface $storage) {}
public function getUserName(int $id): string {
return $this->storage->get("user.$id.name");
}
}
Configuration-Driven Storage
Load storage from Laravel config (e.g., config/storage.php):
$storage = new ArrayStorage(config('storage.data'));
StorageInterface for new backends (e.g., Redis, DynamoDB).class CachedStorage implements StorageInterface {
public function __construct(private StorageInterface $storage) {}
public function get(string $key) {
return Cache::remember("storage.$key", 3600, fn() => $this->storage->get($key));
}
}
No Write Operations
set(), delete(), or put() do not exist. Attempting to call them will throw BadMethodCallException.spatie/laravel-cache) for mutations.Experimental Status
"akeneo/storage-utils": "1.0.0"
ArrayStorage Limitations
ArrayStorage is not persistent across requests. Use for testing or ephemeral data only.StorageInterface methods.get() vs Get()).$storage->get('nonexistent_key'); // Returns null (not an exception).
$storage->has('nonexistent_key'); // Returns false.
config/storage.php by default).mixed for get() and bool for has(). Validate data in your service layer.all() Method: Avoid calling this in loops or with large datasets—it loads everything into memory.get() for specific keys over all() when possible.How can I help you explore Laravel packages today?