ActionCommand.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. *
  78. * @return string
  79. */
  80. protected function replaceClass($stub, $name)
  81. {
  82. $stub = parent::replaceClass($stub, $name);
  83. return str_replace(
  84. [
  85. 'DummyName',
  86. ],
  87. [
  88. $this->className,
  89. ],
  90. $stub
  91. );
  92. }
  93. /**
  94. * Get the stub file for the generator.
  95. *
  96. * @return string
  97. */
  98. public function getStub()
  99. {
  100. return __DIR__."/stubs/actions/{$this->choice}.stub";
  101. }
  102. /**
  103. * Get the default namespace for the class.
  104. *
  105. * @param string $rootNamespace
  106. *
  107. * @return string
  108. */
  109. protected function getDefaultNamespace($rootNamespace)
  110. {
  111. if ($this->namespace) {
  112. return $this->namespace;
  113. }
  114. $segments = explode('\\', config('admin.route.namespace'));
  115. array_pop($segments);
  116. array_push($segments, 'Actions');
  117. if (isset($this->namespaceMap[$this->choice])) {
  118. array_push($segments, $this->namespaceMap[$this->choice]);
  119. }
  120. return implode('\\', $segments);
  121. }
  122. /**
  123. * Get the desired class name from the input.
  124. *
  125. * @return string
  126. */
  127. protected function getNameInput()
  128. {
  129. $this->type = $this->qualifyClass($this->className);
  130. return $this->className;
  131. }
  132. }