Skip to content

Manager Interface

The Manager interface API reference.

use Cline\Manager\Contracts\ManagerInterface;
interface ManagerInterface
{
/**
* Get a connection instance.
*/
public function connection(?string $name = null): mixed;
/**
* Reconnect to a given connection.
*/
public function reconnect(?string $name = null): mixed;
/**
* Disconnect from a given connection.
*/
public function disconnect(?string $name = null): void;
/**
* Get the default connection name.
*/
public function getDefaultConnection(): string;
/**
* Set the default connection name.
*/
public function setDefaultConnection(string $name): void;
}

Get a connection instance by name, or the default connection.

// Get default connection
$connection = $manager->connection();
// Get named connection
$connection = $manager->connection('secondary');
// Connections are cached - same instance returned
$a = $manager->connection('main');
$b = $manager->connection('main');
assert($a === $b); // true

Close and reopen a connection.

// Reconnect default
$manager->reconnect();
// Reconnect specific
$manager->reconnect('main');
// Returns the new connection
$fresh = $manager->reconnect('main');

Close a connection and remove it from the cache.

// Disconnect default
$manager->disconnect();
// Disconnect specific
$manager->disconnect('main');
// Next connection() call creates new instance
$manager->disconnect('main');
$new = $manager->connection('main'); // Fresh connection

Get the name of the default connection.

$default = $manager->getDefaultConnection();
// e.g., "main" or "mysql"

Change the default connection.

$manager->setDefaultConnection('secondary');
// Now connection() without args uses 'secondary'
$connection = $manager->connection(); // Returns 'secondary' connection
interface ExtendedManagerInterface extends ManagerInterface
{
/**
* Get all active connections.
*/
public function getConnections(): array;
/**
* Check if a connection exists.
*/
public function hasConnection(string $name): bool;
/**
* Add a custom driver/connector.
*/
public function extend(string $driver, callable $callback): void;
/**
* Get connection configuration.
*/
public function getConnectionConfig(string $name): array;
}
class QueueManager extends AbstractManager implements ManagerInterface
{
public function push(string $job, array $data = [], ?string $queue = null): void
{
$this->connection()->push($job, $data, $queue);
}
public function pop(?string $queue = null): ?Job
{
return $this->connection()->pop($queue);
}
public function getDefaultConnection(): string
{
return $this->config['default'] ?? 'sync';
}
protected function createSyncConnector(): QueueInterface
{
return new SyncQueue();
}
protected function createRedisConnector(): QueueInterface
{
return new RedisQueue($this->getConnectionConfig('redis'));
}
}