Laravel Manager

repository·5.3·Indexed 18 days ago

https://github.com/grahamcampbell/laravel-manager

A package providing a standardized way to implement manager-style functionality in Laravel packages, facilitating the creation and management of multiple connections for different services or drivers. It includes the ManagerInterface, AbstractManager for custom manager implementation, and supports PHP 7.4-8.5 and Laravel 8-13.

Tokens
574
Snippets
1
Records
4
Agent score
14%

What's inside laravel-manager

  1. Understand the ConnectorInterface

    5.3

    The ConnectorInterface defines a single public method:

    • connect(array $config): Accepts an array of configuration settings and returns a connection instance.

    While this package does not use this interface internally, it is a standard used by compatible packages like Laravel Flysystem to ensure consistent connection creation.

  2. Install Laravel Manager via Composer

    5.3

    To install the latest version of Laravel Manager, use Composer. This version requires PHP 7.4-8.5 and supports Laravel 8-13.

    Note: There are no service providers to register manually after installation.

    $ composer require "graham-campbell/manager:^5.3"
  3. Extend AbstractManager to create a custom manager

    5.3

    The AbstractManager class provides a base implementation of ManagerInterface. To use it, extend this class and implement the following two protected abstract methods:

    1. createConnection(array $config): Called with the specific connection configuration. It must return a connection instance.
    2. getConfigName(): Must return the name of the connection configuration (e.g., 'yourname\yourpackage').

    Pro-tip: AbstractManager uses the __call magic method, allowing you to call methods directly on the manager instance instead of accessing the connection first. For example, instead of $manager->connection()->methodName(), you can use $manager->methodName().

  4. Implement the ManagerInterface

    5.3

    The ManagerInterface defines the public contract for a manager class. If you are building a custom manager, you must implement the following methods:

    • connection(?string $name): Returns a connection instance for the given name, reusing an existing connection from the pool if available.
    • reconnect(?string $name): Forces a re-connection and returns the connection instance.
    • disconnect(?string $name): Removes the specified connection from the pool.
    • getConnectionConfig(string $name): Returns the configuration for the specified connection.
    • getDefaultConnection(): Returns the default connection name as specified in the config.
    • setDefaultConnection(string $name): Sets the default connection name.
    • extend(string $nameOrDriver, callable $callback): Adds custom connection creation methods on the fly. The callback must return a connection.
    • getConnections(): Returns an array of all connections currently in the pool.