DatabaseManager.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Webkul\Installer\Helpers;
  3. use Exception;
  4. use Illuminate\Support\Facades\Artisan;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Schema;
  7. use Webkul\Installer\Database\Seeders\DatabaseSeeder as BagistoDatabaseSeeder;
  8. use Webkul\Installer\Database\Seeders\ProductTableSeeder;
  9. class DatabaseManager
  10. {
  11. /**
  12. * Check Database Connection.
  13. */
  14. public function isInstalled()
  15. {
  16. if (! file_exists(base_path('.env'))) {
  17. return false;
  18. }
  19. try {
  20. DB::connection()->getPDO();
  21. $isConnected = (bool) DB::connection()->getDatabaseName();
  22. if (! $isConnected) {
  23. return false;
  24. }
  25. $hasTable = Schema::hasTable('admins');
  26. if (! $hasTable) {
  27. return false;
  28. }
  29. $userCount = DB::table('admins')->count();
  30. if (! $userCount) {
  31. return false;
  32. }
  33. return true;
  34. } catch (Exception $e) {
  35. return false;
  36. }
  37. }
  38. /**
  39. * Drop all the tables and migrate in the database
  40. *
  41. * @return void|string
  42. */
  43. public function migration()
  44. {
  45. try {
  46. Artisan::call('migrate:fresh');
  47. } catch (Exception $e) {
  48. return response()->json([
  49. 'error' => $e->getMessage(),
  50. ], 500);
  51. }
  52. }
  53. /**
  54. * Seed the database.
  55. *
  56. * @return void|string
  57. */
  58. public function seeder($data)
  59. {
  60. $data['parameter'] = [
  61. 'default_locale' => $data['parameter']['default_locales'],
  62. 'allowed_locales' => $data['parameter']['allowed_locales'],
  63. 'default_currency' => $data['parameter']['default_currency'],
  64. 'allowed_currencies' => $data['parameter']['allowed_currencies'],
  65. 'skip_admin_creation' => $data['parameter']['skip_admin_creation'],
  66. ];
  67. try {
  68. app(BagistoDatabaseSeeder::class)->run($data['parameter']);
  69. $this->storageLink();
  70. } catch (Exception $e) {
  71. return $e->getMessage();
  72. }
  73. }
  74. /**
  75. * Storage Link.
  76. */
  77. private function storageLink()
  78. {
  79. Artisan::call('storage:link');
  80. }
  81. /**
  82. * Generate New Application Key
  83. */
  84. public function generateKey()
  85. {
  86. try {
  87. Artisan::call('key:generate');
  88. } catch (Exception $e) {
  89. }
  90. }
  91. /**
  92. * Generate fake product data.
  93. *
  94. * @return void|string
  95. */
  96. public function seedSampleProducts($parameters)
  97. {
  98. try {
  99. app(ProductTableSeeder::class)->run($parameters);
  100. } catch (Exception $e) {
  101. return response()->json([
  102. 'error' => $e->getMessage(),
  103. ], 500);
  104. }
  105. }
  106. }