Installation:
composer require hwavina/hwa-meta
The package auto-registers its service provider, so no manual registration is required unless overriding defaults.
First Use Case: Access metadata utilities via the facade:
use Hwavina\HwaMeta\Facades\HwaMeta;
// Example: Generate a unique ID
$id = Hwavina\HwaMeta\Facades\HwaMeta::generateId();
Where to Look First:
Hwavina\HwaMeta\Facades\HwaMeta (primary entry point).config/hwa_meta.php (customize behavior like ID formats, metadata defaults).generateId(), getMetadata(), sanitize()).Metadata Management:
// Store metadata for a model
HwaMeta::setMetadata('user:123', ['role' => 'admin', 'last_active' => now()]);
// Retrieve metadata
$data = HwaMeta::getMetadata('user:123');
ID Generation:
// Default UUID
$uuid = HwaMeta::generateId();
// Custom format (e.g., "USER-{timestamp}")
HwaMeta::setIdFormat('USER-{timestamp}');
$customId = HwaMeta::generateId();
Data Sanitization:
$cleaned = HwaMeta::sanitize($dirtyInput, 'html'); // Sanitize HTML
Integration with Laravel Components:
class User extends Model {
public function getMetadataAttribute() {
return HwaMeta::getMetadata("user:{$this->id}");
}
}
Route::get('/admin', function () {
HwaMeta::setMetadata('route:admin', ['requires' => 'auth.admin']);
})->middleware('checkRouteMeta');
Caching Metadata:
HwaMeta::setMetadata('temp:key', $data, 3600); // Cache for 1 hour
user:{id}:preferences).public function handle($request, Closure $next) {
HwaMeta::setMetadata('request:'.$request->id, ['ip' => $request->ip()]);
return $next($request);
}
Namespace Collisions:
Hwavina\HwaMeta namespace. Ensure no conflicts with other packages (e.g., custom HwaMeta classes).Metadata Storage:
HwaMeta::setStorage('database'); // Configure in config/hwa_meta.php
ID Format Overrides:
setIdFormat() affects all subsequent calls. Reset to default with:
HwaMeta::setIdFormat(); // Reverts to UUID
Sanitization Limits:
sanitize() method may not cover all edge cases (e.g., nested arrays). Extend the package:
HwaMeta::extend('customSanitize', function ($input) {
// Custom logic
return $input;
});
config/hwa_meta.php for misconfigurations (e.g., wrong storage driver).\Log::info('Metadata:', HwaMeta::getMetadata('key'));
php artisan cache:clear
Custom Storage:
Extend the storage system by implementing Hwavina\HwaMeta\Contracts\StorageInterface:
class DatabaseStorage implements StorageInterface {
public function get($key) { /* ... */ }
public function set($key, $value, $ttl = null) { /* ... */ }
}
Register it in config/hwa_meta.php:
'storage' => \App\Storage\DatabaseStorage::class,
Add Helper Functions: Extend the facade with custom methods:
HwaMeta::extend('hashMetadata', function ($key) {
return hash('sha256', HwaMeta::getMetadata($key));
});
Event Listeners:
Trigger events for metadata changes (e.g., metadata.stored):
HwaMeta::listen('metadata.stored', function ($key, $value) {
\Log::info("Metadata stored for $key");
});
HwaMeta::batch() to reduce I/O for bulk metadata operations:
HwaMeta::batch(function ($meta) {
$meta->set('user:1', ['role' => 'admin']);
$meta->set('user:2', ['role' => 'guest']);
});
null (no expiration). Set explicitly:
HwaMeta::setMetadata('key', $value, 3600); // 1-hour TTL
memory (default), cache, database. Configure in config/hwa_meta.php:
'driver' => env('HWA_META_DRIVER', 'memory'),
How can I help you explore Laravel packages today?