ControllerCreator.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Dcat\Admin\Scaffold;
  3. use Dcat\Admin\Exception\AdminException;
  4. use Dcat\Admin\Support\Helper;
  5. class ControllerCreator
  6. {
  7. use GridCreator, FormCreator, ShowCreator;
  8. /**
  9. * Controller full name.
  10. *
  11. * @var string
  12. */
  13. protected $name;
  14. /**
  15. * The filesystem instance.
  16. *
  17. * @var \Illuminate\Filesystem\Filesystem
  18. */
  19. protected $files;
  20. /**
  21. * ControllerCreator constructor.
  22. *
  23. * @param string $name
  24. * @param null $files
  25. */
  26. public function __construct($name, $files = null)
  27. {
  28. $this->name = $name;
  29. $this->files = $files ?: app('files');
  30. }
  31. /**
  32. * Create a controller.
  33. *
  34. * @param string $model
  35. *
  36. * @throws \Exception
  37. *
  38. * @return string
  39. */
  40. public function create($model)
  41. {
  42. $path = $this->getPath($this->name);
  43. $dir = dirname($path);
  44. if (! is_dir($dir)) {
  45. $this->files->makeDirectory($dir, 0755, true);
  46. }
  47. if ($this->files->exists($path)) {
  48. throw new AdminException("Controller [$this->name] already exists!");
  49. }
  50. $stub = $this->files->get($this->getStub());
  51. $slug = str_replace('Controller', '', class_basename($this->name));
  52. $model = $model ?: 'App\Admin\Repositories\\'.$slug;
  53. $this->files->put($path, $this->replace($stub, $this->name, $model, $slug));
  54. $this->files->chmod($path, 0777);
  55. return $path;
  56. }
  57. /**
  58. * @param string $stub
  59. * @param string $name
  60. * @param string $model
  61. *
  62. * @return string
  63. */
  64. protected function replace($stub, $name, $model, $slug)
  65. {
  66. $stub = $this->replaceClass($stub, $name);
  67. return str_replace(
  68. [
  69. 'DummyModelNamespace',
  70. 'DummyModel',
  71. 'DummyTitle',
  72. '{controller}',
  73. '{grid}',
  74. '{form}',
  75. '{show}',
  76. ],
  77. [
  78. $model,
  79. class_basename($model),
  80. class_basename($model),
  81. $slug,
  82. $this->generateGrid(),
  83. $this->generateForm(),
  84. $this->generateShow(),
  85. ],
  86. $stub
  87. );
  88. }
  89. /**
  90. * Get controller namespace from giving name.
  91. *
  92. * @param string $name
  93. *
  94. * @return string
  95. */
  96. protected function getNamespace($name)
  97. {
  98. return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
  99. }
  100. /**
  101. * Replace the class name for the given stub.
  102. *
  103. * @param string $stub
  104. * @param string $name
  105. *
  106. * @return string
  107. */
  108. protected function replaceClass($stub, $name)
  109. {
  110. $class = str_replace($this->getNamespace($name).'\\', '', $name);
  111. return str_replace(['DummyClass', 'DummyNamespace'], [$class, $this->getNamespace($name)], $stub);
  112. }
  113. /**
  114. * Get file path from giving controller name.
  115. *
  116. * @param string $name
  117. *
  118. * @return string
  119. */
  120. public function getPath($name)
  121. {
  122. return Helper::guessClassFileName($name);
  123. }
  124. /**
  125. * Get stub file path.
  126. *
  127. * @return string
  128. */
  129. public function getStub()
  130. {
  131. return __DIR__.'/stubs/controller.stub';
  132. }
  133. }