Configuration.php 4.6 KB
Newer Older
1
<?php
2

3 4
namespace Doctrine\DBAL;

5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\DBAL\Logging\SQLLogger;
7 8
use Doctrine\DBAL\Schema\AbstractAsset;
use function preg_match;
9 10

/**
11
 * Configuration container for the Doctrine DBAL.
12
 *
13 14
 * @internal When adding a new configuration option just write a getter/setter
 *           pair and add the option to the _attributes array with a proper default value.
15
 */
16
class Configuration
17 18 19
{
    /**
     * The attributes that are contained in the configuration.
20
     * Values are default values.
21
     *
22
     * @var mixed[]
23
     */
24
    protected $_attributes = [];
25

26 27 28
    /**
     * Sets the SQL logger to use. Defaults to NULL which means SQL logging is disabled.
     *
Benjamin Morel's avatar
Benjamin Morel committed
29
     * @return void
30
     */
31
    public function setSQLLogger(?SQLLogger $logger = null)
32 33 34
    {
        $this->_attributes['sqlLogger'] = $logger;
    }
35

36 37
    /**
     * Gets the SQL logger that is used.
38
     *
39
     * @return SQLLogger|null
40
     */
41
    public function getSQLLogger()
42
    {
43
        return $this->_attributes['sqlLogger'] ?? null;
44
    }
45 46 47 48

    /**
     * Gets the cache driver implementation that is used for query result caching.
     *
49
     * @return Cache|null
50 51 52
     */
    public function getResultCacheImpl()
    {
53
        return $this->_attributes['resultCacheImpl'] ?? null;
54 55 56 57 58
    }

    /**
     * Sets the cache driver implementation that is used for query result caching.
     *
Benjamin Morel's avatar
Benjamin Morel committed
59
     * @return void
60 61 62 63 64
     */
    public function setResultCacheImpl(Cache $cacheImpl)
    {
        $this->_attributes['resultCacheImpl'] = $cacheImpl;
    }
65 66

    /**
Benjamin Morel's avatar
Benjamin Morel committed
67
     * Sets the filter schema assets expression.
68 69 70 71 72
     *
     * Only include tables/sequences matching the filter expression regexp in
     * schema instances generated for the active connection when calling
     * {AbstractSchemaManager#createSchema()}.
     *
73 74
     * @deprecated Use Configuration::setSchemaAssetsFilter() instead
     *
75
     * @param string $filterExpression
Benjamin Morel's avatar
Benjamin Morel committed
76 77
     *
     * @return void
78 79 80 81
     */
    public function setFilterSchemaAssetsExpression($filterExpression)
    {
        $this->_attributes['filterSchemaAssetsExpression'] = $filterExpression;
82 83 84 85 86
        if ($filterExpression) {
            $this->_attributes['filterSchemaAssetsExpressionCallable'] = $this->buildSchemaAssetsFilterFromExpression($filterExpression);
        } else {
            $this->_attributes['filterSchemaAssetsExpressionCallable'] = null;
        }
87 88 89
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
90
     * Returns filter schema assets expression.
91
     *
92 93
     * @deprecated Use Configuration::getSchemaAssetsFilter() instead
     *
94 95 96 97
     * @return string|null
     */
    public function getFilterSchemaAssetsExpression()
    {
98
        return $this->_attributes['filterSchemaAssetsExpression'] ?? null;
99
    }
100

101 102 103 104 105 106 107 108 109
    /**
     * @param string $filterExpression
     */
    private function buildSchemaAssetsFilterFromExpression($filterExpression) : callable
    {
        return static function ($assetName) use ($filterExpression) {
            if ($assetName instanceof AbstractAsset) {
                $assetName = $assetName->getName();
            }
110

111 112 113 114 115 116 117 118 119
            return preg_match($filterExpression, $assetName);
        };
    }

    /**
     * Sets the callable to use to filter schema assets.
     */
    public function setSchemaAssetsFilter(?callable $callable = null) : ?callable
    {
120 121
        $this->_attributes['filterSchemaAssetsExpression'] = null;

122 123 124 125 126 127 128 129 130 131 132
        return $this->_attributes['filterSchemaAssetsExpressionCallable'] = $callable;
    }

    /**
     * Returns the callable to use to filter schema assets.
     */
    public function getSchemaAssetsFilter() : ?callable
    {
        return $this->_attributes['filterSchemaAssetsExpressionCallable'] ?? null;
    }

133 134 135 136 137 138 139 140
    /**
     * Sets the default auto-commit mode for connections.
     *
     * If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual
     * transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by a call to either
     * the method commit or the method rollback. By default, new connections are in auto-commit mode.
     *
     * @see   getAutoCommit
141 142
     *
     * @param bool $autoCommit True to enable auto-commit mode; false to disable it.
143 144 145
     */
    public function setAutoCommit($autoCommit)
    {
146
        $this->_attributes['autoCommit'] = (bool) $autoCommit;
147 148 149 150 151 152
    }

    /**
     * Returns the default auto-commit mode for connections.
     *
     * @see    setAutoCommit
153 154
     *
     * @return bool True if auto-commit mode is enabled by default for connections, false otherwise.
155 156 157
     */
    public function getAutoCommit()
    {
158
        return $this->_attributes['autoCommit'] ?? true;
159
    }
160
}