/blog/vendor/laravel/framework/src/Illuminate/Console/GeneratorCommand.php

https://gitlab.com/zan_zan/laravel_sample · PHP · 204 lines · 87 code · 31 blank · 86 comment · 4 complexity · 352ee83221f451ef735a4b633435c938 MD5 · raw file

  1. <?php
  2. namespace Illuminate\Console;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Filesystem\Filesystem;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. abstract class GeneratorCommand extends Command
  7. {
  8. /**
  9. * The filesystem instance.
  10. *
  11. * @var \Illuminate\Filesystem\Filesystem
  12. */
  13. protected $files;
  14. /**
  15. * The type of class being generated.
  16. *
  17. * @var string
  18. */
  19. protected $type;
  20. /**
  21. * Create a new controller creator command instance.
  22. *
  23. * @param \Illuminate\Filesystem\Filesystem $files
  24. * @return void
  25. */
  26. public function __construct(Filesystem $files)
  27. {
  28. parent::__construct();
  29. $this->files = $files;
  30. }
  31. /**
  32. * Get the stub file for the generator.
  33. *
  34. * @return string
  35. */
  36. abstract protected function getStub();
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return void
  41. */
  42. public function fire()
  43. {
  44. $name = $this->parseName($this->getNameInput());
  45. if ($this->files->exists($path = $this->getPath($name))) {
  46. $this->error($this->type.' already exists!');
  47. return false;
  48. }
  49. $this->makeDirectory($path);
  50. $this->files->put($path, $this->buildClass($name));
  51. $this->info($this->type.' created successfully.');
  52. }
  53. /**
  54. * Get the destination class path.
  55. *
  56. * @param string $name
  57. * @return string
  58. */
  59. protected function getPath($name)
  60. {
  61. $name = str_replace($this->laravel->getNamespace(), '', $name);
  62. return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php';
  63. }
  64. /**
  65. * Parse the name and format according to the root namespace.
  66. *
  67. * @param string $name
  68. * @return string
  69. */
  70. protected function parseName($name)
  71. {
  72. $rootNamespace = $this->laravel->getNamespace();
  73. if (Str::startsWith($name, $rootNamespace)) {
  74. return $name;
  75. }
  76. if (Str::contains($name, '/')) {
  77. $name = str_replace('/', '\\', $name);
  78. }
  79. return $this->parseName($this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name);
  80. }
  81. /**
  82. * Get the default namespace for the class.
  83. *
  84. * @param string $rootNamespace
  85. * @return string
  86. */
  87. protected function getDefaultNamespace($rootNamespace)
  88. {
  89. return $rootNamespace;
  90. }
  91. /**
  92. * Build the directory for the class if necessary.
  93. *
  94. * @param string $path
  95. * @return string
  96. */
  97. protected function makeDirectory($path)
  98. {
  99. if (!$this->files->isDirectory(dirname($path))) {
  100. $this->files->makeDirectory(dirname($path), 0777, true, true);
  101. }
  102. }
  103. /**
  104. * Build the class with the given name.
  105. *
  106. * @param string $name
  107. * @return string
  108. */
  109. protected function buildClass($name)
  110. {
  111. $stub = $this->files->get($this->getStub());
  112. return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
  113. }
  114. /**
  115. * Replace the namespace for the given stub.
  116. *
  117. * @param string $stub
  118. * @param string $name
  119. * @return $this
  120. */
  121. protected function replaceNamespace(&$stub, $name)
  122. {
  123. $stub = str_replace(
  124. 'DummyNamespace', $this->getNamespace($name), $stub
  125. );
  126. $stub = str_replace(
  127. 'DummyRootNamespace', $this->laravel->getNamespace(), $stub
  128. );
  129. return $this;
  130. }
  131. /**
  132. * Get the full namespace name for a given class.
  133. *
  134. * @param string $name
  135. * @return string
  136. */
  137. protected function getNamespace($name)
  138. {
  139. return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
  140. }
  141. /**
  142. * Replace the class name for the given stub.
  143. *
  144. * @param string $stub
  145. * @param string $name
  146. * @return string
  147. */
  148. protected function replaceClass($stub, $name)
  149. {
  150. $class = str_replace($this->getNamespace($name).'\\', '', $name);
  151. return str_replace('DummyClass', $class, $stub);
  152. }
  153. /**
  154. * Get the desired class name from the input.
  155. *
  156. * @return string
  157. */
  158. protected function getNameInput()
  159. {
  160. return $this->argument('name');
  161. }
  162. /**
  163. * Get the console command arguments.
  164. *
  165. * @return array
  166. */
  167. protected function getArguments()
  168. {
  169. return [
  170. ['name', InputArgument::REQUIRED, 'The name of the class'],
  171. ];
  172. }
  173. }