| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace App\Services\Asteria;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- /**
- * Schema helpers that work against Magento 1's MySQL 5.6.
- *
- * Laravel's Schema::hasColumn() reads information_schema.columns.generation_expression,
- * which does not exist until MySQL 5.7.
- */
- class Magento1Schema
- {
- /** @var array<string, array<int, string>> */
- private array $columns = [];
- public function __construct(private string $connection = 'asteria') {}
- public function hasTable(string $table): bool
- {
- return Schema::connection($this->connection)->hasTable($table);
- }
- /**
- * @param array<int, string> $candidates
- * @return array<int, string>
- */
- public function existingColumns(string $table, array $candidates): array
- {
- if (! $this->hasTable($table)) {
- return [];
- }
- $listing = array_fill_keys(array_map('strtolower', $this->columnListing($table)), true);
- return array_values(array_filter(
- $candidates,
- fn (string $column) => isset($listing[strtolower($column)])
- ));
- }
- /**
- * @return array<int, string>
- */
- public function columnListing(string $table): array
- {
- if (isset($this->columns[$table])) {
- return $this->columns[$table];
- }
- $connection = DB::connection($this->connection);
- if ($connection->getDriverName() === 'mysql') {
- $wrapped = $connection->getQueryGrammar()->wrapTable($table);
- $rows = $connection->select('show columns from '.$wrapped);
- $names = [];
- foreach ($rows as $row) {
- $row = (array) $row;
- $name = $row['Field'] ?? $row['field'] ?? null;
- if (is_string($name) && $name !== '') {
- $names[] = $name;
- }
- }
- return $this->columns[$table] = $names;
- }
- return $this->columns[$table] = Schema::connection($this->connection)->getColumnListing($table);
- }
- }
|