<commit_before><?php

/**
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Arlekin\DatabaseAbstractionLayer\Manager;

use Arlekin\DatabaseAbstractionLayer\DriverInterface;
use Exception;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DatabaseConnectionManager implements DatabaseConnectionManagerInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var array
     */
    protected $connectionsByName;

    /**
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        $this->connectionsByName = [];
    }

    /**
     * {@inheritdoc}
     */
    protected function instanciateDatabaseConnection(
        array $parameters
    ) {
        $driverIdsByDriverName = $this->container->getParameter(
            'dbal.driver_ids_by_driver_name'
        );

        $driverParameter = $parameters['driver'];

        if (!isset($driverIdsByDriverName[$driverParameter])) {
            throw new Exception(
                sprintf(
                    'Found no driver with name "%s".',
                    $driverParameter
                )
            );
        }

        $driverId = $driverIdsByDriverName[$driverParameter];

        $driver = $this->container->get($driverId);

        /* @var $driver DriverInterface */

        return $driver->instanciateDatabaseConnection($parameters);
    }

    /**
     * {@inheritdoc}
     */
    protected function instanciateNamedDatabaseConnection($name)
    {
        $configByDatabaseConnectionName = $this->container->getParameter(
            'dbal.parameters_by_database_connection_name'
        );

        if (!isset($configByDatabaseConnectionName[$name])) {
            throw new Exception(
                sprintf(
                    'Found no database connection with name "%s".',
                    $name
                )
            );
        }

        return $this->instanciateDatabaseConnection(
            $configByDatabaseConnectionName[$name]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getConnectionWithName($name)
    {
        if (!isset($this->connectionsByName[$name])) {
            $connection = $this->instanciateNamedDatabaseConnection($name);

            $connection->connect();

            $this->connectionsByName[$name] = $connection;
        }

        return $this->connectionsByName[$name];
    }
}<commit_msg>Removes connection autoconnect.<commit_after><?php

/**
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Arlekin\DatabaseAbstractionLayer\Manager;

use Arlekin\DatabaseAbstractionLayer\DriverInterface;
use Exception;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DatabaseConnectionManager implements DatabaseConnectionManagerInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var array
     */
    protected $connectionsByName;

    /**
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        $this->connectionsByName = [];
    }

    /**
     * {@inheritdoc}
     */
    protected function instanciateDatabaseConnection(
        array $parameters
    ) {
        $driverIdsByDriverName = $this->container->getParameter(
            'dbal.driver_ids_by_driver_name'
        );

        $driverParameter = $parameters['driver'];

        if (!isset($driverIdsByDriverName[$driverParameter])) {
            throw new Exception(
                sprintf(
                    'Found no driver with name "%s".',
                    $driverParameter
                )
            );
        }

        $driverId = $driverIdsByDriverName[$driverParameter];

        $driver = $this->container->get($driverId);

        /* @var $driver DriverInterface */

        return $driver->instanciateDatabaseConnection($parameters);
    }

    /**
     * {@inheritdoc}
     */
    protected function instanciateNamedDatabaseConnection($name)
    {
        $configByDatabaseConnectionName = $this->container->getParameter(
            'dbal.parameters_by_database_connection_name'
        );

        if (!isset($configByDatabaseConnectionName[$name])) {
            throw new Exception(
                sprintf(
                    'Found no database connection with name "%s".',
                    $name
                )
            );
        }

        return $this->instanciateDatabaseConnection(
            $configByDatabaseConnectionName[$name]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getConnectionWithName($name)
    {
        if (!isset($this->connectionsByName[$name])) {
            $connection = $this->instanciateNamedDatabaseConnection($name);

            $this->connectionsByName[$name] = $connection;
        }

        return $this->connectionsByName[$name];
    }
}
{'fraction_non_alphanumeric': 0.1097537698033976, 'fraction_numerical': 0.0, 'mean_word_length': 2.2048929663608563, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6189907', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1284, 'n_words': 247}