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

Storage Utils Laravel Package

akeneo/storage-utils

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require akeneo/storage-utils
    

    Add to composer.json under require (experimental flag noted).

  2. 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
    
  3. 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).

Implementation Patterns

Workflow: Read-Only Data Layer

  1. 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'));
    });
    
  2. 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");
        }
    }
    
  3. Configuration-Driven Storage Load storage from Laravel config (e.g., config/storage.php):

    $storage = new ArrayStorage(config('storage.data'));
    

Extension Points

  • Custom Storage: Implement StorageInterface for new backends (e.g., Redis, DynamoDB).
  • Decorators: Wrap existing storage to add logic (e.g., caching, validation):
    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));
        }
    }
    

Gotchas and Tips

Pitfalls

  1. No Write Operations

    • Methods like set(), delete(), or put() do not exist. Attempting to call them will throw BadMethodCallException.
    • Workaround: Use a separate write-optimized package (e.g., spatie/laravel-cache) for mutations.
  2. Experimental Status

    • API may change without semver compliance. Pin versions strictly:
      "akeneo/storage-utils": "1.0.0"
      
  3. ArrayStorage Limitations

    • ArrayStorage is not persistent across requests. Use for testing or ephemeral data only.

Debugging Tips

  • Verify Implementation: Ensure your storage class implements all StorageInterface methods.
  • Check for Typos: Method names are case-sensitive (get() vs Get()).
  • Test Edge Cases:
    $storage->get('nonexistent_key'); // Returns null (not an exception).
    $storage->has('nonexistent_key'); // Returns false.
    

Configuration Quirks

  • No Built-in Config: The package expects manual initialization (no config/storage.php by default).
  • Type Safety: Return types are mixed for get() and bool for has(). Validate data in your service layer.

Performance

  • all() Method: Avoid calling this in loops or with large datasets—it loads everything into memory.
  • Lazy Loading: Prefer get() for specific keys over all() when possible.
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.
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
spatie/mailcoach-vapor