TableNameResolver.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Setup\Declaration\Schema;
  8. /**
  9. * Resolver of table names.
  10. */
  11. class TableNameResolver
  12. {
  13. /**
  14. * RegEx pattern is used to search cloned temporary tables used as a replica.
  15. *
  16. * @var string
  17. */
  18. private $filterPattern = '';
  19. /**
  20. * Provides the name of the origin table for cloned tables.
  21. *
  22. * Replica tables should be identical to the original -
  23. * indexes and constraints must use the original table name to calculate their own names.
  24. *
  25. * @param string $tableName
  26. * @return string
  27. */
  28. public function getNameOfOriginTable(string $tableName): string
  29. {
  30. $tableIsReplica = preg_match($this->getFilterPattern(), $tableName, $matches);
  31. return $tableIsReplica ? $matches['table_name'] : $tableName;
  32. }
  33. /**
  34. * Provides a RegEx pattern used to search cloned temporary tables used as a replica.
  35. *
  36. * @return string
  37. */
  38. private function getFilterPattern(): string
  39. {
  40. if (!$this->filterPattern) {
  41. $this->filterPattern = '#(?<table_name>\S+)_replica$#i';
  42. }
  43. return $this->filterPattern;
  44. }
  45. }