Creator.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace think\migration;
  3. use InvalidArgumentException;
  4. use Phinx\Util\Util;
  5. use RuntimeException;
  6. use think\App;
  7. class Creator
  8. {
  9. protected $app;
  10. public function __construct(App $app)
  11. {
  12. $this->app = $app;
  13. }
  14. public function create(string $className, string $appName = '', string $pluginName = '')
  15. {
  16. $path = $this->ensureDirectory($appName, $pluginName);
  17. if (!Util::isValidPhinxClassName($className)) {
  18. throw new InvalidArgumentException(sprintf('The migration class name "%s" is invalid. Please use CamelCase format.', $className));
  19. }
  20. if ($appName) {
  21. $className = 'MigrationApp' . cmf_parse_name($appName, 1) . $className;
  22. } elseif ($pluginName) {
  23. $className = 'MigrationPlugin' . cmf_parse_name($pluginName, 1) . $className;
  24. } else {
  25. $className = 'MigrationCmf' . $className;
  26. }
  27. if (!Util::isUniqueMigrationClassName($className, $path)) {
  28. throw new InvalidArgumentException(sprintf('The migration class name "%s" already exists', $className));
  29. }
  30. // Compute the file path
  31. $fileName = Util::mapClassNameToFileName($className);
  32. $filePath = $path . DIRECTORY_SEPARATOR . $fileName;
  33. if (is_file($filePath)) {
  34. throw new InvalidArgumentException(sprintf('The file "%s" already exists', $filePath));
  35. }
  36. // Verify that the template creation class (or the aliased class) exists and that it implements the required interface.
  37. $aliasedClassName = null;
  38. // Load the alternative template if it is defined.
  39. $contents = file_get_contents($this->getTemplate());
  40. // inject the class names appropriate to this migration
  41. $contents = strtr($contents, [
  42. '{%MigratorClass%}' => $className,
  43. ]);
  44. if (false === file_put_contents($filePath, $contents)) {
  45. throw new RuntimeException(sprintf('The file "%s" could not be written to', $path));
  46. }
  47. return $filePath;
  48. }
  49. protected function ensureDirectory(string $appName = '', string $pluginName = '')
  50. {
  51. if ($appName) {
  52. $path = $this->app->getAppPath() . $appName . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'migrations';
  53. } elseif ($pluginName) {
  54. $path = WEB_ROOT . 'plugins' . DIRECTORY_SEPARATOR . cmf_parse_name($pluginName) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'migrations';
  55. } else {
  56. $path = $this->app->getRootPath() . 'vendor/thinkcmf/cmf/src/data/migrations';
  57. }
  58. if (!is_dir($path) && !mkdir($path, 0755, true)) {
  59. throw new InvalidArgumentException(sprintf('directory "%s" does not exist', $path));
  60. }
  61. if (!is_writable($path)) {
  62. throw new InvalidArgumentException(sprintf('directory "%s" is not writable', $path));
  63. }
  64. return $path;
  65. }
  66. protected function getTemplate()
  67. {
  68. return __DIR__ . '/command/stubs/migrate.stub';
  69. }
  70. }