Configuration.php 5.2 KB

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