snapflowio/registry

A simple and lightweight library to pass resources around applications.

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/snapflowio/registry

v0.1.0 2025-12-17 01:49 UTC

This package is auto-updated.

Last update: 2025-12-17 01:50:13 UTC


README

A simple and lightweight library to pass resources around applications.

Installation

composer require snapflowio/registry

Quick Start

<?php

use Snapflow\Registry\Registry;

$registry = new Registry();

// Register a service with a factory callback
$registry->set('database', function () {
    return new PDO('mysql:host=localhost;dbname=myapp', 'user', 'pass');
});

// Get the service (lazy initialization, only created when first accessed)
$db = $registry->get('database');

// Subsequent calls return the same instance
$sameDb = $registry->get('database');

// Register services that always create fresh instances
$registry->set('timestamp', fn () => microtime(true), fresh: true);

// Use contexts for isolation (e.g., per-request, per-test)
$registry->context('testing')->set('database', function () {
    return new PDO('sqlite::memory:');
});

// Check if a service is registered
if ($registry->has('database')) {
    $db = $registry->get('database');
}

// Method chaining
$registry
    ->context('api')
    ->set('logger', fn () => new Logger())
    ->set('cache', fn () => new Cache());

// Info and cleanup
$services = $registry->getRegistered(); // ['database', 'timestamp', 'logger', 'cache']
$currentContext = $registry->getContext(); // 'api'
$registry->clear(); // Clear instances in current context
$registry->remove('cache'); // Remove service completely

License

This library is available under the MIT License.

Copyright

Copyright (c) 2025 Snapflow