au9500/laravel-auto-singleton

PHP 8 attribute to auto-register Laravel singletons.

Installs: 20

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/au9500/laravel-auto-singleton

v1.0.0 2025-12-02 20:04 UTC

This package is auto-updated.

Last update: 2025-12-02 20:05:22 UTC


README

Automatically register classes as singletons in the Laravel service container using a clean PHP 8 attribute.
No more manual container bindings โ€” just add an attribute and you're done. โšก

โœจ Features

  • ๐Ÿงฉ Register any class as a singleton using a simple PHP 8 attribute
  • ๐ŸŽฏ Optional: Bind an interface to its implementation
  • ๐Ÿ” Scans only the directories you choose
  • ๐Ÿ”Œ Zero boilerplate โ€” no service provider bindings required
  • โš™๏ธ Respects Laravel config and auto-discovery
  • ๐Ÿš€ Works with Laravel 10, 11, 12
  • ๐Ÿชถ Lightweight and dependency-free

๐Ÿ“ฆ Installation

Install via Composer:

composer require au9500/laravel-auto-singleton

Publish the configuration file:

php artisan vendor:publish --tag=auto-singleton-config

This creates:

config/auto-singleton.php

โš™๏ธ Configuration

Example contents of config/auto-singleton.php:

return [
    'enabled' => true,

    'directories' => [
        base_path('app/Services'),
        base_path('app/Domain'),
    ],
];

directories determines where attribute scanning is performed.

๐Ÿ›  Usage

1. ๐Ÿ”’ Register a simple singleton

use Au9500\LaravelAutoSingleton\Attributes\AutoSingleton;

#[AutoSingleton]
class PaymentService {
}

Inject anywhere:

public function __construct(PaymentService $service) {
    $this->service = $service;
}

Laravel now resolves this service as a singleton.

2. ๐ŸŽญ Bind an interface

interface UserRepository {}

#[AutoSingleton(UserRepository::class)]
class EloquentUserRepository implements UserRepository {
}

Now you may type-hint the interface:

public function __construct(UserRepository $repo) {
    $this->repo = $repo;
}

Laravel automatically binds the interface โ†’ implementation as a singleton.

3. ๐Ÿšซ Disable auto-registration

In your config:

'enabled' => false,

This disables all automatic registration.

๐Ÿ” How It Works

  1. Laravel boots and loads this package's ServiceProvider
  2. Config tells the provider which directories to scan
  3. The Composer classmap is inspected
  4. Every class in the configured directories is checked via Reflection
  5. If it has the AutoSingleton attribute:
    • It is registered as a singleton
  6. If the attribute specifies an abstract/interface:
    • That abstract is bound to the class

No extra code. No boilerplate. Zero maintenance.

๐Ÿงช Testing

The package ships with full PHPUnit and Orchestra Testbench support.
Run the test suite with:

vendor/bin/phpunit

Tests cover:

  • โœ”๏ธ Basic singleton registration
  • โœ”๏ธ Interface binding
  • โœ”๏ธ Ignoring non-attributed classes
  • โœ”๏ธ Config disabling behavior
  • โœ”๏ธ Directory filtering

๐Ÿ›ก Requirements

  • PHP 8.2+
  • Laravel 10, 11, or 12

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Open a Pull Request

๐Ÿ“„ License

MIT License.