PHP code example of mcrm / laravel-connection-storage

1. Go to this page and download the library: Download mcrm/laravel-connection-storage library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mcrm / laravel-connection-storage example snippets


use Mcrm\LaravelConnectionStorage\Interfaces\ConnectionDataProviderInterface;

class DatabaseConnectionManager
{
    public function __construct(
        private ConnectionDataProviderInterface $connectionProvider
    ) {}

    public function getConnection(string $boxName): ?array
    {
        $connectionData = $this->connectionProvider->getConnectionData($boxName);
        
        if ($connectionData === null) {
            return null;
        }

        return $connectionData->getConnectionConfig('db');
    }
}

use Mcrm\LaravelConnectionStorage\Interfaces\ConnectionDataProviderInterface;
use App\DTOs\YourCustomConnectionDTO;

$connectionData = YourCustomConnectionDTO::fromArray([
    'db' => [
        'host' => 'localhost',
        'database' => 'your_db',
        'username' => 'user',
        'password' => 'pass'
    ]
]);

$this->connectionProvider->cacheConnectionData('box-name', $connectionData);



namespace App\DTOs;

use Mcrm\LaravelConnectionStorage\DTO\BaseConnectionDataDTO;

class YourCustomConnectionDTO extends BaseConnectionDataDTO
{
    public function __construct(
        array $db = [],
        array $rabbitmq = [],
        array $elasticsearch = [],
        // Add your custom fields
        public readonly array $customField = []
    ) {
        parent::__construct($db, $rabbitmq);
    }

    public static function fromArray(array $data): ?static
    {
        return new static(
            db: $data['db'] ?? [],
            rabbitmq: $data['rabbitmq'] ?? [],
            elasticsearch: $data['elasticsearch'] ?? [],
            customField: $data['custom_field'] ?? []
        );
    }

    public function toArray(): array
    {
        return [
            'db' => $this->db,
            'rabbitmq' => $this->rabbitmq,
            'elasticsearch' => $this->elasticsearch,
            'custom_field' => $this->customField,
        ];
    }

    public function getConnectionConfig(string $type): ?array
    {
        return match ($type) {
            'db' => $this->db,
            'rabbitmq' => $this->rabbitmq,
            'elasticsearch' => $this->elasticsearch,
            'custom' => $this->customField,
            default => null,
        };
    }
}

'dto' => [
    'class' => App\DTOs\YourCustomConnectionDTO::class,
],

interface ConnectionDataProviderInterface
{
    public function getConnectionData(string $identifier): ?BaseConnectionDataDTO;
    public function cacheConnectionData(string $identifier, BaseConnectionDataDTO $connectionData): bool;
}

interface ConnectionCacheServiceInterface
{
    public function getConnectionData(string $url): ?BaseConnectionDataDTO;
    public function getRawConnectionData(string $url): ?array;
    public function cacheConnectionData(string $url, BaseConnectionDataDTO $connectionData): bool;
    public function set(string $url, BaseConnectionDataDTO $connectionData, ?int $ttl = null): bool;
    public function forget(string $url): bool;
    public function remember(string $url, callable $callback, ?int $ttl = null): ?BaseConnectionDataDTO;
}
bash
php artisan vendor:publish --tag=connection-storage-config