diff --git a/src/Command/AllCommand.php b/src/Command/AllCommand.php index 6768c05e..0c046502 100644 --- a/src/Command/AllCommand.php +++ b/src/Command/AllCommand.php @@ -84,17 +84,17 @@ public function execute(Arguments $args, ConsoleIo $io): ?int /** @var \Cake\Database\Connection $connection */ $connection = ConnectionManager::get($this->connection); $scanner = new TableScanner($connection); + $tables = $scanner->removeShadowTranslationTables($scanner->listUnskipped()); + if (!$name && !$args->getOption('everything')) { $io->out('Choose a table to generate from the following:'); - foreach ($scanner->listUnskipped() as $table) { + foreach ($tables as $table) { $io->out('- ' . $this->_camelize($table)); } return static::CODE_SUCCESS; } - if ($args->getOption('everything')) { - $tables = $scanner->listUnskipped(); - } else { + if (!$args->getOption('everything')) { $tables = [$name]; } diff --git a/src/Command/FixtureAllCommand.php b/src/Command/FixtureAllCommand.php index 32ca856a..40ccdc95 100644 --- a/src/Command/FixtureAllCommand.php +++ b/src/Command/FixtureAllCommand.php @@ -86,7 +86,9 @@ public function execute(Arguments $args, ConsoleIo $io): ?int $connection = ConnectionManager::get($args->getOption('connection') ?? 'default'); $scanner = new TableScanner($connection); $fixture = new FixtureCommand(); - foreach ($scanner->listUnskipped() as $table) { + + $tables = $scanner->removeShadowTranslationTables($scanner->listUnskipped()); + foreach ($tables as $table) { $fixtureArgs = new Arguments([$table], $args->getOptions(), ['name']); $fixture->execute($fixtureArgs, $io); } diff --git a/src/Command/ModelAllCommand.php b/src/Command/ModelAllCommand.php index e43b4dd7..a65f696c 100644 --- a/src/Command/ModelAllCommand.php +++ b/src/Command/ModelAllCommand.php @@ -83,7 +83,8 @@ public function execute(Arguments $args, ConsoleIo $io): ?int /** @var \Cake\Database\Connection $connection */ $connection = ConnectionManager::get($this->connection); $scanner = new TableScanner($connection); - foreach ($scanner->listUnskipped() as $table) { + $tables = $scanner->removeShadowTranslationTables($scanner->listUnskipped()); + foreach ($tables as $table) { $this->getTableLocator()->clear(); $modelArgs = new Arguments([$table], $args->getOptions(), ['name']); $this->modelCommand->execute($modelArgs, $io); diff --git a/src/Command/ModelCommand.php b/src/Command/ModelCommand.php index 60c07959..89fb4004 100644 --- a/src/Command/ModelCommand.php +++ b/src/Command/ModelCommand.php @@ -1251,7 +1251,7 @@ public function bakeTable(Table $model, array $data, Arguments $args, ConsoleIo } /** - * Outputs the a list of possible models or controllers from database + * Outputs the list of possible models or controllers from database * * @return array */ @@ -1270,7 +1270,7 @@ public function listAll(): array } /** - * Outputs the a list of unskipped models or controllers from database + * Outputs the list of unskipped models or controllers from database * * @return array */ diff --git a/src/Command/TemplateAllCommand.php b/src/Command/TemplateAllCommand.php index 4be17e5b..9270a81f 100644 --- a/src/Command/TemplateAllCommand.php +++ b/src/Command/TemplateAllCommand.php @@ -65,7 +65,8 @@ public function execute(Arguments $args, ConsoleIo $io): int $connection = ConnectionManager::get($this->connection); $scanner = new TableScanner($connection); - foreach ($scanner->listUnskipped() as $table) { + $tables = $scanner->removeShadowTranslationTables($scanner->listUnskipped()); + foreach ($tables as $table) { $parser = $this->templateCommand->getOptionParser(); $templateArgs = new Arguments( [$table], diff --git a/src/Utility/TableScanner.php b/src/Utility/TableScanner.php index 70f56d15..b10be2c0 100644 --- a/src/Utility/TableScanner.php +++ b/src/Utility/TableScanner.php @@ -58,13 +58,13 @@ public function __construct(Connection $connection, ?array $ignore = null) /** * Get all tables in the connection without applying ignores. * - * @return array + * @return array */ public function listAll(): array { $schema = $this->connection->getSchemaCollection(); $tables = $schema->listTables(); - if (empty($tables)) { + if (!$tables) { throw new RuntimeException('Your database does not have any tables.'); } sort($tables); @@ -75,7 +75,7 @@ public function listAll(): array /** * Get all tables in the connection that aren't ignored. * - * @return array + * @return array */ public function listUnskipped(): array { @@ -90,6 +90,29 @@ public function listUnskipped(): array return $tables; } + /** + * Call from any All command that needs the shadow translation tables to be skipped. + * + * @param array $tables + * @return array + */ + public function removeShadowTranslationTables(array $tables): array + { + foreach ($tables as $key => $table) { + if (!preg_match('/^(.+)_translations$/', $table, $matches)) { + continue; + } + + if (empty($tables[$matches[1]])) { + continue; + } + + unset($tables[$key]); + } + + return $tables; + } + /** * @param string $table Table name. * @return bool @@ -97,7 +120,7 @@ public function listUnskipped(): array protected function shouldSkip(string $table): bool { foreach ($this->ignore as $ignore) { - if (strpos($ignore, '/') === 0) { + if (str_starts_with($ignore, '/')) { if ((bool)preg_match($ignore, $table)) { return true; } diff --git a/tests/TestCase/Utility/TableScannerTest.php b/tests/TestCase/Utility/TableScannerTest.php index ea17744a..4591032e 100644 --- a/tests/TestCase/Utility/TableScannerTest.php +++ b/tests/TestCase/Utility/TableScannerTest.php @@ -125,4 +125,26 @@ public function testListUnskippedRegex() } } } + + /** + * @return void + */ + public function testRemoveShadowTranslationTables(): void + { + $this->tableScanner = new TableScanner($this->connection); + + $tables = [ + 'items' => 'items', + 'users' => 'users', + 'users_translations' => 'users_translations', + 'item_translations' => 'item_translations', + ]; + $result = $this->tableScanner->removeShadowTranslationTables($tables); + $expected = [ + 'items' => 'items', + 'users' => 'users', + 'item_translations' => 'item_translations', + ]; + $this->assertEquals($expected, $result); + } }