|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Neuron\Mvc\Cli\Commands\Schema; |
| 4 | + |
| 5 | +use Neuron\Cli\Commands\Command; |
| 6 | +use Neuron\Mvc\Database\MigrationManager; |
| 7 | +use Neuron\Mvc\Database\SchemaExporter; |
| 8 | +use Neuron\Data\Settings\Source\Yaml; |
| 9 | + |
| 10 | +/** |
| 11 | + * CLI command for exporting database schema to YAML |
| 12 | + * Similar to Rails' rake db:schema:dump |
| 13 | + */ |
| 14 | +class DumpCommand extends Command |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @inheritDoc |
| 18 | + */ |
| 19 | + public function getName(): string |
| 20 | + { |
| 21 | + return 'db:schema:dump'; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @inheritDoc |
| 26 | + */ |
| 27 | + public function getDescription(): string |
| 28 | + { |
| 29 | + return 'Export database schema to YAML file for reference'; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @inheritDoc |
| 34 | + */ |
| 35 | + public function configure(): void |
| 36 | + { |
| 37 | + $this->addOption( 'output', 'o', true, 'Output file path (default: db/schema.yaml)' ); |
| 38 | + $this->addOption( 'config', null, true, 'Path to configuration directory' ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @inheritDoc |
| 43 | + */ |
| 44 | + public function execute(): int |
| 45 | + { |
| 46 | + // Get configuration |
| 47 | + $configPath = $this->input->getOption( 'config', $this->findConfigPath() ); |
| 48 | + |
| 49 | + if( !$configPath || !is_dir( $configPath ) ) |
| 50 | + { |
| 51 | + $this->output->error( 'Configuration directory not found: ' . ($configPath ?: 'none specified') ); |
| 52 | + $this->output->info( 'Use --config to specify the configuration directory' ); |
| 53 | + return 1; |
| 54 | + } |
| 55 | + |
| 56 | + // Load settings |
| 57 | + $settings = $this->loadSettings( $configPath ); |
| 58 | + $basePath = dirname( $configPath ); |
| 59 | + |
| 60 | + // Create manager |
| 61 | + $manager = new MigrationManager( $basePath, $settings ); |
| 62 | + |
| 63 | + // Determine output file path |
| 64 | + $outputPath = $this->input->getOption( 'output' ); |
| 65 | + if( !$outputPath ) |
| 66 | + { |
| 67 | + // Check if there's a configured schema file path |
| 68 | + if( $settings ) |
| 69 | + { |
| 70 | + $outputPath = $settings->get( 'migrations', 'schema_file' ); |
| 71 | + } |
| 72 | + |
| 73 | + // Default to db/schema.yaml |
| 74 | + if( !$outputPath ) |
| 75 | + { |
| 76 | + $outputPath = 'db/schema.yaml'; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Resolve relative paths |
| 81 | + if( !str_starts_with( $outputPath, '/' ) ) |
| 82 | + { |
| 83 | + $outputPath = $basePath . '/' . $outputPath; |
| 84 | + } |
| 85 | + |
| 86 | + try |
| 87 | + { |
| 88 | + $this->output->info( 'Exporting database schema...' ); |
| 89 | + |
| 90 | + // Create schema exporter |
| 91 | + $exporter = new SchemaExporter( |
| 92 | + $manager->getPhinxConfig(), |
| 93 | + $manager->getEnvironment(), |
| 94 | + $manager->getMigrationTable() |
| 95 | + ); |
| 96 | + |
| 97 | + // Export to file |
| 98 | + if( $exporter->exportToFile( $outputPath ) ) |
| 99 | + { |
| 100 | + $this->output->newLine(); |
| 101 | + $this->output->success( 'Schema exported successfully to: ' . $outputPath ); |
| 102 | + return 0; |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + $this->output->newLine(); |
| 107 | + $this->output->error( 'Failed to write schema file: ' . $outputPath ); |
| 108 | + return 1; |
| 109 | + } |
| 110 | + } |
| 111 | + catch( \Exception $e ) |
| 112 | + { |
| 113 | + $this->output->error( 'Error exporting schema: ' . $e->getMessage() ); |
| 114 | + |
| 115 | + if( $this->input->hasOption( 'verbose' ) || $this->input->hasOption( 'v' ) ) |
| 116 | + { |
| 117 | + $this->output->write( $e->getTraceAsString() ); |
| 118 | + } |
| 119 | + |
| 120 | + return 1; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Load settings from config directory |
| 126 | + * |
| 127 | + * @param string $configPath |
| 128 | + * @return Yaml|null |
| 129 | + */ |
| 130 | + private function loadSettings( string $configPath ): ?Yaml |
| 131 | + { |
| 132 | + $configFile = $configPath . '/neuron.yaml'; |
| 133 | + |
| 134 | + if( !file_exists( $configFile ) ) |
| 135 | + { |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + try |
| 140 | + { |
| 141 | + return new Yaml( $configFile ); |
| 142 | + } |
| 143 | + catch( \Exception $e ) |
| 144 | + { |
| 145 | + $this->output->warning( 'Could not load configuration: ' . $e->getMessage() ); |
| 146 | + return null; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Try to find the configuration directory |
| 152 | + * |
| 153 | + * @return string|null |
| 154 | + */ |
| 155 | + private function findConfigPath(): ?string |
| 156 | + { |
| 157 | + $locations = [ |
| 158 | + getcwd() . '/config', |
| 159 | + dirname( getcwd() ) . '/config', |
| 160 | + dirname( getcwd(), 2 ) . '/config', |
| 161 | + ]; |
| 162 | + |
| 163 | + foreach( $locations as $location ) |
| 164 | + { |
| 165 | + if( is_dir( $location ) ) |
| 166 | + { |
| 167 | + return $location; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return null; |
| 172 | + } |
| 173 | +} |
0 commit comments