|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Gingdev\NetteExtension; |
| 6 | + |
| 7 | +use Illuminate\Database\Capsule\Manager; |
| 8 | +use Illuminate\Database\ConnectionResolver; |
| 9 | +use Illuminate\Database\Migrations\DatabaseMigrationRepository; |
| 10 | +use Illuminate\Database\Migrations\MigrationCreator; |
| 11 | +use Illuminate\Database\Migrations\Migrator; |
| 12 | +use Illuminate\Filesystem\Filesystem; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | + |
| 15 | +class Migration |
| 16 | +{ |
| 17 | + protected Migrator $migrator; |
| 18 | + |
| 19 | + public function __construct() |
| 20 | + { |
| 21 | + $connection = Manager::connection(); |
| 22 | + |
| 23 | + $resolver = new ConnectionResolver(['default' => $connection]); |
| 24 | + $resolver->setDefaultConnection('default'); |
| 25 | + |
| 26 | + $repository = new DatabaseMigrationRepository($resolver, 'migrations'); |
| 27 | + |
| 28 | + if (!$repository->repositoryExists()) { |
| 29 | + $repository->createRepository(); |
| 30 | + } |
| 31 | + |
| 32 | + $this->migrator = new Migrator($repository, $resolver, new Filesystem()); |
| 33 | + } |
| 34 | + |
| 35 | + protected function getMigrationPath() |
| 36 | + { |
| 37 | + return getcwd().DIRECTORY_SEPARATOR.'migrations'; |
| 38 | + } |
| 39 | + |
| 40 | + public function create(string $name, ?string $table, bool $create = false) |
| 41 | + { |
| 42 | + $creator = new MigrationCreator(new Filesystem(), __DIR__.DIRECTORY_SEPARATOR.'stub'); |
| 43 | + |
| 44 | + $path = $creator->create($name, $this->getMigrationPath(), $table, $create); |
| 45 | + |
| 46 | + return pathinfo($path, PATHINFO_FILENAME); |
| 47 | + } |
| 48 | + |
| 49 | + public function setMigratorOutput(OutputInterface $output): void |
| 50 | + { |
| 51 | + $this->migrator->setOutput($output); |
| 52 | + } |
| 53 | + |
| 54 | + public function run(array $options = []): array |
| 55 | + { |
| 56 | + return $this->migrator->run($this->getMigrationPath(), $options); |
| 57 | + } |
| 58 | + |
| 59 | + public function rollback(array $options = []): array |
| 60 | + { |
| 61 | + return $this->migrator->rollback($this->getMigrationPath(), $options); |
| 62 | + } |
| 63 | +} |
0 commit comments