PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.2
Independent Analytics – WordPress Analytics Plugin v2.15.2
2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / illuminate / database / Console / TableCommand.php
independent-analytics / vendor / illuminate / database / Console Last commit date
Factories 2 weeks ago Migrations 2 weeks ago Seeds 2 weeks ago DatabaseInspectionCommand.php 2 weeks ago DbCommand.php 2 weeks ago DumpCommand.php 2 weeks ago MonitorCommand.php 2 weeks ago PruneCommand.php 2 weeks ago ShowCommand.php 2 weeks ago TableCommand.php 2 weeks ago WipeCommand.php 2 weeks ago
TableCommand.php
165 lines
1 <?php
2
3 namespace IAWPSCOPED\Illuminate\Database\Console;
4
5 use IAWPSCOPED\Doctrine\DBAL\Schema\Column;
6 use IAWPSCOPED\Doctrine\DBAL\Schema\ForeignKeyConstraint;
7 use IAWPSCOPED\Doctrine\DBAL\Schema\Index;
8 use IAWPSCOPED\Doctrine\DBAL\Schema\Table;
9 use IAWPSCOPED\Illuminate\Database\ConnectionResolverInterface;
10 use IAWPSCOPED\Illuminate\Support\Str;
11 use IAWPSCOPED\Symfony\Component\Console\Attribute\AsCommand;
12 /** @internal */
13 #[AsCommand(name: 'db:table')]
14 class TableCommand extends DatabaseInspectionCommand
15 {
16 /**
17 * The name and signature of the console command.
18 *
19 * @var string
20 */
21 protected $signature = 'db:table
22 {table? : The name of the table}
23 {--database= : The database connection}
24 {--json : Output the table information as JSON}';
25 /**
26 * The console command description.
27 *
28 * @var string
29 */
30 protected $description = 'Display information about the given database table';
31 /**
32 * Execute the console command.
33 *
34 * @return int
35 */
36 public function handle(ConnectionResolverInterface $connections)
37 {
38 if (!$this->ensureDependenciesExist()) {
39 return 1;
40 }
41 $connection = $connections->connection($this->input->getOption('database'));
42 $schema = $connection->getDoctrineSchemaManager();
43 $this->registerTypeMappings($schema->getDatabasePlatform());
44 $table = $this->argument('table') ?: $this->components->choice('Which table would you like to inspect?', \IAWPSCOPED\collect($schema->listTables())->flatMap(fn(Table $table) => [$table->getName()])->toArray());
45 if (!$schema->tablesExist([$table])) {
46 return $this->components->warn("Table [{$table}] doesn't exist.");
47 }
48 $table = $schema->listTableDetails($table);
49 $columns = $this->columns($table);
50 $indexes = $this->indexes($table);
51 $foreignKeys = $this->foreignKeys($table);
52 $data = ['table' => ['name' => $table->getName(), 'columns' => $columns->count(), 'size' => $this->getTableSize($connection, $table->getName())], 'columns' => $columns, 'indexes' => $indexes, 'foreign_keys' => $foreignKeys];
53 $this->display($data);
54 return 0;
55 }
56 /**
57 * Get the information regarding the table's columns.
58 *
59 * @param \Doctrine\DBAL\Schema\Table $table
60 * @return \Illuminate\Support\Collection
61 */
62 protected function columns(Table $table)
63 {
64 return \IAWPSCOPED\collect($table->getColumns())->map(fn(Column $column) => ['column' => $column->getName(), 'attributes' => $this->getAttributesForColumn($column), 'default' => $column->getDefault(), 'type' => $column->getType()->getName()]);
65 }
66 /**
67 * Get the attributes for a table column.
68 *
69 * @param \Doctrine\DBAL\Schema\Column $column
70 * @return \Illuminate\Support\Collection
71 */
72 protected function getAttributesForColumn(Column $column)
73 {
74 return \IAWPSCOPED\collect([$column->getAutoincrement() ? 'autoincrement' : null, 'type' => $column->getType()->getName(), $column->getUnsigned() ? 'unsigned' : null, !$column->getNotNull() ? 'nullable' : null])->filter();
75 }
76 /**
77 * Get the information regarding the table's indexes.
78 *
79 * @param \Doctrine\DBAL\Schema\Table $table
80 * @return \Illuminate\Support\Collection
81 */
82 protected function indexes(Table $table)
83 {
84 return \IAWPSCOPED\collect($table->getIndexes())->map(fn(Index $index) => ['name' => $index->getName(), 'columns' => \IAWPSCOPED\collect($index->getColumns()), 'attributes' => $this->getAttributesForIndex($index)]);
85 }
86 /**
87 * Get the attributes for a table index.
88 *
89 * @param \Doctrine\DBAL\Schema\Index $index
90 * @return \Illuminate\Support\Collection
91 */
92 protected function getAttributesForIndex(Index $index)
93 {
94 return \IAWPSCOPED\collect(['compound' => \count($index->getColumns()) > 1, 'unique' => $index->isUnique(), 'primary' => $index->isPrimary()])->filter()->keys()->map(fn($attribute) => Str::lower($attribute));
95 }
96 /**
97 * Get the information regarding the table's foreign keys.
98 *
99 * @param \Doctrine\DBAL\Schema\Table $table
100 * @return \Illuminate\Support\Collection
101 */
102 protected function foreignKeys(Table $table)
103 {
104 return \IAWPSCOPED\collect($table->getForeignKeys())->map(fn(ForeignKeyConstraint $foreignKey) => ['name' => $foreignKey->getName(), 'local_table' => $table->getName(), 'local_columns' => \IAWPSCOPED\collect($foreignKey->getLocalColumns()), 'foreign_table' => $foreignKey->getForeignTableName(), 'foreign_columns' => \IAWPSCOPED\collect($foreignKey->getForeignColumns()), 'on_update' => Str::lower(rescue(fn() => $foreignKey->getOption('onUpdate'), 'N/A')), 'on_delete' => Str::lower(rescue(fn() => $foreignKey->getOption('onDelete'), 'N/A'))]);
105 }
106 /**
107 * Render the table information.
108 *
109 * @param array $data
110 * @return void
111 */
112 protected function display(array $data)
113 {
114 $this->option('json') ? $this->displayJson($data) : $this->displayForCli($data);
115 }
116 /**
117 * Render the table information as JSON.
118 *
119 * @param array $data
120 * @return void
121 */
122 protected function displayJson(array $data)
123 {
124 $this->output->writeln(\json_encode($data));
125 }
126 /**
127 * Render the table information formatted for the CLI.
128 *
129 * @param array $data
130 * @return void
131 */
132 protected function displayForCli(array $data)
133 {
134 [$table, $columns, $indexes, $foreignKeys] = [$data['table'], $data['columns'], $data['indexes'], $data['foreign_keys']];
135 $this->newLine();
136 $this->components->twoColumnDetail('<fg=green;options=bold>' . $table['name'] . '</>');
137 $this->components->twoColumnDetail('Columns', $table['columns']);
138 if ($size = $table['size']) {
139 $this->components->twoColumnDetail('Size', \number_format($size / 1024 / 1024, 2) . 'MiB');
140 }
141 $this->newLine();
142 if ($columns->isNotEmpty()) {
143 $this->components->twoColumnDetail('<fg=green;options=bold>Column</>', 'Type');
144 $columns->each(function ($column) {
145 $this->components->twoColumnDetail($column['column'] . ' <fg=gray>' . $column['attributes']->implode(', ') . '</>', ($column['default'] ? '<fg=gray>' . $column['default'] . '</> ' : '') . '' . $column['type'] . '');
146 });
147 $this->newLine();
148 }
149 if ($indexes->isNotEmpty()) {
150 $this->components->twoColumnDetail('<fg=green;options=bold>Index</>');
151 $indexes->each(function ($index) {
152 $this->components->twoColumnDetail($index['name'] . ' <fg=gray>' . $index['columns']->implode(', ') . '</>', $index['attributes']->implode(', '));
153 });
154 $this->newLine();
155 }
156 if ($foreignKeys->isNotEmpty()) {
157 $this->components->twoColumnDetail('<fg=green;options=bold>Foreign Key</>', 'On Update / On Delete');
158 $foreignKeys->each(function ($foreignKey) {
159 $this->components->twoColumnDetail($foreignKey['name'] . ' <fg=gray;options=bold>' . $foreignKey['local_columns']->implode(', ') . ' references ' . $foreignKey['foreign_columns']->implode(', ') . ' on ' . $foreignKey['foreign_table'] . '</>', $foreignKey['on_update'] . ' / ' . $foreignKey['on_delete']);
160 });
161 $this->newLine();
162 }
163 }
164 }
165