setName("pdo:inspect"); $this->setDescription("Inspect the database, a table, or a row"); $this->addOption("res", "r", InputOption::VALUE_REQUIRED, "Resource to query", "db"); $this->addArgument("table", InputArgument::OPTIONAL, "Table name to inspect"); } protected function execute(InputInterface $input, OutputInterface $output) { $sourceName = $input->getOption("res"); $source = get_resource($sourceName); if (!$source) { $output->writeln("Invalid resource: {$source}"); return Command::INVALID; } $reflector = $source->getReflector(); $tableName = $input->getArgument("table"); if ($tableName) { try { $table = $reflector->createTableReflection($tableName); if ($table) { $this->dumpTable($tableName, $table, $output); } } catch (\Exception $e) { $output->writeln("{$e->getMessage()}"); } } else { try { $database = $reflector->createDatabaseReflection(); foreach ($database->getAllTables() as $tableName=>$table) { $this->dumpTable($tableName, $table, $output); } } catch (\Exception $e) { $output->writeln("{$e->getMessage()}"); } } return Command::SUCCESS; } private function dumpTable(string $name, TableReflectionInterface $table, OutputInterface $output) { $output->writeln("{$name}"); $t = new Table($output); $t->setStyle('compact'); $t->setHeaders([ "Name", "Type", "PK", "NULL", "Default" ]); $t->setColumnWidth(0, 30); $t->setColumnWidth(1, 30); foreach ($table->getAllColumns() as $column) { $t->addRow([ $column->getName(), $column->getType(), $column->isPrimaryKey()?"Y":"-", $column->isNullable()?"Y":"-", $column->getDefaultValue(), ]); } $t->render(); $output->writeln(""); } }