PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/ealexis.t/trends
PHP | 219 lines | 110 code | 30 blank | 79 comment | 2 complexity | e6a0f5c9980434aa0facc1b068d46f38 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 bool|null
  41. */
  42. public function fire()
  43. {
  44. $name = $this->parseName($this->getNameInput());
  45. $path = $this->getPath($name);
  46. if ($this->alreadyExists($this->getNameInput())) {
  47. $this->error($this->type.' already exists!');
  48. return false;
  49. }
  50. $this->makeDirectory($path);
  51. $this->files->put($path, $this->buildClass($name));
  52. $this->info($this->type.' created successfully.');
  53. }
  54. /**
  55. * Determine if the class already exists.
  56. *
  57. * @param string $rawName
  58. * @return bool
  59. */
  60. protected function alreadyExists($rawName)
  61. {
  62. $name = $this->parseName($rawName);
  63. return $this->files->exists($this->getPath($name));
  64. }
  65. /**
  66. * Get the destination class path.
  67. *
  68. * @param string $name
  69. * @return string
  70. */
  71. protected function getPath($name)
  72. {
  73. $name = str_replace($this->laravel->getNamespace(), '', $name);
  74. return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php';
  75. }
  76. /**
  77. * Parse the name and format according to the root namespace.
  78. *
  79. * @param string $name
  80. * @return string
  81. */
  82. protected function parseName($name)
  83. {
  84. $rootNamespace = $this->laravel->getNamespace();
  85. if (Str::startsWith($name, $rootNamespace)) {
  86. return $name;
  87. }
  88. if (Str::contains($name, '/')) {
  89. $name = str_replace('/', '\\', $name);
  90. }
  91. return $this->parseName($this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name);
  92. }
  93. /**
  94. * Get the default namespace for the class.
  95. *
  96. * @param string $rootNamespace
  97. * @return string
  98. */
  99. protected function getDefaultNamespace($rootNamespace)
  100. {
  101. return $rootNamespace;
  102. }
  103. /**
  104. * Build the directory for the class if necessary.
  105. *
  106. * @param string $path
  107. * @return string
  108. */
  109. protected function makeDirectory($path)
  110. {
  111. if (! $this->files->isDirectory(dirname($path))) {
  112. $this->files->makeDirectory(dirname($path), 0777, true, true);
  113. }
  114. }
  115. /**
  116. * Build the class with the given name.
  117. *
  118. * @param string $name
  119. * @return string
  120. */
  121. protected function buildClass($name)
  122. {
  123. $stub = $this->files->get($this->getStub());
  124. return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
  125. }
  126. /**
  127. * Replace the namespace for the given stub.
  128. *
  129. * @param string $stub
  130. * @param string $name
  131. * @return $this
  132. */
  133. protected function replaceNamespace(&$stub, $name)
  134. {
  135. $stub = str_replace(
  136. 'DummyNamespace', $this->getNamespace($name), $stub
  137. );
  138. $stub = str_replace(
  139. 'DummyRootNamespace', $this->laravel->getNamespace(), $stub
  140. );
  141. return $this;
  142. }
  143. /**
  144. * Get the full namespace name for a given class.
  145. *
  146. * @param string $name
  147. * @return string
  148. */
  149. protected function getNamespace($name)
  150. {
  151. return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
  152. }
  153. /**
  154. * Replace the class name for the given stub.
  155. *
  156. * @param string $stub
  157. * @param string $name
  158. * @return string
  159. */
  160. protected function replaceClass($stub, $name)
  161. {
  162. $class = str_replace($this->getNamespace($name).'\\', '', $name);
  163. return str_replace('DummyClass', $class, $stub);
  164. }
  165. /**
  166. * Get the desired class name from the input.
  167. *
  168. * @return string
  169. */
  170. protected function getNameInput()
  171. {
  172. return trim($this->argument('name'));
  173. }
  174. /**
  175. * Get the console command arguments.
  176. *
  177. * @return array
  178. */
  179. protected function getArguments()
  180. {
  181. return [
  182. ['name', InputArgument::REQUIRED, 'The name of the class'],
  183. ];
  184. }
  185. }