|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AdrienPoupa\MigrateRoutines\Console; |
| 4 | + |
| 5 | +use Illuminate\Database\Console\Migrations\BaseCommand; |
| 6 | +use Illuminate\Filesystem\Filesystem; |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use stdClass; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class MigrateRoutines |
| 12 | + * @package AdrienPoupa\MigrateRoutines\Console |
| 13 | + */ |
| 14 | +abstract class MigrateRoutines extends BaseCommand |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $database; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $migrationType; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var Filesystem |
| 28 | + */ |
| 29 | + protected $filesystem; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var Str |
| 33 | + */ |
| 34 | + protected $str; |
| 35 | + |
| 36 | + /** |
| 37 | + * Create a new command instance. |
| 38 | + * |
| 39 | + */ |
| 40 | + public function __construct() |
| 41 | + { |
| 42 | + parent::__construct(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Execute the console command. |
| 47 | + * |
| 48 | + * @param Filesystem $filesystem |
| 49 | + * @param Str $str |
| 50 | + * @return void |
| 51 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 52 | + */ |
| 53 | + public function handle(Filesystem $filesystem, Str $str) |
| 54 | + { |
| 55 | + $this->filesystem = $filesystem; |
| 56 | + $this->str = $str; |
| 57 | + |
| 58 | + $this->database = $this->option('database'); |
| 59 | + |
| 60 | + if ($this->database === 'default') { |
| 61 | + $this->database = config('database.connections.mysql.database'); |
| 62 | + } |
| 63 | + |
| 64 | + $this->convert(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Convert the existing routines |
| 69 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 70 | + */ |
| 71 | + private function convert() |
| 72 | + { |
| 73 | + $routines = $this->getData(); |
| 74 | + |
| 75 | + if (!$routines) { |
| 76 | + $this->info('Nothing to migrate.'); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + foreach ($routines as $routine) { |
| 81 | + $migrationName = $this->str->snake($routine->name); |
| 82 | + $up = $this->up($routine); |
| 83 | + $down = $this->down($routine); |
| 84 | + $this->write($migrationName, $up, $down); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return array |
| 90 | + */ |
| 91 | + abstract public function getData(); |
| 92 | + |
| 93 | + /** |
| 94 | + * Generate the up function |
| 95 | + * We use unprepared to avoid error 2014 |
| 96 | + * @param stdClass $routine |
| 97 | + * @return string |
| 98 | + */ |
| 99 | + abstract public function up(stdClass $routine); |
| 100 | + |
| 101 | + /** |
| 102 | + * Generate the down function |
| 103 | + * @param stdClass $routine |
| 104 | + * @return string |
| 105 | + */ |
| 106 | + abstract public function down(stdClass $routine); |
| 107 | + |
| 108 | + /** |
| 109 | + * Write the migration |
| 110 | + * @param string $migrationName |
| 111 | + * @param string $up |
| 112 | + * @param string $down |
| 113 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 114 | + */ |
| 115 | + public function write(string $migrationName, string $up, string $down) |
| 116 | + { |
| 117 | + $filename = date('Y_m_d_His') . '_create_' . $this->migrationType . '_' . $migrationName . '.php'; |
| 118 | + |
| 119 | + $path = $this->getMigrationPath() . '/' . $filename; |
| 120 | + |
| 121 | + $content = $this->generateMigrationContent($migrationName, $up, $down); |
| 122 | + |
| 123 | + $this->filesystem->put($path, $content); |
| 124 | + |
| 125 | + $this->line("<info>Created Migration:</info> {$filename}"); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Insert the migration information into the stub |
| 130 | + * @param string $migrationName |
| 131 | + * @param string $up |
| 132 | + * @param string $down |
| 133 | + * @return string|string[] |
| 134 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 135 | + */ |
| 136 | + public function generateMigrationContent(string $migrationName, string $up, string $down) |
| 137 | + { |
| 138 | + return str_replace( |
| 139 | + ['DummyClass', 'schema_up', 'schema_down'], |
| 140 | + [$this->getClassName($migrationName), $this->indent($up), $this->indent($down)], |
| 141 | + $this->filesystem->get(__DIR__ . '/stubs/migration.stub') |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Get the class name of the new migration file |
| 147 | + * @param string $migrationName |
| 148 | + * @return string |
| 149 | + */ |
| 150 | + protected function getClassName(string $migrationName) |
| 151 | + { |
| 152 | + return 'Create' . ucfirst($this->migrationType) . str_replace('_', '', $this->str->title($migrationName)); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Indent the migration |
| 157 | + * @param string $text |
| 158 | + * @return mixed |
| 159 | + */ |
| 160 | + protected function indent(string $text) |
| 161 | + { |
| 162 | + return str_replace("\n", "\n ", $text); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Escape the double quotes |
| 167 | + * @param string $text |
| 168 | + * @return mixed |
| 169 | + */ |
| 170 | + protected function escape(string $text) |
| 171 | + { |
| 172 | + return str_replace('"', '\"', $text); |
| 173 | + } |
| 174 | +} |
0 commit comments