ControllerCreator.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 = $model ?: 'App\Admin\Repositories\\'.$slug;
  51. $this->files->put($path, $this->replace($stub, $this->name, $model, $slug));
  52. $this->files->chmod($path, 0777);
  53. return $path;
  54. }
  55. /**
  56. * @param string $stub
  57. * @param string $name
  58. * @param string $model
  59. *
  60. * @return string
  61. */
  62. protected function replace($stub, $name, $model, $slug)
  63. {
  64. $stub = $this->replaceClass($stub, $name);
  65. return str_replace(
  66. [
  67. 'DummyModelNamespace',
  68. 'DummyModel',
  69. 'DummyTitle',
  70. '{controller}',
  71. '{grid}',
  72. '{form}',
  73. '{show}',
  74. ],
  75. [
  76. $model,
  77. class_basename($model),
  78. class_basename($model),
  79. $slug,
  80. $this->generateGrid(),
  81. $this->generateForm(),
  82. $this->generateShow(),
  83. ],
  84. $stub
  85. );
  86. }
  87. /**
  88. * Get controller namespace from giving name.
  89. *
  90. * @param string $name
  91. *
  92. * @return string
  93. */
  94. protected function getNamespace($name)
  95. {
  96. return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
  97. }
  98. /**
  99. * Replace the class name for the given stub.
  100. *
  101. * @param string $stub
  102. * @param string $name
  103. *
  104. * @return string
  105. */
  106. protected function replaceClass($stub, $name)
  107. {
  108. $class = str_replace($this->getNamespace($name).'\\', '', $name);
  109. return str_replace(['DummyClass', 'DummyNamespace'], [$class, $this->getNamespace($name)], $stub);
  110. }
  111. /**
  112. * Get file path from giving controller name.
  113. *
  114. * @param $name
  115. *
  116. * @return string
  117. */
  118. public function getPath($name)
  119. {
  120. $segments = explode('\\', $name);
  121. array_shift($segments);
  122. return app_path(implode('/', $segments)).'.php';
  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. }