ActionCommand.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Dcat\Admin\Console;
  3. class ActionCommand extends GeneratorCommand
  4. {
  5. /**
  6. * The name and signature of the console command.
  7. *
  8. * @var string
  9. */
  10. protected $signature = 'admin:action';
  11. /**
  12. * The console command description.
  13. *
  14. * @var string
  15. */
  16. protected $description = 'Make a admin action';
  17. /**
  18. * @var string
  19. */
  20. protected $choice;
  21. /**
  22. * @var string
  23. */
  24. protected $className;
  25. /**
  26. * @var string
  27. */
  28. protected $namespace;
  29. /**
  30. * @var array
  31. */
  32. protected $namespaceMap = [
  33. 'grid-batch' => 'Grid',
  34. 'grid-row' => 'Grid',
  35. 'grid-tool' => 'Grid',
  36. 'form-tool' => 'Form',
  37. 'show-tool' => 'Show',
  38. 'tree-row' => 'Tree',
  39. 'tree-tool' => 'Tree',
  40. ];
  41. public function handle()
  42. {
  43. $this->choice = $this->choice(
  44. 'Which type of action would you like to make?',
  45. $choices = $this->actionTyps()
  46. );
  47. INPUT_NAME:
  48. $this->className = ucfirst(trim($this->ask('Please enter a name of action class')));
  49. if (! $this->className) {
  50. goto INPUT_NAME;
  51. }
  52. $this->namespace = ucfirst(trim($this->ask('Please enter the namespace of action class', $this->getDefaultNamespace(null))));
  53. $this->askBaseDirectory();
  54. return parent::handle();
  55. }
  56. /**
  57. * @return array
  58. */
  59. protected function actionTyps()
  60. {
  61. return [
  62. 'default',
  63. 'grid-batch',
  64. 'grid-row',
  65. 'grid-tool',
  66. 'form-tool',
  67. 'show-tool',
  68. 'tree-row',
  69. 'tree-tool',
  70. ];
  71. }
  72. /**
  73. * Replace the class name for the given stub.
  74. *
  75. * @param string $stub
  76. * @param string $name
  77. * @return string
  78. */
  79. protected function replaceClass($stub, $name)
  80. {
  81. $stub = parent::replaceClass($stub, $name);
  82. return str_replace(
  83. [
  84. 'DummyName',
  85. ],
  86. [
  87. $this->className,
  88. ],
  89. $stub
  90. );
  91. }
  92. /**
  93. * Get the stub file for the generator.
  94. *
  95. * @return string
  96. */
  97. public function getStub()
  98. {
  99. return __DIR__."/stubs/actions/{$this->choice}.stub";
  100. }
  101. /**
  102. * Get the default namespace for the class.
  103. *
  104. * @param string $rootNamespace
  105. * @return string
  106. */
  107. protected function getDefaultNamespace($rootNamespace)
  108. {
  109. if ($this->namespace) {
  110. return $this->namespace;
  111. }
  112. $segments = explode('\\', config('admin.route.namespace'));
  113. array_pop($segments);
  114. array_push($segments, 'Actions');
  115. if (isset($this->namespaceMap[$this->choice])) {
  116. array_push($segments, $this->namespaceMap[$this->choice]);
  117. }
  118. return implode('\\', $segments);
  119. }
  120. /**
  121. * Get the desired class name from the input.
  122. *
  123. * @return string
  124. */
  125. protected function getNameInput()
  126. {
  127. $this->type = $this->qualifyClass($this->className);
  128. return $this->className;
  129. }
  130. }