Failed Conditions
Pull Request — master (#3469)
by Sergei
15:32 queued 12:49
created

Configuration::getSQLLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Doctrine\DBAL;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\DBAL\Logging\SQLLogger;
7
use Doctrine\DBAL\Schema\AbstractAsset;
8
use function preg_match;
9
10
/**
11
 * Configuration container for the Doctrine DBAL.
12
 *
13
 * @internal When adding a new configuration option just write a getter/setter
14
 *           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
     * Sets the SQL logger to use. Defaults to NULL which means SQL logging is disabled.
28
     *
29
     * @return void
30
     */
31 9379
    public function setSQLLogger(?SQLLogger $logger = null)
32
    {
33 9379
        $this->_attributes['sqlLogger'] = $logger;
34 9379
    }
35
36
    /**
37
     * Gets the SQL logger that is used.
38
     *
39
     * @return SQLLogger|null
40
     */
41 9525
    public function getSQLLogger()
42
    {
43 9525
        return $this->_attributes['sqlLogger'] ?? null;
44
    }
45
46
    /**
47
     * Gets the cache driver implementation that is used for query result caching.
48
     *
49
     * @return Cache|null
50
     */
51 4007
    public function getResultCacheImpl()
52
    {
53 4007
        return $this->_attributes['resultCacheImpl'] ?? null;
54
    }
55
56
    /**
57
     * Sets the cache driver implementation that is used for query result caching.
58
     *
59
     * @return void
60
     */
61 4009
    public function setResultCacheImpl(Cache $cacheImpl)
62
    {
63 4009
        $this->_attributes['resultCacheImpl'] = $cacheImpl;
64 4009
    }
65
66
    /**
67
     * Sets the filter schema assets expression.
68
     *
69
     * Only include tables/sequences matching the filter expression regexp in
70
     * schema instances generated for the active connection when calling
71
     * {AbstractSchemaManager#createSchema()}.
72
     *
73
     * @deprecated Use Configuration::setSchemaAssetsFilter() instead
74
     *
75
     * @param string $filterExpression
76
     *
77
     * @return void
78
     */
79 1417
    public function setFilterSchemaAssetsExpression($filterExpression)
80
    {
81 1417
        $this->_attributes['filterSchemaAssetsExpression'] = $filterExpression;
82 1417
        if ($filterExpression) {
83 1361
            $this->_attributes['filterSchemaAssetsExpressionCallable'] = $this->buildSchemaAssetsFilterFromExpression($filterExpression);
84
        } else {
85 1417
            $this->_attributes['filterSchemaAssetsExpressionCallable'] = null;
86
        }
87 1417
    }
88
89
    /**
90
     * Returns filter schema assets expression.
91
     *
92
     * @deprecated Use Configuration::getSchemaAssetsFilter() instead
93
     *
94
     * @return string|null
95
     */
96 425
    public function getFilterSchemaAssetsExpression()
97
    {
98 425
        return $this->_attributes['filterSchemaAssetsExpression'] ?? null;
99
    }
100
101
    /**
102
     * @param string $filterExpression
103
     */
104 1361
    private function buildSchemaAssetsFilterFromExpression($filterExpression) : callable
105
    {
106
        return static function ($assetName) use ($filterExpression) {
107 1361
            if ($assetName instanceof AbstractAsset) {
108
                $assetName = $assetName->getName();
109
            }
110 1361
            return preg_match($filterExpression, $assetName);
111 1361
        };
112
    }
113
114
    /**
115
     * Sets the callable to use to filter schema assets.
116
     */
117 425
    public function setSchemaAssetsFilter(?callable $callable = null) : ?callable
118
    {
119 425
        $this->_attributes['filterSchemaAssetsExpression']                = null;
120 425
        return $this->_attributes['filterSchemaAssetsExpressionCallable'] = $callable;
121
    }
122
123
    /**
124
     * Returns the callable to use to filter schema assets.
125
     */
126 4519
    public function getSchemaAssetsFilter() : ?callable
127
    {
128 4519
        return $this->_attributes['filterSchemaAssetsExpressionCallable'] ?? null;
129
    }
130
131
    /**
132
     * Sets the default auto-commit mode for connections.
133
     *
134
     * If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual
135
     * transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by a call to either
136
     * the method commit or the method rollback. By default, new connections are in auto-commit mode.
137
     *
138
     * @see   getAutoCommit
139
     *
140
     * @param bool $autoCommit True to enable auto-commit mode; false to disable it.
141
     */
142 9304
    public function setAutoCommit($autoCommit)
143
    {
144 9304
        $this->_attributes['autoCommit'] = (bool) $autoCommit;
145 9304
    }
146
147
    /**
148
     * Returns the default auto-commit mode for connections.
149
     *
150
     * @see    setAutoCommit
151
     *
152
     * @return bool True if auto-commit mode is enabled by default for connections, false otherwise.
153
     */
154 9588
    public function getAutoCommit()
155
    {
156 9588
        return $this->_attributes['autoCommit'] ?? true;
157
    }
158
}
159