abterphp /
admin
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace AbterPhp\Admin\Bootstrappers\Database; |
||
| 6 | |||
| 7 | use AbterPhp\Admin\Databases\Migrations\Init; |
||
| 8 | use AbterPhp\Framework\Constant\Env; |
||
| 9 | use Opulence\Databases\Adapters\Pdo\MySql\Driver as MySqlDriver; |
||
| 10 | use Opulence\Databases\Adapters\Pdo\PostgreSql\Driver as PostgreSqlDriver; |
||
| 11 | use Opulence\Databases\IConnection; |
||
| 12 | use Opulence\Ioc\Bootstrappers\Bootstrapper; |
||
| 13 | use Opulence\Ioc\Bootstrappers\ILazyBootstrapper; |
||
| 14 | use Opulence\Ioc\IContainer; |
||
| 15 | |||
| 16 | class MigrationsBootstrapper extends Bootstrapper implements ILazyBootstrapper |
||
| 17 | { |
||
| 18 | const MODULE_KEY = 'AbterPhp\\Admin'; |
||
| 19 | |||
| 20 | const MIGRATIONS_PATH = '/migrations'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @return array |
||
| 24 | */ |
||
| 25 | public function getBindings(): array |
||
| 26 | { |
||
| 27 | return [ |
||
| 28 | Init::class, |
||
| 29 | ]; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param IContainer $container |
||
| 34 | * |
||
| 35 | * @throws \Opulence\Ioc\IocException |
||
| 36 | */ |
||
| 37 | public function registerBindings(IContainer $container) |
||
| 38 | { |
||
| 39 | $migrationsPath = $this->getMigrationPath(); |
||
| 40 | $driver = $this->getDriver(); |
||
| 41 | |||
| 42 | /** @var IConnection $connection */ |
||
| 43 | $connection = $container->resolve(IConnection::class); |
||
| 44 | |||
| 45 | $migration = new Init($connection, $migrationsPath, $driver); |
||
| 46 | |||
| 47 | $container->bindInstance(Init::class, $migration); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public function getMigrationPath(): string |
||
| 54 | { |
||
| 55 | global $abterModuleManager; |
||
| 56 | |||
| 57 | $resourcePaths = $abterModuleManager->getResourcePaths(); |
||
| 58 | |||
| 59 | if (empty($resourcePaths[static::MODULE_KEY])) { |
||
| 60 | throw new \RuntimeException("Invalid resource path."); |
||
| 61 | } |
||
| 62 | |||
| 63 | return $resourcePaths[static::MODULE_KEY] . static::MIGRATIONS_PATH; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | public function getDriver(): string |
||
| 70 | { |
||
| 71 | $dirMigrations = getenv(Env::DIR_MIGRATIONS); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 72 | $driverClass = getenv(Env::DB_DRIVER) ?: PostgreSqlDriver::class; |
||
| 73 | |||
| 74 | switch ($driverClass) { |
||
| 75 | case MySqlDriver::class: |
||
| 76 | return 'mysql'; |
||
| 77 | case PostgreSqlDriver::class: |
||
| 78 | return 'pgsql'; |
||
| 79 | } |
||
| 80 | |||
| 81 | throw new \RuntimeException( |
||
| 82 | "Invalid database driver type specified in environment var \"DB_DRIVER\": $driverClass" |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | } |
||
| 86 |