ActionCommand.php 3.1 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-tool' => 'Tree',
  39. ];
  40. public function handle()
  41. {
  42. $this->choice = $this->choice(
  43. 'Which type of action would you like to make?',
  44. $choices = $this->actionTyps()
  45. );
  46. INPUT_NAME:
  47. $this->className = ucfirst(trim($this->ask('Please enter a name of action class')));
  48. if (! $this->className) {
  49. goto INPUT_NAME;
  50. }
  51. $this->namespace = ucfirst(trim($this->ask('Please enter the namespace of action class', $this->getDefaultNamespace(null))));
  52. $this->askBaseDirectory();
  53. return parent::handle();
  54. }
  55. /**
  56. * @return array
  57. */
  58. protected function actionTyps()
  59. {
  60. return [
  61. 'default',
  62. 'grid-batch',
  63. 'grid-row',
  64. 'grid-tool',
  65. 'form-tool',
  66. 'show-tool',
  67. 'tree-tool',
  68. ];
  69. }
  70. /**
  71. * Replace the class name for the given stub.
  72. *
  73. * @param string $stub
  74. * @param string $name
  75. *
  76. * @return string
  77. */
  78. protected function replaceClass($stub, $name)
  79. {
  80. $stub = parent::replaceClass($stub, $name);
  81. return str_replace(
  82. [
  83. 'DummyName',
  84. ],
  85. [
  86. $this->className,
  87. ],
  88. $stub
  89. );
  90. }
  91. /**
  92. * Get the stub file for the generator.
  93. *
  94. * @return string
  95. */
  96. public function getStub()
  97. {
  98. return __DIR__."/stubs/actions/{$this->choice}.stub";
  99. }
  100. /**
  101. * Get the default namespace for the class.
  102. *
  103. * @param string $rootNamespace
  104. *
  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. }