Configuration.php 4.6 KB

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