관리-도구
편집 파일: DatabaseManager.php
<?php namespace Modules\LMS\Classes; use Exception; use Illuminate\Database\SQLiteConnection; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; use Symfony\Component\Console\Output\BufferedOutput; class DatabaseManager { /** * Migrate and seed the database. * * @return array */ public function migrateAndSeed() { $outputLog = new BufferedOutput; $this->sqlite($outputLog); return $this->migrate($outputLog); } /** * Migrate and seed the database. * * @return array */ public function migrateTable() { $outputLog = new BufferedOutput; $this->sqlite($outputLog); return $this->migrateOnlyTable($outputLog); } /** * Run the migration and call the seeder. * * @return array */ private function migrate(BufferedOutput $outputLog) { try { Artisan::call('migrate:fresh', ['--force' => true], $outputLog); } catch (Exception $e) { return $this->response($e->getMessage(), 'error', $outputLog); } return $this->seed($outputLog); } /** * Run the migration table. */ private function migrateOnlyTable(BufferedOutput $outputLog) { try { Artisan::call('migrate:fresh', ['--force' => true], $outputLog); } catch (Exception $e) { return $this->response($e->getMessage(), 'error', $outputLog); } } /** * Seed the database. * * @return array */ private function seed(BufferedOutput $outputLog) { try { Artisan::call('module:seed', ['-a' => true], $outputLog); } catch (Exception $e) { return $this->response($e->getMessage(), 'error', $outputLog); } return $this->response('Application\'s database has been successfully updated.', 'success', $outputLog); } /** * Return a formatted error messages. * * @param string $message * @param string $status * @return array */ private function response($message, $status, BufferedOutput $outputLog) { return [ 'status' => $status, 'message' => $message, 'dbOutputLog' => $outputLog->fetch(), ]; } /** * Check database type. If SQLite, then create the database file. */ private function sqlite(BufferedOutput $outputLog) { if (DB::connection() instanceof SQLiteConnection) { $database = DB::connection()->getDatabaseName(); if (! file_exists($database)) { touch($database); DB::reconnect(Config::get('database.default')); } $outputLog->write('Using SqlLite database: '.$database, 1); } } }