SeederCreator.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Database\Seeders;
  12. use Hyperf\Stringable\Str;
  13. use Hyperf\Support\Filesystem\Filesystem;
  14. use InvalidArgumentException;
  15. class SeederCreator
  16. {
  17. /**
  18. * The filesystem instance.
  19. *
  20. * @var Filesystem
  21. */
  22. protected $files;
  23. /**
  24. * Create a new seeder creator instance.
  25. */
  26. public function __construct(Filesystem $files)
  27. {
  28. $this->files = $files;
  29. }
  30. /**
  31. * Create a new seeder at the given path.
  32. *
  33. * @param string $name
  34. * @param string $path
  35. * @return string
  36. */
  37. public function create($name, $path)
  38. {
  39. $this->ensureSeederDoesntAlreadyExist($name);
  40. $stub = $this->getStub();
  41. $this->files->put(
  42. $path = $this->getPath($name, $path),
  43. $this->populateStub($name, $stub)
  44. );
  45. return $path;
  46. }
  47. /**
  48. * Get the path to the stubs.
  49. *
  50. * @return string
  51. */
  52. public function stubPath()
  53. {
  54. return __DIR__ . '/stubs';
  55. }
  56. /**
  57. * Get the filesystem instance.
  58. *
  59. * @return Filesystem
  60. */
  61. public function getFilesystem()
  62. {
  63. return $this->files;
  64. }
  65. /**
  66. * Get the seeder stub file.
  67. *
  68. * @return string
  69. */
  70. protected function getStub()
  71. {
  72. return $this->files->get($this->stubPath() . '/seeder.stub');
  73. }
  74. /**
  75. * Populate the place-holders in the seeder stub.
  76. *
  77. * @param string $name
  78. * @param string $stub
  79. * @return string
  80. */
  81. protected function populateStub($name, $stub)
  82. {
  83. return str_replace('DummyClass', $this->getClassName($name), $stub);
  84. }
  85. /**
  86. * Ensure that a seeder with the given name doesn't already exist.
  87. *
  88. * @param string $name
  89. *
  90. * @throws InvalidArgumentException
  91. */
  92. protected function ensureSeederDoesntAlreadyExist($name)
  93. {
  94. if (class_exists($className = $this->getClassName($name))) {
  95. throw new InvalidArgumentException("A {$className} class already exists.");
  96. }
  97. }
  98. /**
  99. * Get the class name of a seeder name.
  100. *
  101. * @param string $name
  102. * @return string
  103. */
  104. protected function getClassName($name)
  105. {
  106. return Str::studly($name);
  107. }
  108. /**
  109. * Get the full path to the seeder.
  110. *
  111. * @param string $name
  112. * @param string $path
  113. * @return string
  114. */
  115. protected function getPath($name, $path)
  116. {
  117. return $path . '/' . $name . '.php';
  118. }
  119. }