Completed
Push — master ( ed25dc...18a804 )
by Sergei
35:26 queued 32:37
created

Configuration::getAutoCommit()   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 5501
    public function setSQLLogger(?SQLLogger $logger = null)
32
    {
33 5501
        $this->_attributes['sqlLogger'] = $logger;
34 5501
    }
35
36
    /**
37
     * Gets the SQL logger that is used.
38
     *
39
     * @return SQLLogger|null
40
     */
41 5546
    public function getSQLLogger()
42
    {
43 5546
        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 190
    public function getResultCacheImpl()
52
    {
53 190
        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 190
    public function setResultCacheImpl(Cache $cacheImpl)
62
    {
63 190
        $this->_attributes['resultCacheImpl'] = $cacheImpl;
64 190
    }
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
     * @param string $filterExpression
74
     *
75
     * @return void
76
     */
77 476
    public function setFilterSchemaAssetsExpression($filterExpression)
78
    {
79 476
        $this->_attributes['filterSchemaAssetsExpression'] = $filterExpression;
80 476
        if ($filterExpression) {
81 6
            $this->_attributes['filterSchemaAssetsExpressionCallable'] = $this->buildSchemaAssetsFilterFromExpression($filterExpression);
82
        } else {
83 476
            $this->_attributes['filterSchemaAssetsExpressionCallable'] = null;
84
        }
85 476
    }
86
87
    /**
88
     * Returns filter schema assets expression.
89
     *
90
     * @return string|null
91
     */
92
    public function getFilterSchemaAssetsExpression()
93
    {
94
        return $this->_attributes['filterSchemaAssetsExpression'] ?? null;
95
    }
96
97
    /**
98
     * @param string $filterExpression
99
     */
100 6
    private function buildSchemaAssetsFilterFromExpression($filterExpression) : callable
101
    {
102
        return static function ($assetName) use ($filterExpression) {
103 6
            if ($assetName instanceof AbstractAsset) {
104
                $assetName = $assetName->getName();
105
            }
106 6
            return preg_match($filterExpression, $assetName);
107 6
        };
108
    }
109
110
    /**
111
     * Sets the callable to use to filter schema assets.
112
     */
113
    public function setSchemaAssetsFilter(?callable $callable = null) : ?callable
114
    {
115
        $this->_attributes['filterSchemaAssetsExpression']                = null;
116
        return $this->_attributes['filterSchemaAssetsExpressionCallable'] = $callable;
117
    }
118
119
    /**
120
     * Returns the callable to use to filter schema assets.
121
     */
122 1485
    public function getSchemaAssetsFilter() : ?callable
123
    {
124 1485
        return $this->_attributes['filterSchemaAssetsExpressionCallable'] ?? null;
125
    }
126
127
    /**
128
     * Sets the default auto-commit mode for connections.
129
     *
130
     * If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual
131
     * transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by a call to either
132
     * the method commit or the method rollback. By default, new connections are in auto-commit mode.
133
     *
134
     * @see   getAutoCommit
135
     *
136
     * @param bool $autoCommit True to enable auto-commit mode; false to disable it.
137
     */
138 19
    public function setAutoCommit($autoCommit)
139
    {
140 19
        $this->_attributes['autoCommit'] = (bool) $autoCommit;
141 19
    }
142
143
    /**
144
     * Returns the default auto-commit mode for connections.
145
     *
146
     * @see    setAutoCommit
147
     *
148
     * @return bool True if auto-commit mode is enabled by default for connections, false otherwise.
149
     */
150 2672
    public function getAutoCommit()
151
    {
152 2672
        return $this->_attributes['autoCommit'] ?? true;
153
    }
154
}
155