ControllerCreator.php 3.3 KB

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