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

Размер файла: 3.35Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use Illuminate\Support\Str;
  8. use Phinx\Config\Config;
  9. use Phinx\Console\Command\Migrate;
  10. use Phinx\Console\PhinxApplication;
  11. use Phinx\Wrapper\TextWrapper;
  12. use Symfony\Component\Filesystem\Filesystem;
  13.  
  14. /**
  15. * Class Module
  16. *
  17. * @property int id
  18. * @property string version
  19. * @property string name
  20. * @property int disabled
  21. * @property int updated_at
  22. * @property int created_at
  23. */
  24. class Module extends BaseModel
  25. {
  26. /**
  27. * Indicates if the model should be timestamped.
  28. *
  29. * @var bool
  30. */
  31. public $timestamps = false;
  32.  
  33. /**
  34. * The attributes that aren't mass assignable.
  35. *
  36. * @var array
  37. */
  38. protected $guarded = [];
  39.  
  40. /**
  41. * Assets modules path
  42. */
  43. public $assetsPath = HOME . '/assets/modules/';
  44.  
  45. /**
  46. * Выполняет применение миграции
  47. *
  48. * @param string $modulePath
  49. */
  50. public function migrate(string $modulePath): void
  51. {
  52. $migrationPath = $modulePath . '/migrations';
  53.  
  54. if (file_exists($migrationPath)) {
  55. $app = new PhinxApplication();
  56. $app->add(new Migrate());
  57.  
  58. /** @var Migrate $command */
  59. $command = $app->find('migrate');
  60.  
  61. $config = require APP . '/migration.php';
  62. $config['paths']['migrations'] = $migrationPath;
  63.  
  64. $command->setConfig(new Config($config));
  65.  
  66. $wrap = new TextWrapper($app);
  67. $wrap->getMigrate();
  68. }
  69. }
  70.  
  71. /**
  72. * Выполняет откат миграций
  73. *
  74. * @param string $modulePath
  75. */
  76. public function rollback(string $modulePath): void
  77. {
  78. $migrationPath = $modulePath . '/migrations';
  79.  
  80. if (file_exists($migrationPath)) {
  81. $app = new PhinxApplication();
  82. $app->add(new Migrate());
  83.  
  84. /** @var Migrate $command */
  85. $command = $app->find('rollback');
  86.  
  87. $config = require APP . '/migration.php';
  88. $config['paths']['migrations'] = $migrationPath;
  89.  
  90. $command->setConfig(new Config($config));
  91.  
  92. $wrap = new TextWrapper($app);
  93. $wrap->getRollback(null, 0);
  94. }
  95. }
  96.  
  97. /**
  98. * Создает симлинки модулей
  99. *
  100. * @param string $modulePath
  101. */
  102. public function createSymlink(string $modulePath): void
  103. {
  104. $filesystem = new Filesystem();
  105. $originPath = $this->getLinkName($modulePath);
  106. $modulesPath = $modulePath . '/resources/assets';
  107.  
  108. if (! file_exists($modulesPath)) {
  109. return;
  110. }
  111.  
  112. $filesystem->symlink($modulesPath, $originPath, true);
  113. }
  114.  
  115. /**
  116. * Удаляет симлинки модулей
  117. *
  118. * @param string $modulePath
  119. */
  120. public function deleteSymlink(string $modulePath): void
  121. {
  122. $originPath = $this->getLinkName($modulePath);
  123.  
  124. if (! file_exists($originPath)) {
  125. return;
  126. }
  127.  
  128. $filesystem = new Filesystem();
  129. $filesystem->remove($originPath);
  130. }
  131.  
  132. /**
  133. * Получает название директории для симлинка
  134. *
  135. * @param string $modulePath
  136. * @return string
  137. */
  138. public function getLinkName(string $modulePath): string
  139. {
  140. return $this->assetsPath . Str::plural(strtolower(basename($modulePath)));
  141. }
  142. }