Installation Add the package via Composer:
composer require aeruz/app-registry-common
Publish the package's configuration (if applicable) with:
php artisan vendor:publish --provider="Aeruz\AppRegistryCommon\AppRegistryCommonServiceProvider"
First Use Case: Defining a Registry Model
Extend the base RegistryModel trait to create a custom registry entry:
use Aeruz\AppRegistryCommon\Models\RegistryModel;
class MyApp extends RegistryModel
{
protected $registryKey = 'my_app'; // Unique identifier for the registry
protected $registryName = 'My Application'; // Human-readable name
}
Registering the Model Bind your model to the registry in a service provider:
use Aeruz\AppRegistryCommon\Facades\AppRegistry;
public function register()
{
AppRegistry::register(MyApp::class);
}
Retrieving Entries Fetch all registered apps or a specific one:
$apps = AppRegistry::all();
$myApp = AppRegistry::get('my_app');
RegistryModel as a base for all registry entries.
class Service extends RegistryModel
{
protected $registryKey = 'service_x';
protected $registryName = 'Service X';
protected $version = '1.0.0';
}
registryKey, registryName) and leverages built-in validation.// In a service provider or boot method
AppRegistry::register(Service::class);
AppRegistry::register(AnotherService::class);
AppRegistry::registerMultiple([Class1::class, Class2::class]) for bulk registration.use Aeruz\AppRegistryCommon\Facades\AppRegistry;
class AppController extends Controller
{
public function showApps()
{
$apps = AppRegistry::all();
return view('apps.index', compact('apps'));
}
}
$this->app->bind(
\Aeruz\AppRegistryCommon\Repositories\AppRegistryRepository::class,
\Aeruz\AppRegistryCommon\Repositories\EloquentAppRegistryRepository::class
);
Registered, Unregistered).
use Aeruz\AppRegistryCommon\Events\RegistryEvent;
Event::listen(RegistryEvent::REGISTERED, function ($event) {
Log::info("Registered app: {$event->app->registryName}");
});
use Aeruz\AppRegistryCommon\Rules\RegistryKey;
$validator = Validator::make($data, [
'registry_key' => ['required', new RegistryKey],
]);
getRules() in your RegistryModel for model-specific validation.namespace App\Http\Resources;
use Aeruz\AppRegistryCommon\Models\RegistryModel;
use Illuminate\Http\Resources\Json\JsonResource;
class AppResource extends JsonResource
{
public function toArray($request)
{
return [
'key' => $this->registryKey,
'name' => $this->registryName,
'version' => $this->version ?? null,
];
}
}
Route::get('/apps', function () {
return AppResource::collection(AppRegistry::all());
});
Duplicate Registry Keys
registryKey will overwrite the first entry.if (AppRegistry::has('duplicate_key')) {
throw new \InvalidArgumentException("Key 'duplicate_key' already exists.");
}
Missing registryKey or registryName
RegistryModel subclasses. Omitting them may cause runtime errors.protected $registryKey = '...';
protected $registryName = '...';
Repository Mismatch
EloquentAppRegistryRepository) without setting up the database schema.php artisan vendor:publish --tag="app-registry-migrations"
php artisan migrate
Caching Stale Data
CacheAppRegistryRepository), stale data may persist after updates.Event::listen(RegistryEvent::UPDATED, function () {
Cache::forget('app_registry');
});
Enable Logging Configure the package to log registry operations:
'logging' => [
'enabled' => true,
'channel' => 'single',
],
in the published config.
Inspect Registered Apps Dump the registry state for debugging:
dd(AppRegistry::all()->toArray());
Override Repository for Testing Mock the repository in tests to avoid database dependencies:
$this->app->instance(
\Aeruz\AppRegistryCommon\Repositories\AppRegistryRepository::class,
Mockery::mock(\Aeruz\AppRegistryCommon\Repositories\AppRegistryRepository::class)
);
Custom Repository Implement your own repository (e.g., for Redis or a custom storage backend):
use Aeruz\AppRegistryCommon\Repositories\AppRegistryRepository;
class RedisAppRegistryRepository implements AppRegistryRepository
{
public function all(): Collection
{
return collect(Redis::hgetall('app_registry'));
}
// Implement other methods...
}
Bind it in a service provider:
$this->app->bind(
AppRegistryRepository::class,
RedisAppRegistryRepository::class
);
Custom Events Extend the event system for domain-specific logic:
Event::listen(RegistryEvent::REGISTERED, function ($event) {
// Custom logic (e.g., notify a queue)
Dispatch(new ProcessAppRegistration($event->app));
});
Model Observers
Add observers to RegistryModel for automatic side effects:
class AppRegisteredObserver
{
public function registered(RegistryModel $model)
{
// Example: Log to a custom table
CustomLog::create(['action' => 'app_registered', 'key' => $model->registryKey]);
}
}
Register the observer in a service provider:
RegistryModel::observe(AppRegisteredObserver::class);
API Middleware Restrict access to registry endpoints:
Route::middleware(['auth:api', 'can:view,app'])->get('/apps', function () {
return AppRegistry::all();
});
How can I help you explore Laravel packages today?