diff --git a/README.md b/README.md index 8482255..abe6212 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Based on a great work of creators:https://github.com/noplay/python-mysql-repli **Note:** Resolve these issues: +- Add regular expression matching support for `DatabasesOnly` or `TablesOnly` of `Config`. - Resolve [krowinski/php-mysql-replication#94](https://github.com/krowinski/php-mysql-replication/issues/94), change static config properties to non-static. - Add retry feature. ```php diff --git a/src/MySQLReplication/Config/Config.php b/src/MySQLReplication/Config/Config.php index e3d58c2..33c51b2 100644 --- a/src/MySQLReplication/Config/Config.php +++ b/src/MySQLReplication/Config/Config.php @@ -177,7 +177,7 @@ public function getTableCacheSize(): int public function checkDataBasesOnly(string $database): bool { - return [] !== $this->getDatabasesOnly() && !in_array($database, $this->getDatabasesOnly(), true); + return [] !== $this->getDatabasesOnly() && !self::matchNames($database, $this->getDatabasesOnly()); } public function getDatabasesOnly(): array @@ -187,7 +187,18 @@ public function getDatabasesOnly(): array public function checkTablesOnly(string $table): bool { - return [] !== $this->getTablesOnly() && !in_array($table, $this->getTablesOnly(), true); + return [] !== $this->getTablesOnly() && !self::matchNames($table, $this->getTablesOnly()); + } + + private static function matchNames(string $subject, array $names): bool + { + foreach ($names as $name) { + if (preg_match("/$name/", $subject)) { + return true; + } + } + + return false; } public function getTablesOnly(): array diff --git a/tests/Unit/Config/ConfigTest.php b/tests/Unit/Config/ConfigTest.php index e06c03a..bb36eee 100644 --- a/tests/Unit/Config/ConfigTest.php +++ b/tests/Unit/Config/ConfigTest.php @@ -93,6 +93,9 @@ public function shouldCheckDataBasesOnly(): void $config = (new ConfigBuilder())->withDatabasesOnly(['foo'])->build(); self::assertTrue($config->checkDataBasesOnly('bar')); + + $config = (new ConfigBuilder())->withDatabasesOnly(['foo_.*'])->build(); + self::assertFalse($config->checkDataBasesOnly('foo_123')); } /** @@ -111,6 +114,9 @@ public function shouldCheckTablesOnly(): void $config = (new ConfigBuilder())->withTablesOnly(['foo'])->build(); self::assertTrue($config->checkTablesOnly('bar')); + + $config = (new ConfigBuilder())->withTablesOnly(['foo_.*'])->build(); + self::assertFalse($config->checkDataBasesOnly('foo_123')); } /**