/Front_End/vendor/laravel/framework/src/Illuminate/Foundation/Console/AppNameCommand.php

https://gitlab.com/Sigpot/AirSpot · PHP · 286 lines · 133 code · 41 blank · 112 comment · 0 complexity · 86234e404d7fe06f57c18939e133dcc3 MD5 · raw file

  1. <?php
  2. namespace Illuminate\Foundation\Console;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Composer;
  5. use Symfony\Component\Finder\Finder;
  6. use Illuminate\Filesystem\Filesystem;
  7. use Symfony\Component\Console\Input\InputArgument;
  8. class AppNameCommand extends Command
  9. {
  10. /**
  11. * The console command name.
  12. *
  13. * @var string
  14. */
  15. protected $name = 'app:name';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Set the application namespace';
  22. /**
  23. * The Composer class instance.
  24. *
  25. * @var \Illuminate\Support\Composer
  26. */
  27. protected $composer;
  28. /**
  29. * The filesystem instance.
  30. *
  31. * @var \Illuminate\Filesystem\Filesystem
  32. */
  33. protected $files;
  34. /**
  35. * Current root application namespace.
  36. *
  37. * @var string
  38. */
  39. protected $currentRoot;
  40. /**
  41. * Create a new key generator command.
  42. *
  43. * @param \Illuminate\Support\Composer $composer
  44. * @param \Illuminate\Filesystem\Filesystem $files
  45. * @return void
  46. */
  47. public function __construct(Composer $composer, Filesystem $files)
  48. {
  49. parent::__construct();
  50. $this->files = $files;
  51. $this->composer = $composer;
  52. }
  53. /**
  54. * Execute the console command.
  55. *
  56. * @return void
  57. */
  58. public function fire()
  59. {
  60. $this->currentRoot = trim($this->laravel->getNamespace(), '\\');
  61. $this->setBootstrapNamespaces();
  62. $this->setAppDirectoryNamespace();
  63. $this->setConfigNamespaces();
  64. $this->setComposerNamespace();
  65. $this->setDatabaseFactoryNamespaces();
  66. $this->info('Application namespace set!');
  67. $this->composer->dumpAutoloads();
  68. $this->call('clear-compiled');
  69. }
  70. /**
  71. * Set the namespace on the files in the app directory.
  72. *
  73. * @return void
  74. */
  75. protected function setAppDirectoryNamespace()
  76. {
  77. $files = Finder::create()
  78. ->in($this->laravel['path'])
  79. ->contains($this->currentRoot)
  80. ->name('*.php');
  81. foreach ($files as $file) {
  82. $this->replaceNamespace($file->getRealPath());
  83. }
  84. }
  85. /**
  86. * Replace the App namespace at the given path.
  87. *
  88. * @param string $path
  89. * @return void
  90. */
  91. protected function replaceNamespace($path)
  92. {
  93. $search = [
  94. 'namespace '.$this->currentRoot.';',
  95. $this->currentRoot.'\\',
  96. ];
  97. $replace = [
  98. 'namespace '.$this->argument('name').';',
  99. $this->argument('name').'\\',
  100. ];
  101. $this->replaceIn($path, $search, $replace);
  102. }
  103. /**
  104. * Set the bootstrap namespaces.
  105. *
  106. * @return void
  107. */
  108. protected function setBootstrapNamespaces()
  109. {
  110. $search = [
  111. $this->currentRoot.'\\Http',
  112. $this->currentRoot.'\\Console',
  113. $this->currentRoot.'\\Exceptions',
  114. ];
  115. $replace = [
  116. $this->argument('name').'\\Http',
  117. $this->argument('name').'\\Console',
  118. $this->argument('name').'\\Exceptions',
  119. ];
  120. $this->replaceIn($this->getBootstrapPath(), $search, $replace);
  121. }
  122. /**
  123. * Set the PSR-4 namespace in the Composer file.
  124. *
  125. * @return void
  126. */
  127. protected function setComposerNamespace()
  128. {
  129. $this->replaceIn(
  130. $this->getComposerPath(), str_replace('\\', '\\\\', $this->currentRoot).'\\\\', str_replace('\\', '\\\\', $this->argument('name')).'\\\\'
  131. );
  132. }
  133. /**
  134. * Set the namespace in the appropriate configuration files.
  135. *
  136. * @return void
  137. */
  138. protected function setConfigNamespaces()
  139. {
  140. $this->setAppConfigNamespaces();
  141. $this->setAuthConfigNamespace();
  142. $this->setServicesConfigNamespace();
  143. }
  144. /**
  145. * Set the application provider namespaces.
  146. *
  147. * @return void
  148. */
  149. protected function setAppConfigNamespaces()
  150. {
  151. $search = [
  152. $this->currentRoot.'\\Providers',
  153. $this->currentRoot.'\\Http\\Controllers\\',
  154. ];
  155. $replace = [
  156. $this->argument('name').'\\Providers',
  157. $this->argument('name').'\\Http\\Controllers\\',
  158. ];
  159. $this->replaceIn($this->getConfigPath('app'), $search, $replace);
  160. }
  161. /**
  162. * Set the authentication User namespace.
  163. *
  164. * @return void
  165. */
  166. protected function setAuthConfigNamespace()
  167. {
  168. $this->replaceIn(
  169. $this->getConfigPath('auth'), $this->currentRoot.'\\User', $this->argument('name').'\\User'
  170. );
  171. }
  172. /**
  173. * Set the services User namespace.
  174. *
  175. * @return void
  176. */
  177. protected function setServicesConfigNamespace()
  178. {
  179. $this->replaceIn(
  180. $this->getConfigPath('services'), $this->currentRoot.'\\User', $this->argument('name').'\\User'
  181. );
  182. }
  183. /**
  184. * Set the namespace in database factory files.
  185. *
  186. * @return void
  187. */
  188. protected function setDatabaseFactoryNamespaces()
  189. {
  190. $this->replaceIn(
  191. $this->laravel->databasePath().'/factories/ModelFactory.php', $this->currentRoot, $this->argument('name')
  192. );
  193. }
  194. /**
  195. * Replace the given string in the given file.
  196. *
  197. * @param string $path
  198. * @param string|array $search
  199. * @param string|array $replace
  200. * @return void
  201. */
  202. protected function replaceIn($path, $search, $replace)
  203. {
  204. $this->files->put($path, str_replace($search, $replace, $this->files->get($path)));
  205. }
  206. /**
  207. * Get the path to the bootstrap/app.php file.
  208. *
  209. * @return string
  210. */
  211. protected function getBootstrapPath()
  212. {
  213. return $this->laravel->bootstrapPath().'/app.php';
  214. }
  215. /**
  216. * Get the path to the Composer.json file.
  217. *
  218. * @return string
  219. */
  220. protected function getComposerPath()
  221. {
  222. return $this->laravel->basePath().'/composer.json';
  223. }
  224. /**
  225. * Get the path to the given configuration file.
  226. *
  227. * @param string $name
  228. * @return string
  229. */
  230. protected function getConfigPath($name)
  231. {
  232. return $this->laravel['path.config'].'/'.$name.'.php';
  233. }
  234. /**
  235. * Get the console command arguments.
  236. *
  237. * @return array
  238. */
  239. protected function getArguments()
  240. {
  241. return [
  242. ['name', InputArgument::REQUIRED, 'The desired namespace.'],
  243. ];
  244. }
  245. }