Вход Регистрация
Файл: gapps/vendor/graham-campbell/manager/src/AbstractManager.php
Строк: 344
<?php

/*
 * This file is part of Laravel Manager.
 *
 * (c) Graham Campbell <graham@alt-three.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace GrahamCampbellManager;

use 
IlluminateContractsConfigRepository;
use 
InvalidArgumentException;

/**
 * This is the abstract manager class.
 *
 * @author Graham Campbell <graham@alt-three.com>
 */
abstract class AbstractManager implements ManagerInterface
{
    
/**
     * The config instance.
     *
     * @var IlluminateContractsConfigRepository
     */
    
protected $config;

    
/**
     * The active connection instances.
     *
     * @var array
     */
    
protected $connections = [];

    
/**
     * The custom connection resolvers.
     *
     * @var array
     */
    
protected $extensions = [];

    
/**
     * Create a new manager instance.
     *
     * @param IlluminateContractsConfigRepository $config
     *
     * @return void
     */
    
public function __construct(Repository $config)
    {
        
$this->config $config;
    }

    
/**
     * Get a connection instance.
     *
     * @param string $name
     *
     * @return object
     */
    
public function connection($name null)
    {
        
$name $name ?: $this->getDefaultConnection();

        if (!isset(
$this->connections[$name])) {
            
$this->connections[$name] = $this->makeConnection($name);
        }

        return 
$this->connections[$name];
    }

    
/**
     * Reconnect to the given connection.
     *
     * @param string $name
     *
     * @return object
     */
    
public function reconnect($name null)
    {
        
$name $name ?: $this->getDefaultConnection();

        
$this->disconnect($name);

        return 
$this->connection($name);
    }

    
/**
     * Disconnect from the given connection.
     *
     * @param string $name
     *
     * @return void
     */
    
public function disconnect($name null)
    {
        
$name $name ?: $this->getDefaultConnection();

        unset(
$this->connections[$name]);
    }

    
/**
     * Create the connection instance.
     *
     * @param array $config
     *
     * @return mixed
     */
    
abstract protected function createConnection(array $config);

    
/**
     * Make the connection instance.
     *
     * @param string $name
     *
     * @return mixed
     */
    
protected function makeConnection($name)
    {
        
$config $this->getConnectionConfig($name);

        if (isset(
$this->extensions[$name])) {
            return 
call_user_func($this->extensions[$name], $config);
        }

        if (
$driver array_get($config'driver')) {
            if (isset(
$this->extensions[$driver])) {
                return 
call_user_func($this->extensions[$driver], $config);
            }
        }

        return 
$this->createConnection($config);
    }

    
/**
     * Get the configuration name.
     *
     * @return string
     */
    
abstract protected function getConfigName();

    
/**
     * Get the configuration for a connection.
     *
     * @param string $name
     *
     * @throws InvalidArgumentException
     *
     * @return array
     */
    
public function getConnectionConfig($name)
    {
        
$name $name ?: $this->getDefaultConnection();

        
$connections $this->config->get($this->getConfigName().'.connections');

        if (!
is_array($config array_get($connections$name)) && !$config) {
            throw new 
InvalidArgumentException("Connection [$name] not configured.");
        }

        
$config['name'] = $name;

        return 
$config;
    }

    
/**
     * Get the default connection name.
     *
     * @return string
     */
    
public function getDefaultConnection()
    {
        return 
$this->config->get($this->getConfigName().'.default');
    }

    
/**
     * Set the default connection name.
     *
     * @param string $name
     *
     * @return void
     */
    
public function setDefaultConnection($name)
    {
        
$this->config->set($this->getConfigName().'.default'$name);
    }

    
/**
     * Register an extension connection resolver.
     *
     * @param string   $name
     * @param callable $resolver
     *
     * @return void
     */
    
public function extend($name$resolver)
    {
        
$this->extensions[$name] = $resolver;
    }

    
/**
     * Return all of the created connections.
     *
     * @return object[]
     */
    
public function getConnections()
    {
        return 
$this->connections;
    }

    
/**
     * Get the config instance.
     *
     * @return IlluminateContractsConfigRepository
     */
    
public function getConfig()
    {
        return 
$this->config;
    }

    
/**
     * Dynamically pass methods to the default connection.
     *
     * @param string $method
     * @param array  $parameters
     *
     * @return mixed
     */
    
public function __call($method$parameters)
    {
        return 
call_user_func_array([$this->connection(), $method], $parameters);
    }
}
Онлайн: 0
Реклама