Просмотр файла app/Models/Module.php

Размер файла: 3.72Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use Exception;
  8. use Illuminate\Support\Facades\Artisan;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Str;
  11. use Symfony\Component\Filesystem\Filesystem;
  12.  
  13. /**
  14. * Class Module
  15. *
  16. * @property int id
  17. * @property string version
  18. * @property string name
  19. * @property int disabled
  20. * @property int updated_at
  21. * @property int created_at
  22. */
  23. class Module extends BaseModel
  24. {
  25. /**
  26. * Indicates if the model should be timestamped.
  27. *
  28. * @var bool
  29. */
  30. public $timestamps = false;
  31.  
  32. /**
  33. * The attributes that aren't mass assignable.
  34. *
  35. * @var array
  36. */
  37. protected $guarded = [];
  38.  
  39. /**
  40. * Assets modules path
  41. */
  42. public $assetsPath = '/assets/modules/';
  43.  
  44. /**
  45. * Выполняет применение миграции
  46. *
  47. * @param string $modulePath
  48. */
  49. public function migrate(string $modulePath): void
  50. {
  51. $migrationPath = $modulePath . '/migrations';
  52.  
  53. if (file_exists($migrationPath)) {
  54. Artisan::call('migrate', [
  55. '--force' => true,
  56. '--realpath' => true,
  57. '--path' => $migrationPath,
  58. ]);
  59. }
  60. }
  61.  
  62. /**
  63. * Выполняет откат миграций
  64. *
  65. * @param string $modulePath
  66. */
  67. public function rollback(string $modulePath): void
  68. {
  69. $migrationPath = $modulePath . '/migrations';
  70.  
  71. if (file_exists($migrationPath)) {
  72. $migrator = app('migrator');
  73. $nextBatchNumber = $migrator->getRepository()->getNextBatchNumber();
  74. $migrationNames = array_keys($migrator->getMigrationFiles($migrationPath));
  75.  
  76. DB::table(config('database.migrations'))
  77. ->whereIn('migration', $migrationNames)
  78. ->update(['batch' => $nextBatchNumber]);
  79.  
  80. Artisan::call('migrate:rollback', [
  81. '--force' => true,
  82. '--realpath' => true,
  83. '--path' => $migrationPath,
  84. ]);
  85. }
  86. }
  87.  
  88.  
  89. /**
  90. * Создает симлинки модулей
  91. *
  92. * @param string $modulePath
  93. */
  94. public function createSymlink(string $modulePath): void
  95. {
  96. $filesystem = new Filesystem();
  97. $originPath = $this->getLinkName($modulePath);
  98. $modulesPath = $modulePath . '/resources/assets';
  99.  
  100. if (! file_exists($modulesPath)) {
  101. return;
  102. }
  103.  
  104. if (function_exists('symlink')) {
  105. $filesystem->symlink($modulesPath, $originPath, true);
  106. } else {
  107. $filesystem->mirror($modulesPath, $originPath, null, ['override' => true, 'delete' => true]);
  108. }
  109. }
  110.  
  111. /**
  112. * Удаляет симлинки модулей
  113. *
  114. * @param string $modulePath
  115. */
  116. public function deleteSymlink(string $modulePath): void
  117. {
  118. $originPath = $this->getLinkName($modulePath);
  119.  
  120. if (! file_exists($originPath)) {
  121. return;
  122. }
  123.  
  124. $filesystem = new Filesystem();
  125. $filesystem->remove($originPath);
  126. }
  127.  
  128. /**
  129. * Получает название директории для симлинка
  130. *
  131. * @param string $modulePath
  132. * @return string
  133. */
  134. public function getLinkName(string $modulePath): string
  135. {
  136. return public_path($this->assetsPath . Str::plural(strtolower(basename($modulePath))));
  137. }
  138.  
  139. /**
  140. * Get enabled modules
  141. *
  142. * @return array
  143. */
  144. public static function getEnabledModules(): array
  145. {
  146. try {
  147. $modules = self::query()->where('disabled', 0)->pluck('name')->all();
  148. } catch (Exception $e) {
  149. $modules = [];
  150. }
  151.  
  152. return $modules;
  153. }
  154. }